[JS] Add getFullyQualifiedName() (#6119)

This optionally generates a static `getFullyQualifiedName()` function to get
the fully-qualified name of a type in JavaScript and TypeScript in a similar
fashion to the C++ codegen.
This commit is contained in:
Anass Al
2020-09-17 12:19:07 -07:00
committed by GitHub
parent f96d1ef744
commit b8e87fafe4
16 changed files with 213 additions and 8 deletions

View File

@@ -1783,6 +1783,18 @@ class JsTsGenerator : public BaseGenerator {
}
}
// Emit the fully qualified name
if (parser_.opts.generate_name_strings) {
GenDocComment(code_ptr, GenTypeAnnotation(kReturns, "string", "", false));
if (lang_.language == IDLOptions::kTs) {
code += "static getFullyQualifiedName():string {\n";
} else {
code += object_name + ".getFullyQualifiedName = function() {\n";
}
code += " return '" + WrapInNameSpace(struct_def) + "';\n";
code += "}\n\n";
}
// Emit a factory constructor
if (struct_def.fixed) {
std::string annotations =

View File

@@ -16,9 +16,9 @@
pushd "$(dirname $0)" >/dev/null
../flatc -b -I include_test monster_test.fbs unicode_test.json
../flatc --js -o js --gen-mutable --no-fb-import -I include_test monster_test.fbs
../flatc --js -o js --gen-name-strings --gen-mutable --no-fb-import -I include_test monster_test.fbs
node JavaScriptTest ./js/monster_test_generated
../flatc --js -o js --no-fb-import union_vector/union_vector.fbs
../flatc --js --gen-name-strings -o js --no-fb-import union_vector/union_vector.fbs
node JavaScriptUnionVectorTest ./js/union_vector_generated
node JavaScriptFlexBuffersTest

View File

@@ -20,12 +20,12 @@ npm install @types/flatbuffers
export FB_TS_TEST="TRUE"
../flatc --ts --no-fb-import --gen-mutable --gen-object-api -o ts -I include_test monster_test.fbs
../flatc --ts --no-fb-import --gen-name-strings --gen-mutable --gen-object-api -o ts -I include_test monster_test.fbs
../flatc --gen-object-api -b -I include_test monster_test.fbs unicode_test.json
tsc --strict --noUnusedParameters --noUnusedLocals --noImplicitReturns --strictNullChecks ts/monster_test_generated.ts
node JavaScriptTest ./ts/monster_test_generated
../flatc --ts --no-fb-import --gen-object-api -o ts union_vector/union_vector.fbs
../flatc --ts --gen-name-strings --no-fb-import --gen-object-api -o ts union_vector/union_vector.fbs
tsc --strict --noUnusedParameters --noUnusedLocals --noImplicitReturns --strictNullChecks ts/union_vector_generated.ts
node JavaScriptUnionVectorTest ./ts/union_vector_generated

View File

@@ -29,6 +29,7 @@ if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" (
set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS%
set TEST_CS_FLAGS=--cs-gen-json-serializer
set TEST_JS_TS_FLAGS=--gen-name-strings
set TEST_RUST_FLAGS=--gen-name-strings
set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api
set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes --no-fb-import
@@ -40,9 +41,9 @@ set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes --no-fb-import
..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL
..\%buildtype%\flatc.exe --binary --cpp --java --csharp --dart --go --lobster --lua --js --ts --php --python --rust ^
%TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL
%TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% %TEST_JS_TS_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --js --ts --php %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% -o union_vector ./union_vector/union_vector.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --js --ts --php %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% %TEST_JS_TS_FLAGS% -o union_vector ./union_vector/union_vector.fbs || goto FAIL
..\%buildtype%\flatc.exe --rust -I include_test -o include_test include_test/include_test1.fbs || goto FAIL
..\%buildtype%\flatc.exe --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs || goto FAIL
..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs || goto FAIL

View File

@@ -26,6 +26,7 @@ fi
TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS"
TEST_CS_FLAGS="--cs-gen-json-serializer"
TEST_JS_TS_FLAGS="--gen-name-strings"
TEST_BASE_FLAGS="--reflect-names --gen-mutable --gen-object-api"
TEST_RUST_FLAGS="$TEST_BASE_FLAGS --gen-name-strings"
TEST_NOINCL_FLAGS="$TEST_BASE_FLAGS --no-includes --no-fb-import"
@@ -37,9 +38,9 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -I include_test monster_test.f
../flatc --python $TEST_BASE_FLAGS -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --js --ts --php --python --rust \
$TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
$TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_JS_TS_FLAGS -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../flatc --cpp --java --kotlin --csharp --js --ts --php $TEST_BASE_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -o union_vector ./union_vector/union_vector.fbs
../flatc --cpp --java --kotlin --csharp --js --ts --php $TEST_BASE_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_JS_TS_FLAGS -o union_vector ./union_vector/union_vector.fbs
../flatc --rust -I include_test -o include_test include_test/include_test1.fbs
../flatc --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs

View File

@@ -66,6 +66,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return StructInNestedNSTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.NamespaceB.StructInNestedNS";
}
StructInNestedNS()
: a_(0),
b_(0) {
@@ -102,6 +105,9 @@ inline bool operator!=(const StructInNestedNS &lhs, const StructInNestedNS &rhs)
struct TableInNestedNST : public flatbuffers::NativeTable {
typedef TableInNestedNS TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.NamespaceB.TableInNestedNST";
}
int32_t foo;
TableInNestedNST()
: foo(0) {
@@ -124,6 +130,9 @@ struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInNestedNSTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.NamespaceB.TableInNestedNS";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_FOO = 4
};

View File

@@ -98,6 +98,13 @@ NamespaceA.NamespaceB.TableInNestedNS.prototype.mutate_foo = function(value) {
return true;
};
/**
* @returns {string}
*/
NamespaceA.NamespaceB.TableInNestedNS.getFullyQualifiedName = function() {
return 'NamespaceA.NamespaceB.TableInNestedNS';
}
/**
* @param {flatbuffers.Builder} builder
*/
@@ -191,6 +198,13 @@ NamespaceA.NamespaceB.StructInNestedNS.prototype.mutate_b = function(value) {
return true;
};
/**
* @returns {string}
*/
NamespaceA.NamespaceB.StructInNestedNS.getFullyQualifiedName = function() {
return 'NamespaceA.NamespaceB.StructInNestedNS';
}
/**
* @param {flatbuffers.Builder} builder
* @param {number} a

View File

@@ -141,6 +141,10 @@ impl StructInNestedNS {
}
}
pub const fn get_fully_qualified_name() -> &'static str {
"NamespaceA.NamespaceB.StructInNestedNS"
}
pub fn a(&self) -> i32 {
self.a_.from_little_endian()
}
@@ -165,6 +169,10 @@ impl<'a> flatbuffers::Follow<'a> for TableInNestedNS<'a> {
}
impl<'a> TableInNestedNS<'a> {
pub const fn get_fully_qualified_name() -> &'static str {
"NamespaceA.NamespaceB.TableInNestedNS"
}
#[inline]
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
TableInNestedNS {

View File

@@ -72,6 +72,13 @@ mutate_foo(value:number):boolean {
return true;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'NamespaceA.NamespaceB.TableInNestedNS';
}
/**
* @param flatbuffers.Builder builder
*/
@@ -190,6 +197,13 @@ mutate_b(value:number):boolean {
return true;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'NamespaceA.NamespaceB.StructInNestedNS';
}
/**
* @param flatbuffers.Builder builder
* @param number a

View File

@@ -59,6 +59,9 @@ inline const flatbuffers::TypeTable *SecondTableInATypeTable();
struct TableInFirstNST : public flatbuffers::NativeTable {
typedef TableInFirstNS TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.TableInFirstNST";
}
flatbuffers::unique_ptr<NamespaceA::NamespaceB::TableInNestedNST> foo_table;
NamespaceA::NamespaceB::EnumInNestedNS foo_enum;
flatbuffers::unique_ptr<NamespaceA::NamespaceB::StructInNestedNS> foo_struct;
@@ -85,6 +88,9 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInFirstNSTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.TableInFirstNS";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_FOO_TABLE = 4,
VT_FOO_ENUM = 6,
@@ -165,6 +171,9 @@ namespace NamespaceC {
struct TableInCT : public flatbuffers::NativeTable {
typedef TableInC TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceC.TableInCT";
}
flatbuffers::unique_ptr<NamespaceA::TableInFirstNST> refer_to_a1;
flatbuffers::unique_ptr<NamespaceA::SecondTableInAT> refer_to_a2;
TableInCT() {
@@ -188,6 +197,9 @@ struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return TableInCTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceC.TableInC";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_REFER_TO_A1 = 4,
VT_REFER_TO_A2 = 6
@@ -256,6 +268,9 @@ namespace NamespaceA {
struct SecondTableInAT : public flatbuffers::NativeTable {
typedef SecondTableInA TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.SecondTableInAT";
}
flatbuffers::unique_ptr<NamespaceC::TableInCT> refer_to_c;
SecondTableInAT() {
}
@@ -277,6 +292,9 @@ struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return SecondTableInATypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "NamespaceA.SecondTableInA";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_REFER_TO_C = 4
};

View File

@@ -104,6 +104,13 @@ NamespaceA.TableInFirstNS.prototype.fooStruct = function(obj) {
return offset ? (obj || new NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb) : null;
};
/**
* @returns {string}
*/
NamespaceA.TableInFirstNS.getFullyQualifiedName = function() {
return 'NamespaceA.TableInFirstNS';
}
/**
* @param {flatbuffers.Builder} builder
*/
@@ -207,6 +214,13 @@ NamespaceC.TableInC.prototype.referToA2 = function(obj) {
return offset ? (obj || new NamespaceA.SecondTableInA).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
};
/**
* @returns {string}
*/
NamespaceC.TableInC.getFullyQualifiedName = function() {
return 'NamespaceC.TableInC';
}
/**
* @param {flatbuffers.Builder} builder
*/
@@ -293,6 +307,13 @@ NamespaceA.SecondTableInA.prototype.referToC = function(obj) {
return offset ? (obj || new NamespaceC.TableInC).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
};
/**
* @returns {string}
*/
NamespaceA.SecondTableInA.getFullyQualifiedName = function() {
return 'NamespaceA.SecondTableInA';
}
/**
* @param {flatbuffers.Builder} builder
*/

View File

@@ -35,6 +35,10 @@ impl<'a> flatbuffers::Follow<'a> for TableInFirstNS<'a> {
}
impl<'a> TableInFirstNS<'a> {
pub const fn get_fully_qualified_name() -> &'static str {
"NamespaceA.TableInFirstNS"
}
#[inline]
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
TableInFirstNS {
@@ -133,6 +137,10 @@ impl<'a> flatbuffers::Follow<'a> for SecondTableInA<'a> {
}
impl<'a> SecondTableInA<'a> {
pub const fn get_fully_qualified_name() -> &'static str {
"NamespaceA.SecondTableInA"
}
#[inline]
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
SecondTableInA {
@@ -219,6 +227,10 @@ impl<'a> flatbuffers::Follow<'a> for TableInC<'a> {
}
impl<'a> TableInC<'a> {
pub const fn get_fully_qualified_name() -> &'static str {
"NamespaceC.TableInC"
}
#[inline]
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
TableInC {

View File

@@ -80,6 +80,13 @@ fooStruct(obj?:NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS):NS8
return offset ? (obj || new NS8755221360535654258.NamespaceA.NamespaceB.StructInNestedNS()).__init(this.bb_pos + offset, this.bb!) : null;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'NamespaceA.TableInFirstNS';
}
/**
* @param flatbuffers.Builder builder
*/
@@ -225,6 +232,13 @@ referToA2(obj?:NamespaceA.SecondTableInA):NamespaceA.SecondTableInA|null {
return offset ? (obj || new NamespaceA.SecondTableInA()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'NamespaceC.TableInC';
}
/**
* @param flatbuffers.Builder builder
*/
@@ -348,6 +362,13 @@ referToC(obj?:NamespaceC.TableInC):NamespaceC.TableInC|null {
return offset ? (obj || new NamespaceC.TableInC()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'NamespaceA.SecondTableInA';
}
/**
* @param flatbuffers.Builder builder
*/

View File

@@ -202,6 +202,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return RapunzelTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "Rapunzel";
}
Rapunzel()
: hair_length_(0) {
}
@@ -235,6 +238,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return BookReaderTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "BookReader";
}
BookReader()
: books_read_(0) {
}
@@ -262,6 +268,9 @@ inline bool operator!=(const BookReader &lhs, const BookReader &rhs) {
struct AttackerT : public flatbuffers::NativeTable {
typedef Attacker TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "AttackerT";
}
int32_t sword_attack_damage;
AttackerT()
: sword_attack_damage(0) {
@@ -284,6 +293,9 @@ struct Attacker FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return AttackerTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "Attacker";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_SWORD_ATTACK_DAMAGE = 4
};
@@ -333,6 +345,9 @@ flatbuffers::Offset<Attacker> CreateAttacker(flatbuffers::FlatBufferBuilder &_fb
struct MovieT : public flatbuffers::NativeTable {
typedef Movie TableType;
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "MovieT";
}
CharacterUnion main_character;
std::vector<CharacterUnion> characters;
MovieT() {
@@ -356,6 +371,9 @@ struct Movie FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return MovieTypeTable();
}
static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {
return "Movie";
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_MAIN_CHARACTER_TYPE = 4,
VT_MAIN_CHARACTER = 6,

View File

@@ -94,6 +94,13 @@ Attacker.prototype.mutate_sword_attack_damage = function(value) {
return true;
};
/**
* @returns {string}
*/
Attacker.getFullyQualifiedName = function() {
return 'Attacker';
}
/**
* @param {flatbuffers.Builder} builder
*/
@@ -171,6 +178,13 @@ Rapunzel.prototype.mutate_hair_length = function(value) {
return true;
};
/**
* @returns {string}
*/
Rapunzel.getFullyQualifiedName = function() {
return 'Rapunzel';
}
/**
* @param {flatbuffers.Builder} builder
* @param {number} hair_length
@@ -224,6 +238,13 @@ BookReader.prototype.mutate_books_read = function(value) {
return true;
};
/**
* @returns {string}
*/
BookReader.getFullyQualifiedName = function() {
return 'BookReader';
}
/**
* @param {flatbuffers.Builder} builder
* @param {number} books_read
@@ -348,6 +369,13 @@ Movie.prototype.charactersLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {string}
*/
Movie.getFullyQualifiedName = function() {
return 'Movie';
}
/**
* @param {flatbuffers.Builder} builder
*/

View File

@@ -107,6 +107,13 @@ mutate_sword_attack_damage(value:number):boolean {
return true;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'Attacker';
}
/**
* @param flatbuffers.Builder builder
*/
@@ -207,6 +214,13 @@ mutate_hair_length(value:number):boolean {
return true;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'Rapunzel';
}
/**
* @param flatbuffers.Builder builder
* @param number hair_length
@@ -289,6 +303,13 @@ mutate_books_read(value:number):boolean {
return true;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'BookReader';
}
/**
* @param flatbuffers.Builder builder
* @param number books_read
@@ -442,6 +463,13 @@ charactersLength():number {
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns string
*/
static getFullyQualifiedName():string {
return 'Movie';
}
/**
* @param flatbuffers.Builder builder
*/