Partial support for --ts-flat-files and --gen-all (#7446)

* Partial support for --ts-flat-files and --gen-all

* Add generated code changes

* remove some debugging code left over

* missed grpc files
This commit is contained in:
Derek Bailey
2022-08-15 16:11:45 -07:00
committed by GitHub
parent a3508f36d5
commit a66de58af9
38 changed files with 241 additions and 99 deletions

View File

@@ -643,10 +643,6 @@ int FlatCompiler::Compile(int argc, const char **argv) {
"well.");
}
if (opts.ts_flat_file && opts.generate_all) {
Error("Combining --ts-flat-file and --gen-all is not supported.");
}
flatbuffers::Parser conform_parser;
if (!conform_to_schema.empty()) {
std::string contents;

View File

@@ -38,7 +38,7 @@ struct ImportDefinition {
};
enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 };
}
} // namespace
namespace ts {
// Iterate through all definitions we haven't generate code for (enums, structs,
@@ -111,10 +111,6 @@ class TsGenerator : public BaseGenerator {
for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
}
bool generate() {
if (parser_.opts.ts_flat_file && parser_.opts.generate_all) {
// Not implemented; warning message should have been emitted by flatc.
return false;
}
generateEnums();
generateStructs();
generateEntry();
@@ -227,11 +223,15 @@ class TsGenerator : public BaseGenerator {
// Generate code for a single entry point module.
void generateEntry() {
std::string code;
std::string code =
"// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
if (parser_.opts.ts_flat_file) {
if (import_flatbuffers_lib_) {
code += "import * as flatbuffers from 'flatbuffers';\n";
code += "\n";
}
// Only include import statements when not generating all.
if (!parser_.opts.generate_all) {
for (const auto &it : flat_file_import_declarations_) {
// Note that we do end up generating an import for ourselves, which
// should generally be harmless.
@@ -259,7 +259,9 @@ class TsGenerator : public BaseGenerator {
code.resize(code.size() - 2);
code += "} from '" + include_name + "';\n";
}
code += "\n\n";
code += "\n";
}
code += flat_file_;
const std::string filename =
GeneratedFileName(path_, file_name_, parser_.opts);
@@ -305,7 +307,9 @@ class TsGenerator : public BaseGenerator {
if (reverse) return; // FIXME.
std::string &code = *code_ptr;
GenDocComment(enum_def.doc_comment, code_ptr);
code += "export enum " + EscapeKeyword(enum_def.name) + "{\n";
code += "export enum ";
// TODO(7445): figure out if the export needs a namespace for ts-flat-files
code += EscapeKeyword(enum_def.name) + " {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
if (!ev.doc_comment.empty()) {
@@ -659,18 +663,13 @@ class TsGenerator : public BaseGenerator {
// above, but since we force all non-self-imports to use namespace-based
// names in flat file generation, it's fine).
if (dependent.file == dependency.file) {
// Should already be caught elsewhere, but if we ever try to get flat
// file generation and --gen-all working concurrently, then we'll need
// to update this import logic.
FLATBUFFERS_ASSERT(!parser_.opts.generate_all);
long_import_name = import_name;
} else {
long_import_name = ns + import_name;
std::string file = dependency.declaration_file == nullptr
? dependency.file
: dependency.declaration_file->substr(2);
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
std::string file =
RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
dependency.file)
// Strip the leading //
.substr(2);
flat_file_import_declarations_[file][import_name] = long_import_name;
if (parser_.opts.generate_object_based_api) {
@@ -753,18 +752,13 @@ class TsGenerator : public BaseGenerator {
// above, but since we force all non-self-imports to use namespace-based
// names in flat file generation, it's fine).
if (dependent.file == dependency.file) {
// Should already be caught elsewhere, but if we ever try to get flat
// file generation and --gen-all working concurrently, then we'll need
// to update this import logic.
FLATBUFFERS_ASSERT(!parser_.opts.generate_all);
long_import_name = import_name;
} else {
long_import_name = ns + import_name;
std::string file = dependency.declaration_file == nullptr
? dependency.file
: dependency.declaration_file->substr(2);
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
std::string file =
RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
dependency.file)
// Strip the leading //
.substr(2);
flat_file_import_declarations_[file][import_name] = long_import_name;
}
@@ -1347,7 +1341,9 @@ class TsGenerator : public BaseGenerator {
// Emit constructor
object_name = EscapeKeyword(struct_def.name);
GenDocComment(struct_def.doc_comment, code_ptr);
code += "export class " + object_name;
code += "export class ";
// TODO(7445): figure out if the export needs a namespace for ts-flat-files
code += object_name;
code += " {\n";
code += " bb: flatbuffers.ByteBuffer|null = null;\n";
code += " bb_pos = 0;\n";

View File

@@ -1,3 +1,6 @@
table Baz {
a:int;
enum Baz : short {
None = 0,
Red,
Green,
Blue,
}

View File

@@ -77,8 +77,22 @@ def assert_file_contains(file, needles):
return file
def assert_file_and_contents(file, needle, path=script_path, unlink=True):
def assert_file_doesnt_contains(file, needles):
with open(file) as file:
contents = file.read()
for needle in [needles] if isinstance(needles, str) else needles:
assert needle not in contents, (
"Found unexpected '" + needle + "' in file: " + str(file)
)
return file
def assert_file_and_contents(
file, needle, doesnt_contain=None, path=script_path, unlink=True
):
assert_file_contains(assert_file_exists(file, path), needle)
if doesnt_contain:
assert_file_doesnt_contains(assert_file_exists(file, path), doesnt_contain)
if unlink:
Path(path, file).unlink()
@@ -102,7 +116,7 @@ def run_all(*modules):
module_passing = module_passing + 1
except Exception as e:
print(" [FAILED]: " + str(e))
failingmodule_failing = failingmodule_failing + 1
module_failing = module_failing + 1
print(
"{0}: {1} of {2} passsed".format(
module.__name__, module_passing, module_passing + module_failing

View File

@@ -14,6 +14,7 @@
from flatc_test import *
class TsTests():
def Base(self):
@@ -24,7 +25,10 @@ class TsTests():
# include, bar.
assert_file_and_contents(
"foo_generated.ts",
["export { Bar } from './bar';", "export { Foo } from './foo';"],
[
"export { Bar } from './bar';",
"export { Foo } from './foo';",
],
)
# Foo should be generated in place and exports the Foo table.
@@ -33,6 +37,26 @@ class TsTests():
# Included files, like bar, should not be generated.
assert_file_doesnt_exists("bar.ts")
def BaseMultipleFiles(self):
# Generate both foo and bar with no extra arguments
flatc(["--ts", "foo.fbs", "bar/bar.fbs"])
# Should generate the module that exports both foo and its direct
# include, bar.
assert_file_and_contents(
"foo_generated.ts",
[
"export { Bar } from './bar';",
"export { Foo } from './foo';",
],
)
# Foo should be generated in place and exports the Foo table.
assert_file_and_contents("foo.ts", "export class Foo {")
# Bar should also be generatd in place and exports the Bar table.
assert_file_and_contents("bar.ts", "export class Bar {")
def BaseWithNamespace(self):
# Generate foo with namespacing, with no extra arguments
flatc(["--ts", "foo_with_ns.fbs"])
@@ -41,29 +65,132 @@ class TsTests():
# directory and its direct include, bar.
assert_file_and_contents(
"foo_with_ns_generated.ts",
["export { Bar } from './bar';", "export { Foo } from './something/foo';"],
[
"export { Bar } from './bar';",
"export { Foo } from './something/foo';",
],
)
# Foo should be placed in the namespaced directory. It should export
# Foo, and the import of Bar should be relative to its location.
assert_file_and_contents(
"something/foo.ts",
["export class Foo {", "import { Bar } from '../bar';"],
[
"export class Foo {",
"import { Bar } from '../bar';",
],
)
# Included files, like bar, should not be generated.
assert_file_doesnt_exists("bar.ts")
def GenAll(self):
# Generate foo with generate all options
flatc(["--ts", "--gen-all", "foo.fbs"])
# Should generate a single file that exports all the generated types.
assert_file_and_contents(
"foo_generated.ts",
[
"export { Bar } from './bar'",
"export { Baz } from './baz'",
"export { Foo } from './foo'",
],
)
# Foo should be generated with an import to Bar and an export of itself.
assert_file_and_contents(
"foo.ts",
[
"import { Bar } from './bar';",
"export class Foo {",
],
)
# Bar should be generated with an import to Baz and an export of itself.
assert_file_and_contents(
"bar.ts",
[
"import { Baz } from './baz';",
"export class Bar {",
],
)
# Baz should be generated with an export of itself.
assert_file_and_contents(
"baz.ts",
[
"export enum Baz {",
],
)
def FlatFiles(self):
# Generate just foo the flat files option
# Generate just foo with the flat files option
flatc(["--ts", "--ts-flat-files", "foo.fbs"])
# Should generate a single file that imports bar as a single file, and]
# Should generate a single file that imports bar as a single file, and
# exports the Foo table.
assert_file_and_contents(
"foo_generated.ts",
["import {Bar as Bar} from './bar_generated';", "export class Foo {"],
[
"import {Bar as Bar} from './bar_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"])
# Should generate a single foo file that imports bar as a single file,
# and exports the Foo table.
assert_file_and_contents(
"foo_generated.ts",
[
"import {Bar as Bar} from './bar_generated';",
"export class Foo {",
],
)
# Should generate a single bar file that imports bar as a single file,
# and exports the Bar table.
assert_file_and_contents(
"bar_generated.ts",
[
"import {Baz as Baz} from './baz_generated';",
"export class Bar {",
],
)
# The types Foo and Bar should not be generated in their own files
assert_file_doesnt_exists("foo.ts")
assert_file_doesnt_exists("bar.ts")
def FlatFilesGenAll(self):
# Generate foo with all of its dependents with the flat files option
flatc(["--ts", "--ts-flat-files", "--gen-all", "foo.fbs"])
# Should generate a single foo file
assert_file_and_contents(
"foo_generated.ts",
# Should export each of the types within the single file
[
"export class Foo {",
"export class Bar {",
"export enum Baz {",
],
# 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,3 +1,5 @@
// 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 { Ability, AbilityT } from './my-game/example/ability';

View File

@@ -1,2 +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';

View File

@@ -1,3 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { Attacker, AttackerT } from './attacker';
export { BookReader, BookReaderT } from './book-reader';
export { Character, unionToCharacter, unionListToCharacter } from './character';