Wrap types in namespace for --ts-flat-files and --gen-all (#7451)

* Wrap types in namespace for --ts-flat-files and --gen-all

* Fixes for escaping object types

* Added to generate_code
This commit is contained in:
Derek Bailey
2022-08-16 12:52:26 -07:00
committed by GitHub
parent f7c511957f
commit 82b75407a3
48 changed files with 2152 additions and 256 deletions

0
tests/TypeScriptTest.py Normal file → Executable file
View File

View File

@@ -0,0 +1,14 @@
include "baz/baz_with_ns.fbs";
include "baz/baz.fbs";
namespace bar;
table Bar {
baz:baz.Baz;
baz2:Baz;
foo:Foo;
}
table Foo {
a:int;
}

View File

@@ -0,0 +1,8 @@
namespace baz;
enum Baz : short {
None = 0,
Red,
Green,
Blue,
}

View File

@@ -66,7 +66,7 @@ class TsTests():
assert_file_and_contents(
"foo_with_ns_generated.ts",
[
"export { Bar } from './bar';",
"export { Bar } from './bar/bar';",
"export { Foo } from './something/foo';",
],
)
@@ -77,7 +77,7 @@ class TsTests():
"something/foo.ts",
[
"export class Foo {",
"import { Bar } from '../bar';",
"import { Bar } from '../bar/bar';",
],
)
@@ -142,6 +142,23 @@ class TsTests():
# The root type Foo should not be generated in its own file.
assert_file_doesnt_exists("foo.ts")
def FlatFilesWithNamespace(self):
# Generate just foo with the flat files option
flatc(["--ts", "--ts-flat-files", "foo_with_ns.fbs"])
# Should generate a single file that imports bar as a single file, and
# exports the Foo table.
assert_file_and_contents(
"foo_with_ns_generated.ts",
[
"import {Bar as Bar} from './bar_with_ns_generated';",
"export class Foo {",
],
)
# The root type Foo should not be generated in its own file.
assert_file_doesnt_exists("foo.ts")
def FlatFilesMultipleFiles(self):
# Generate both foo and bar with the flat files option
flatc(["--ts", "--ts-flat-files", "foo.fbs", "bar/bar.fbs"])
@@ -194,3 +211,32 @@ class TsTests():
assert_file_doesnt_exists("foo.ts")
assert_file_doesnt_exists("bar.ts")
assert_file_doesnt_exists("baz.ts")
def ZFlatFilesGenAllWithNamespacing(self):
# Generate foo with all of its dependents with the flat files option
flatc(["--ts", "--ts-flat-files", "--gen-all", "foo_with_ns.fbs"])
# Should generate a single foo file
assert_file_and_contents(
"foo_with_ns_generated.ts",
# Should export each of the types within the single file
[
"export class bar_Bar {",
"export class bar_Foo {",
"export enum Baz {",
"export enum baz_Baz {",
"export class something_Foo {"
],
# No includes for the dependent types should be present.
doesnt_contain=[
"import {Bar as Bar}",
"import {Baz as Baz}",
],
)
# The types Foo, Bar and Baz should not be generated in their own files.
assert_file_doesnt_exists("foo.ts")
assert_file_doesnt_exists("bar.ts")
assert_file_doesnt_exists("baz.ts")

View File

@@ -1,7 +1,9 @@
include "bar/bar.fbs";
include "bar/bar_with_ns.fbs";
table Foo {
bar:Bar;
bar2:bar.Bar;
}
root_type Foo;

View File

@@ -1,9 +1,9 @@
include "bar/bar.fbs";
include "bar/bar_with_ns.fbs";
namespace something;
table Foo {
bar:Bar;
bar:bar.Bar;
}
root_type Foo;

View File

@@ -27,3 +27,5 @@ print("{0} of {1} tests passed".format(passing, passing + failing))
if failing > 0:
sys.exit(1)
# TsTests().Base()

View File

