mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 20:58:56 +00:00
Add flag to make short names in JS/TS. (#5152)
* Add flag to make short names in JS/TS. * Synced all missing docs in Compiler.md <-> flatc.cpp. * Review changes.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
78fdce28c7
commit
e1defaae5e
@@ -72,6 +72,12 @@ Additional options:
|
|||||||
in quotes, no trailing commas in tables/vectors). By default, no quotes are
|
in quotes, no trailing commas in tables/vectors). By default, no quotes are
|
||||||
required/generated, and trailing commas are allowed.
|
required/generated, and trailing commas are allowed.
|
||||||
|
|
||||||
|
- `--allow-non-utf8` : Pass non-UTF-8 input through parser and emit nonstandard
|
||||||
|
\x escapes in JSON. (Default is to raise parse error on non-UTF-8 input.)
|
||||||
|
|
||||||
|
- `--natural-utf8` : Output strings with UTF-8 as human-readable strings.
|
||||||
|
By default, UTF-8 characters are printed as \uXXXX escapes."
|
||||||
|
|
||||||
- `--defaults-json` : Output fields whose value is equal to the default value
|
- `--defaults-json` : Output fields whose value is equal to the default value
|
||||||
when writing JSON text.
|
when writing JSON text.
|
||||||
|
|
||||||
@@ -91,23 +97,36 @@ Additional options:
|
|||||||
- `--gen-mutable` : Generate additional non-const accessors for mutating
|
- `--gen-mutable` : Generate additional non-const accessors for mutating
|
||||||
FlatBuffers in-place.
|
FlatBuffers in-place.
|
||||||
|
|
||||||
`--gen-object-api` : Generate an additional object-based API. This API is
|
- `--gen-onefile` : Generate single output file for C# and Go.
|
||||||
|
|
||||||
|
- `--gen-name-strings` : Generate type name functions for C++.
|
||||||
|
|
||||||
|
- `--gen-object-api` : Generate an additional object-based API. This API is
|
||||||
more convenient for object construction and mutation than the base API,
|
more convenient for object construction and mutation than the base API,
|
||||||
at the cost of efficiency (object allocation). Recommended only to be used
|
at the cost of efficiency (object allocation). Recommended only to be used
|
||||||
if other options are insufficient.
|
if other options are insufficient.
|
||||||
|
|
||||||
- `--gen-compare` : Generate operator== for object-based API types.
|
- `--gen-compare` : Generate operator== for object-based API types.
|
||||||
|
|
||||||
- `--gen-onefile` : Generate single output file (useful for C#)
|
- `--gen-nullable` : Add Clang _Nullable for C++ pointer. or @Nullable for Java.
|
||||||
|
|
||||||
- `--gen-all`: Generate not just code for the current schema files, but
|
- `--gen-generated` : Add @Generated annotation for Java.
|
||||||
|
|
||||||
|
- `--gen-all` : Generate not just code for the current schema files, but
|
||||||
for all files it includes as well. If the language uses a single file for
|
for all files it includes as well. If the language uses a single file for
|
||||||
output (by default the case for C++ and JS), all code will end up in
|
output (by default the case for C++ and JS), all code will end up in
|
||||||
this one file.
|
this one file.
|
||||||
|
|
||||||
- `--gen-generated`: Add @Generated annotation for Java
|
- `--cpp-ptr-type T` : Set object API pointer type (default std::unique_ptr)
|
||||||
|
|
||||||
- `--no-js-exports` : Removes Node.js style export lines (useful for JS)
|
- `--cpp-str-type T` : Set object API string type (default std::string)
|
||||||
|
- T::c_str() and T::length() must be supported.
|
||||||
|
|
||||||
|
- `--object-prefix` : Customise class prefix for C++ object-based API.
|
||||||
|
|
||||||
|
- `--object-suffix` : Customise class suffix for C++ object-based API.
|
||||||
|
|
||||||
|
- `--no-js-exports` : Removes Node.js style export lines (useful for JS)
|
||||||
|
|
||||||
- `--goog-js-export` : Uses goog.exportsSymbol and goog.exportsProperty
|
- `--goog-js-export` : Uses goog.exportsSymbol and goog.exportsProperty
|
||||||
instead of Node.js style exporting. Needed for compatibility with the
|
instead of Node.js style exporting. Needed for compatibility with the
|
||||||
@@ -117,9 +136,16 @@ Additional options:
|
|||||||
instead of Node.js style exporting. Useful when integrating flatbuffers
|
instead of Node.js style exporting. Useful when integrating flatbuffers
|
||||||
with modern Javascript projects.
|
with modern Javascript projects.
|
||||||
|
|
||||||
|
- `--go-namespace` : Generate the overrided namespace in Golang.
|
||||||
|
|
||||||
|
- `--go-import` : Generate the overrided import for flatbuffers in Golang.
|
||||||
|
(default is "github.com/google/flatbuffers/go").
|
||||||
|
|
||||||
- `--raw-binary` : Allow binaries without a file_indentifier to be read.
|
- `--raw-binary` : Allow binaries without a file_indentifier to be read.
|
||||||
This may crash flatc given a mismatched schema.
|
This may crash flatc given a mismatched schema.
|
||||||
|
|
||||||
|
- `--size-prefixed` : Input binaries are size prefixed buffers.
|
||||||
|
|
||||||
- `--proto`: Expect input files to be .proto files (protocol buffers).
|
- `--proto`: Expect input files to be .proto files (protocol buffers).
|
||||||
Output the corresponding .fbs file.
|
Output the corresponding .fbs file.
|
||||||
Currently supports: `package`, `message`, `enum`, nested declarations,
|
Currently supports: `package`, `message`, `enum`, nested declarations,
|
||||||
@@ -127,6 +153,10 @@ Additional options:
|
|||||||
Does not support, but will skip without error: `option`, `service`,
|
Does not support, but will skip without error: `option`, `service`,
|
||||||
`extensions`, and most everything else.
|
`extensions`, and most everything else.
|
||||||
|
|
||||||
|
- `--oneof-union` : Translate .proto oneofs to flatbuffer unions.
|
||||||
|
|
||||||
|
- `--grpc` : Generate GRPC interfaces for the specified languages.
|
||||||
|
|
||||||
- `--schema`: Serialize schemas instead of JSON (use with -b). This will
|
- `--schema`: Serialize schemas instead of JSON (use with -b). This will
|
||||||
output a binary version of the specified schema that itself corresponds
|
output a binary version of the specified schema that itself corresponds
|
||||||
to the reflection/reflection.fbs schema. Loading this binary file is the
|
to the reflection/reflection.fbs schema. Loading this binary file is the
|
||||||
@@ -138,12 +168,22 @@ Additional options:
|
|||||||
an evolution of. Gives errors if not. Useful to check if schema
|
an evolution of. Gives errors if not. Useful to check if schema
|
||||||
modifications don't break schema evolution rules.
|
modifications don't break schema evolution rules.
|
||||||
|
|
||||||
|
- `--conform-includes PATH` : Include path for the schema given with
|
||||||
|
`--conform PATH`.
|
||||||
|
|
||||||
- `--include-prefix PATH` : Prefix this path to any generated include
|
- `--include-prefix PATH` : Prefix this path to any generated include
|
||||||
statements.
|
statements.
|
||||||
|
|
||||||
- `--keep-prefix` : Keep original prefix of schema include statement.
|
- `--keep-prefix` : Keep original prefix of schema include statement.
|
||||||
|
|
||||||
|
- `--no-fb-impor` : Don't include flatbuffers import statement for TypeScript.
|
||||||
|
|
||||||
|
- `--no-ts-reexpor` : Don't re-export imported dependencies for TypeScript.
|
||||||
|
|
||||||
|
- `--short-name` : Use short function names for JS and TypeScript.
|
||||||
|
|
||||||
- `--reflect-types` : Add minimal type reflection to code generation.
|
- `--reflect-types` : Add minimal type reflection to code generation.
|
||||||
|
|
||||||
- `--reflect-names` : Add minimal type/name reflection.
|
- `--reflect-names` : Add minimal type/name reflection.
|
||||||
|
|
||||||
- `--root-type T` : Select or override the default root_type.
|
- `--root-type T` : Select or override the default root_type.
|
||||||
|
|||||||
@@ -425,6 +425,7 @@ struct IDLOptions {
|
|||||||
std::string go_import;
|
std::string go_import;
|
||||||
std::string go_namespace;
|
std::string go_namespace;
|
||||||
bool reexport_ts_modules;
|
bool reexport_ts_modules;
|
||||||
|
bool js_ts_short_names;
|
||||||
bool protobuf_ascii_alike;
|
bool protobuf_ascii_alike;
|
||||||
bool size_prefixed;
|
bool size_prefixed;
|
||||||
std::string root_type;
|
std::string root_type;
|
||||||
@@ -496,6 +497,7 @@ struct IDLOptions {
|
|||||||
binary_schema_builtins(false),
|
binary_schema_builtins(false),
|
||||||
skip_flatbuffers_import(false),
|
skip_flatbuffers_import(false),
|
||||||
reexport_ts_modules(true),
|
reexport_ts_modules(true),
|
||||||
|
js_ts_short_names(false),
|
||||||
protobuf_ascii_alike(false),
|
protobuf_ascii_alike(false),
|
||||||
size_prefixed(false),
|
size_prefixed(false),
|
||||||
force_defaults(false),
|
force_defaults(false),
|
||||||
|
|||||||
@@ -98,38 +98,43 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
|||||||
" --gen-name-strings Generate type name functions for C++.\n"
|
" --gen-name-strings Generate type name functions for C++.\n"
|
||||||
" --gen-object-api Generate an additional object-based API.\n"
|
" --gen-object-api Generate an additional object-based API.\n"
|
||||||
" --gen-compare Generate operator== for object-based API types.\n"
|
" --gen-compare Generate operator== for object-based API types.\n"
|
||||||
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
|
|
||||||
" --cpp-str-type T Set object API string type (default std::string)\n"
|
|
||||||
" T::c_str() and T::length() must be supported\n"
|
|
||||||
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||||
" --gen-generated Add @Generated annotation for Java\n"
|
" --gen-generated Add @Generated annotation for Java\n"
|
||||||
|
" --gen-all Generate not just code for the current schema files,\n"
|
||||||
|
" but for all files it includes as well.\n"
|
||||||
|
" If the language uses a single file for output (by default\n"
|
||||||
|
" the case for C++ and JS), all code will end up in this one\n"
|
||||||
|
" file.\n"
|
||||||
|
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr).\n"
|
||||||
|
" --cpp-str-type T Set object API string type (default std::string).\n"
|
||||||
|
" T::c_str() and T::length() must be supported.\n"
|
||||||
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
||||||
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
||||||
" Default value is \"T\"\n"
|
" Default value is \"T\".\n"
|
||||||
" --no-js-exports Removes Node.js style export lines in JS.\n"
|
" --no-js-exports Removes Node.js style export lines in JS.\n"
|
||||||
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
|
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
|
||||||
" --es6-js-export Uses ECMAScript 6 export style lines in JS.\n"
|
" --es6-js-export Uses ECMAScript 6 export style lines in JS.\n"
|
||||||
" --go-namespace Generate the overrided namespace in Golang.\n"
|
" --go-namespace Generate the overrided namespace in Golang.\n"
|
||||||
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
|
" --go-import Generate the overrided import for flatbuffers in Golang\n"
|
||||||
" (default is \"github.com/google/flatbuffers/go\")\n"
|
" (default is \"github.com/google/flatbuffers/go\").\n"
|
||||||
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||||
" This may crash flatc given a mismatched schema.\n"
|
" This may crash flatc given a mismatched schema.\n"
|
||||||
" --size-prefixed Input binaries are size prefixed buffers.\n"
|
" --size-prefixed Input binaries are size prefixed buffers.\n"
|
||||||
" --proto Input is a .proto, translate to .fbs.\n"
|
" --proto Input is a .proto, translate to .fbs.\n"
|
||||||
" --oneof-union Translate .proto oneofs to flatbuffer unions.\n"
|
" --oneof-union Translate .proto oneofs to flatbuffer unions.\n"
|
||||||
" --grpc Generate GRPC interfaces for the specified languages\n"
|
" --grpc Generate GRPC interfaces for the specified languages.\n"
|
||||||
" --schema Serialize schemas instead of JSON (use with -b)\n"
|
" --schema Serialize schemas instead of JSON (use with -b).\n"
|
||||||
" --bfbs-comments Add doc comments to the binary schema files.\n"
|
" --bfbs-comments Add doc comments to the binary schema files.\n"
|
||||||
" --bfbs-builtins Add builtin attributes to the binary schema files.\n"
|
" --bfbs-builtins Add builtin attributes to the binary schema files.\n"
|
||||||
" --conform FILE Specify a schema the following schemas should be\n"
|
" --conform FILE Specify a schema the following schemas should be\n"
|
||||||
" an evolution of. Gives errors if not.\n"
|
" an evolution of. Gives errors if not.\n"
|
||||||
" --conform-includes Include path for the schema given with --conform\n"
|
" --conform-includes Include path for the schema given with --conform PATH\n"
|
||||||
" PATH \n"
|
|
||||||
" --include-prefix Prefix this path to any generated include statements.\n"
|
" --include-prefix Prefix this path to any generated include statements.\n"
|
||||||
" PATH\n"
|
" PATH\n"
|
||||||
" --keep-prefix Keep original prefix of schema include statement.\n"
|
" --keep-prefix Keep original prefix of schema include statement.\n"
|
||||||
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
|
" --no-fb-import Don't include flatbuffers import statement for TypeScript.\n"
|
||||||
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
|
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
|
||||||
|
" --short-names Use short function names for JS and TypeScript.\n"
|
||||||
" --reflect-types Add minimal type reflection to code generation.\n"
|
" --reflect-types Add minimal type reflection to code generation.\n"
|
||||||
" --reflect-names Add minimal type/name reflection.\n"
|
" --reflect-names Add minimal type/name reflection.\n"
|
||||||
" --root-type T Select or override the default root_type\n"
|
" --root-type T Select or override the default root_type\n"
|
||||||
@@ -289,6 +294,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
|||||||
opts.skip_flatbuffers_import = true;
|
opts.skip_flatbuffers_import = true;
|
||||||
} else if (arg == "--no-ts-reexport") {
|
} else if (arg == "--no-ts-reexport") {
|
||||||
opts.reexport_ts_modules = false;
|
opts.reexport_ts_modules = false;
|
||||||
|
} else if (arg == "--short-names") {
|
||||||
|
opts.js_ts_short_names = true;
|
||||||
} else if (arg == "--reflect-types") {
|
} else if (arg == "--reflect-types") {
|
||||||
opts.mini_reflect = IDLOptions::kTypes;
|
opts.mini_reflect = IDLOptions::kTypes;
|
||||||
} else if (arg == "--reflect-names") {
|
} else if (arg == "--reflect-names") {
|
||||||
|
|||||||
@@ -695,11 +695,11 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
GenTypeAnnotation(kParam, object_name + "=", "obj") +
|
GenTypeAnnotation(kParam, object_name + "=", "obj") +
|
||||||
GenTypeAnnotation(kReturns, object_name, "", false));
|
GenTypeAnnotation(kReturns, object_name, "", false));
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
code += "static getRootAs" + struct_def.name;
|
code += "static getRoot" + Verbose(struct_def,"As");
|
||||||
code += "(bb:flatbuffers.ByteBuffer, obj?:" + object_name +
|
code += "(bb:flatbuffers.ByteBuffer, obj?:" + object_name +
|
||||||
"):" + object_name + " {\n";
|
"):" + object_name + " {\n";
|
||||||
} else {
|
} else {
|
||||||
code += object_name + ".getRootAs" + struct_def.name;
|
code += object_name + ".getRoot" + Verbose(struct_def, "As");
|
||||||
code += " = function(bb, obj) {\n";
|
code += " = function(bb, obj) {\n";
|
||||||
}
|
}
|
||||||
code += " return (obj || new " + object_name;
|
code += " return (obj || new " + object_name;
|
||||||
@@ -1083,12 +1083,11 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
"", false));
|
"", false));
|
||||||
|
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
code +=
|
code += "static create" + Verbose(struct_def) + "(builder:flatbuffers.Builder";
|
||||||
"static create" + struct_def.name + "(builder:flatbuffers.Builder";
|
|
||||||
code += arguments + "):flatbuffers.Offset {\n";
|
code += arguments + "):flatbuffers.Offset {\n";
|
||||||
} else {
|
} else {
|
||||||
code +=
|
code += object_name + ".create" + Verbose(struct_def);
|
||||||
object_name + ".create" + struct_def.name + " = function(builder";
|
code += " = function(builder";
|
||||||
code += arguments + ") {\n";
|
code += arguments + ") {\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1100,10 +1099,9 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
"builder", false));
|
"builder", false));
|
||||||
|
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
code += "static start" + struct_def.name;
|
code += "static start" + Verbose(struct_def) + "(builder:flatbuffers.Builder) {\n";
|
||||||
code += "(builder:flatbuffers.Builder) {\n";
|
|
||||||
} else {
|
} else {
|
||||||
code += object_name + ".start" + struct_def.name;
|
code += object_name + ".start" + Verbose(struct_def);
|
||||||
code += " = function(builder) {\n";
|
code += " = function(builder) {\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1213,10 +1211,10 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
GenTypeAnnotation(kReturns, "flatbuffers.Offset", "", false));
|
GenTypeAnnotation(kReturns, "flatbuffers.Offset", "", false));
|
||||||
|
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
code += "static end" + struct_def.name;
|
code += "static end" + Verbose(struct_def);
|
||||||
code += "(builder:flatbuffers.Builder):flatbuffers.Offset {\n";
|
code += "(builder:flatbuffers.Builder):flatbuffers.Offset {\n";
|
||||||
} else {
|
} else {
|
||||||
code += object_name + ".end" + struct_def.name;
|
code += object_name + ".end" + Verbose(struct_def);
|
||||||
code += " = function(builder) {\n";
|
code += " = function(builder) {\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1242,11 +1240,10 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
false));
|
false));
|
||||||
|
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
code += "static finish" + struct_def.name + "Buffer";
|
code += "static finish" + Verbose(struct_def) + "Buffer";
|
||||||
code +=
|
code += "(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {\n";
|
||||||
"(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {\n";
|
|
||||||
} else {
|
} else {
|
||||||
code += object_name + ".finish" + struct_def.name + "Buffer";
|
code += object_name + ".finish" + Verbose(struct_def) + "Buffer";
|
||||||
code += " = function(builder, offset) {\n";
|
code += " = function(builder, offset) {\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1260,7 +1257,9 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
if (lang_.language == IDLOptions::kTs) {
|
if (lang_.language == IDLOptions::kTs) {
|
||||||
// Generate a convenient CreateX function
|
// Generate a convenient CreateX function
|
||||||
code += "static create" + struct_def.name + "(builder:flatbuffers.Builder";
|
code += "static create" + Verbose(struct_def);
|
||||||
|
code += "(builder:flatbuffers.Builder";
|
||||||
|
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
for (auto it = struct_def.fields.vec.begin();
|
||||||
it != struct_def.fields.vec.end(); ++it) {
|
it != struct_def.fields.vec.end(); ++it) {
|
||||||
const auto &field = **it;
|
const auto &field = **it;
|
||||||
@@ -1271,7 +1270,7 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
code += "):flatbuffers.Offset {\n";
|
code += "):flatbuffers.Offset {\n";
|
||||||
code += " " + struct_def.name + ".start" + struct_def.name + "(builder);\n";
|
code += " " + struct_def.name + ".start" + Verbose(struct_def) + "(builder);\n";
|
||||||
|
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
for (auto it = struct_def.fields.vec.begin();
|
||||||
it != struct_def.fields.vec.end(); ++it) {
|
it != struct_def.fields.vec.end(); ++it) {
|
||||||
@@ -1283,7 +1282,7 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
code += "builder, " + GetArgName(field) + ");\n";
|
code += "builder, " + GetArgName(field) + ");\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
code += " return " + struct_def.name + ".end" + struct_def.name + "(builder);\n";
|
code += " return " + struct_def.name + ".end" + Verbose(struct_def) + "(builder);\n";
|
||||||
code += "}\n";
|
code += "}\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1307,6 +1306,12 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
return argname;
|
return argname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Verbose(const StructDef &struct_def,
|
||||||
|
const char* prefix = "")
|
||||||
|
{
|
||||||
|
return parser_.opts.js_ts_short_names ? "" : prefix + struct_def.name;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace jsts
|
} // namespace jsts
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user