@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { Monster } from './my-game/example/monster';
export { Monster as MyGameExample2Monster, MonsterT as MyGameExample2MonsterT } from './my-game/example2/monster';
export { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from './my-game/example2/monster';
export { Ability, AbilityT } from './my-game/example/ability';
export { Any, unionToAny, unionListToAny } from './my-game/example/any';
export { AnyAmbiguousAliases, unionToAnyAmbiguousAliases, unionListToAnyAmbiguousAliases } from './my-game/example/any-ambiguous-aliases';

View File

@@ -24,7 +24,7 @@ export class Ability {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.Ability';
return 'MyGame_Example_Ability';
}
static sizeOf() {
return 8;

View File

@@ -32,7 +32,7 @@ mutate_distance(value:number):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Ability';
return 'MyGame_Example_Ability';
}
static sizeOf():number {

View File

@@ -36,4 +36,3 @@ export function unionListToAnyAmbiguousAliases(
default: return null;
}
}

View File

@@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
import { Monster as MyGameExample2Monster } from '../../my-game/example2/monster';
import { Monster as MyGame_Example2_Monster } from '../../my-game/example2/monster';
import { Monster } from '../../my-game/example/monster';
import { TestSimpleTableWithEnum } from '../../my-game/example/test-simple-table-with-enum';
export var AnyUniqueAliases;
@@ -14,7 +14,7 @@ export function unionToAnyUniqueAliases(type, accessor) {
case 'NONE': return null;
case 'M': return accessor(new Monster());
case 'TS': return accessor(new TestSimpleTableWithEnum());
case 'M2': return accessor(new MyGameExample2Monster());
case 'M2': return accessor(new MyGame_Example2_Monster());
default: return null;
}
}
@@ -23,7 +23,7 @@ export function unionListToAnyUniqueAliases(type, accessor, index) {
case 'NONE': return null;
case 'M': return accessor(index, new Monster());
case 'TS': return accessor(index, new TestSimpleTableWithEnum());
case 'M2': return accessor(index, new MyGameExample2Monster());
case 'M2': return accessor(index, new MyGame_Example2_Monster());
default: return null;
}
}

View File

@@ -1,6 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
import { Monster as MyGameExample2Monster, MonsterT as MyGameExample2MonsterT } from '../../my-game/example2/monster';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster';
import { Monster, MonsterT } from '../../my-game/example/monster';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum';
@@ -14,28 +14,27 @@ export enum AnyUniqueAliases {
export function unionToAnyUniqueAliases(
type: AnyUniqueAliases,
accessor: (obj:Monster|MyGameExample2Monster|TestSimpleTableWithEnum) => Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null
): Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null {
accessor: (obj:Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum) => Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null
): Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null {
switch(AnyUniqueAliases[type]) {
case 'NONE': return null;
case 'M': return accessor(new Monster())! as Monster;
case 'TS': return accessor(new TestSimpleTableWithEnum())! as TestSimpleTableWithEnum;
case 'M2': return accessor(new MyGameExample2Monster())! as MyGameExample2Monster;
case 'M2': return accessor(new MyGame_Example2_Monster())! as MyGame_Example2_Monster;
default: return null;
}
}
export function unionListToAnyUniqueAliases(
type: AnyUniqueAliases,
accessor: (index: number, obj:Monster|MyGameExample2Monster|TestSimpleTableWithEnum) => Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null,
accessor: (index: number, obj:Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum) => Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null,
index: number
): Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null {
): Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null {
switch(AnyUniqueAliases[type]) {
case 'NONE': return null;
case 'M': return accessor(index, new Monster())! as Monster;
case 'TS': return accessor(index, new TestSimpleTableWithEnum())! as TestSimpleTableWithEnum;
case 'M2': return accessor(index, new MyGameExample2Monster())! as MyGameExample2Monster;
case 'M2': return accessor(index, new MyGame_Example2_Monster())! as MyGame_Example2_Monster;
default: return null;
}
}

View File

@@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
import { Monster as MyGameExample2Monster } from '../../my-game/example2/monster';
import { Monster as MyGame_Example2_Monster } from '../../my-game/example2/monster';
import { Monster } from '../../my-game/example/monster';
import { TestSimpleTableWithEnum } from '../../my-game/example/test-simple-table-with-enum';
export var Any;
@@ -14,7 +14,7 @@ export function unionToAny(type, accessor) {
case 'NONE': return null;
case 'Monster': return accessor(new Monster());
case 'TestSimpleTableWithEnum': return accessor(new TestSimpleTableWithEnum());
case 'MyGame_Example2_Monster': return accessor(new MyGameExample2Monster());
case 'MyGame_Example2_Monster': return accessor(new MyGame_Example2_Monster());
default: return null;
}
}
@@ -23,7 +23,7 @@ export function unionListToAny(type, accessor, index) {
case 'NONE': return null;
case 'Monster': return accessor(index, new Monster());
case 'TestSimpleTableWithEnum': return accessor(index, new TestSimpleTableWithEnum());
case 'MyGame_Example2_Monster': return accessor(index, new MyGameExample2Monster());
case 'MyGame_Example2_Monster': return accessor(index, new MyGame_Example2_Monster());
default: return null;
}
}

View File

@@ -1,6 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
import { Monster as MyGameExample2Monster, MonsterT as MyGameExample2MonsterT } from '../../my-game/example2/monster';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster';
import { Monster, MonsterT } from '../../my-game/example/monster';
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum';
@@ -14,28 +14,27 @@ export enum Any {
export function unionToAny(
type: Any,
accessor: (obj:Monster|MyGameExample2Monster|TestSimpleTableWithEnum) => Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null
): Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null {
accessor: (obj:Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum) => Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null
): Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null {
switch(Any[type]) {
case 'NONE': return null;
case 'Monster': return accessor(new Monster())! as Monster;
case 'TestSimpleTableWithEnum': return accessor(new TestSimpleTableWithEnum())! as TestSimpleTableWithEnum;
case 'MyGame_Example2_Monster': return accessor(new MyGameExample2Monster())! as MyGameExample2Monster;
case 'MyGame_Example2_Monster': return accessor(new MyGame_Example2_Monster())! as MyGame_Example2_Monster;
default: return null;
}
}
export function unionListToAny(
type: Any,
accessor: (index: number, obj:Monster|MyGameExample2Monster|TestSimpleTableWithEnum) => Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null,
accessor: (index: number, obj:Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum) => Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null,
index: number
): Monster|MyGameExample2Monster|TestSimpleTableWithEnum|null {
): Monster|MyGame_Example2_Monster|TestSimpleTableWithEnum|null {
switch(Any[type]) {
case 'NONE': return null;
case 'Monster': return accessor(index, new Monster())! as Monster;
case 'TestSimpleTableWithEnum': return accessor(index, new TestSimpleTableWithEnum())! as TestSimpleTableWithEnum;
case 'MyGame_Example2_Monster': return accessor(index, new MyGameExample2Monster())! as MyGameExample2Monster;
case 'MyGame_Example2_Monster': return accessor(index, new MyGame_Example2_Monster())! as MyGame_Example2_Monster;
default: return null;
}
}

View File

@@ -17,4 +17,3 @@ export enum Color {
*/
Blue = 8
}

View File

@@ -5,4 +5,3 @@ export enum LongEnum {
LongTwo = '4',
LongBig = '1099511627776'
}

View File

@@ -527,7 +527,7 @@ export class Monster {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.Monster';
return 'MyGame_Example_Monster';
}
static startMonster(builder) {
builder.startObject(54);

View File

@@ -2,7 +2,7 @@
import * as flatbuffers from 'flatbuffers';
import { Monster as MyGameExample2Monster, MonsterT as MyGameExample2MonsterT } from '../../my-game/example2/monster';
import { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from '../../my-game/example2/monster';
import { Ability, AbilityT } from '../../my-game/example/ability';
import { Any, unionToAny, unionListToAny } from '../../my-game/example/any';
import { AnyAmbiguousAliases, unionToAnyAmbiguousAliases, unionListToAnyAmbiguousAliases } from '../../my-game/example/any-ambiguous-aliases';
@@ -684,7 +684,7 @@ mutate_long_enum_normal_default(value:bigint):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Monster';
return 'MyGame_Example_Monster';
}
static startMonster(builder:flatbuffers.Builder) {
@@ -1296,7 +1296,7 @@ constructor(
public inventory: (number)[] = [],
public color: Color = Color.Blue,
public testType: Any = Any.NONE,
public test: MonsterT|MyGameExample2MonsterT|TestSimpleTableWithEnumT|null = null,
public test: MonsterT|MyGame_Example2_MonsterT|TestSimpleTableWithEnumT|null = null,
public test4: (TestT)[] = [],
public testarrayofstring: (string)[] = [],
public testarrayoftables: (MonsterT)[] = [],
@@ -1332,7 +1332,7 @@ constructor(
public nonOwningReference: bigint = BigInt('0'),
public vectorOfNonOwningReferences: (bigint)[] = [],
public anyUniqueType: AnyUniqueAliases = AnyUniqueAliases.NONE,
public anyUnique: MonsterT|MyGameExample2MonsterT|TestSimpleTableWithEnumT|null = null,
public anyUnique: MonsterT|MyGame_Example2_MonsterT|TestSimpleTableWithEnumT|null = null,
public anyAmbiguousType: AnyAmbiguousAliases = AnyAmbiguousAliases.NONE,
public anyAmbiguous: MonsterT|null = null,
public vectorOfEnums: (Color)[] = [],

View File

@@ -6,4 +6,3 @@ export enum Race {
Dwarf = 1,
Elf = 2
}

View File

@@ -30,7 +30,7 @@ export class Referrable {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.Referrable';
return 'MyGame_Example_Referrable';
}
static startReferrable(builder) {
builder.startObject(1);

View File

@@ -39,7 +39,7 @@ mutate_id(value:bigint):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Referrable';
return 'MyGame_Example_Referrable';
}
static startReferrable(builder:flatbuffers.Builder) {

View File

@@ -46,7 +46,7 @@ export class Stat {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.Stat';
return 'MyGame_Example_Stat';
}
static startStat(builder) {
builder.startObject(3);

View File

@@ -62,7 +62,7 @@ mutate_count(value:number):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Stat';
return 'MyGame_Example_Stat';
}
static startStat(builder:flatbuffers.Builder) {

View File

@@ -19,7 +19,7 @@ a(obj?:StructOfStructs):StructOfStructs|null {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.StructOfStructsOfStructs';
return 'MyGame_Example_StructOfStructsOfStructs';
}
static sizeOf():number {

View File

@@ -21,7 +21,7 @@ export class StructOfStructs {
return (obj || new Ability()).__init(this.bb_pos + 12, this.bb);
}
static getFullyQualifiedName() {
return 'MyGame.Example.StructOfStructs';
return 'MyGame_Example_StructOfStructs';
}
static sizeOf() {
return 20;

View File

@@ -28,7 +28,7 @@ c(obj?:Ability):Ability|null {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.StructOfStructs';
return 'MyGame_Example_StructOfStructs';
}
static sizeOf():number {

View File

@@ -31,7 +31,7 @@ export class TestSimpleTableWithEnum {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.TestSimpleTableWithEnum';
return 'MyGame_Example_TestSimpleTableWithEnum';
}
static startTestSimpleTableWithEnum(builder) {
builder.startObject(1);

View File

@@ -40,7 +40,7 @@ mutate_color(value:Color):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.TestSimpleTableWithEnum';
return 'MyGame_Example_TestSimpleTableWithEnum';
}
static startTestSimpleTableWithEnum(builder:flatbuffers.Builder) {

View File

@@ -24,7 +24,7 @@ export class Test {
return true;
}
static getFullyQualifiedName() {
return 'MyGame.Example.Test';
return 'MyGame_Example_Test';
}
static sizeOf() {
return 4;

View File

@@ -32,7 +32,7 @@ mutate_b(value:number):boolean {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Test';
return 'MyGame_Example_Test';
}
static sizeOf():number {

View File

@@ -162,7 +162,7 @@ export class TypeAliases {
return offset ? new Float64Array(this.bb.bytes().buffer, this.bb.bytes().byteOffset + this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
}
static getFullyQualifiedName() {
return 'MyGame.Example.TypeAliases';
return 'MyGame_Example_TypeAliases';
}
static startTypeAliases(builder) {
builder.startObject(12);

View File

@@ -213,7 +213,7 @@ vf64Array():Float64Array|null {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.TypeAliases';
return 'MyGame_Example_TypeAliases';
}
static startTypeAliases(builder:flatbuffers.Builder) {

View File

@@ -49,7 +49,7 @@ export class Vec3 {
return (obj || new Test()).__init(this.bb_pos + 26, this.bb);
}
static getFullyQualifiedName() {
return 'MyGame.Example.Vec3';
return 'MyGame_Example_Vec3';
}
static sizeOf() {
return 32;

View File

@@ -65,7 +65,7 @@ test3(obj?:Test):Test|null {
}
static getFullyQualifiedName():string {
return 'MyGame.Example.Vec3';
return 'MyGame_Example_Vec3';
}
static sizeOf():number {

View File

@@ -18,7 +18,7 @@ export class Monster {
return (obj || new Monster()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
static getFullyQualifiedName() {
return 'MyGame.Example2.Monster';
return 'MyGame_Example2_Monster';
}
static startMonster(builder) {
builder.startObject(0);

View File

@@ -23,7 +23,7 @@ static getSizePrefixedRootAsMonster(bb:flatbuffers.ByteBuffer, obj?:Monster):Mon
}
static getFullyQualifiedName():string {
return 'MyGame.Example2.Monster';
return 'MyGame_Example2_Monster';
}
static startMonster(builder:flatbuffers.Builder) {

View File

@@ -18,7 +18,7 @@ export class InParentNamespace {
return (obj || new InParentNamespace()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
static getFullyQualifiedName() {
return 'MyGame.InParentNamespace';
return 'MyGame_InParentNamespace';
}
static startInParentNamespace(builder) {
builder.startObject(0);

View File

@@ -23,7 +23,7 @@ static getSizePrefixedRootAsInParentNamespace(bb:flatbuffers.ByteBuffer, obj?:In
}
static getFullyQualifiedName():string {
return 'MyGame.InParentNamespace';
return 'MyGame_InParentNamespace';
}
static startInParentNamespace(builder:flatbuffers.Builder) {

View File

@@ -5,4 +5,3 @@ export enum OptionalByte {
One = 1,
Two = 2
}

View File

@@ -1,4 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { ScalarStuff } from './optional-scalars/scalar-stuff';
export { OptionalByte } from './optional-scalars/optional-byte';
export { ScalarStuff } from './optional-scalars/scalar-stuff';

File diff suppressed because it is too large Load Diff

View File

@@ -47,4 +47,3 @@ export function unionListToCharacter(
default: return null;
}
}

View File

@@ -34,4 +34,3 @@ export function unionListToGadget(
default: return null;
}
}