Skip to content

Commit

Permalink
[GO] compiles to much files (google#8118)
Browse files Browse the repository at this point in the history
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* [TS] Fix reserved words as arguments (google#6955)

* [TS] Fix generation of reserved words in object api (google#7106)

* [TS] Fix generation of object api

* [TS] Fix MakeCamel -> ConvertCase

* [C#] Fix collision of field name and type name

* [TS] Add test for struct of struct of struct

* Update generated files

* Add missing files

* [TS] Fix query of null/undefined fields in object api

* Generate only files for comiled fbs (not for dependend ones)

---------

Co-authored-by: Derek Bailey <[email protected]>
  • Loading branch information
2 people authored and candysonya committed Jan 8, 2024
1 parent 974906b commit f54d867
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,54 +102,72 @@ class GoGenerator : public BaseGenerator {

bool generate() {
std::string one_file_code;

if (!generateEnums(&one_file_code)) return false;
if (!generateStructs(&one_file_code)) return false;

if (parser_.opts.one_file) {
std::string code = "";
const bool is_enum = !parser_.enums_.vec.empty();
BeginFile(LastNamespacePart(go_namespace_), true, is_enum, &code);
code += one_file_code;
const std::string filename =
GeneratedFileName(path_, file_name_, parser_.opts);
return SaveFile(filename.c_str(), code, false);
}

return true;
}

private:
bool generateEnums(std::string *one_file_code) {
bool needs_imports = false;
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
if (!parser_.opts.one_file) {
needs_imports = false;
ResetImports();
}
auto &enum_def = **it;
std::string enumcode;
GenEnum(**it, &enumcode);
if ((*it)->is_union && parser_.opts.generate_object_based_api) {
GenNativeUnion(**it, &enumcode);
GenNativeUnionPack(**it, &enumcode);
GenNativeUnionUnPack(**it, &enumcode);
GenEnum(enum_def, &enumcode);
if (enum_def.is_union && parser_.opts.generate_object_based_api) {
GenNativeUnionCreator(enum_def, &enumcode);
needs_imports = true;
}
if (parser_.opts.one_file) {
one_file_code += enumcode;
*one_file_code += enumcode;
} else {
if (!SaveType(**it, enumcode, needs_imports, true)) return false;
if (!SaveType(enum_def, enumcode, needs_imports, true)) return false;
}
}
return true;
}

void GenNativeUnionCreator(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;

GenNativeUnion(enum_def, code_ptr);
GenNativeUnionPack(enum_def, code_ptr);
GenNativeUnionUnPack(enum_def, code_ptr);
}

bool generateStructs(std::string *one_file_code) {
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
if (!parser_.opts.one_file) { ResetImports(); }
std::string declcode;
GenStruct(**it, &declcode);
auto &struct_def = **it;
GenStruct(struct_def, &declcode);
if (parser_.opts.one_file) {
one_file_code += declcode;
*one_file_code += declcode;
} else {
if (!SaveType(**it, declcode, true, false)) return false;
if (!SaveType(struct_def, declcode, true, false)) return false;
}
}

if (parser_.opts.one_file) {
std::string code = "";
const bool is_enum = !parser_.enums_.vec.empty();
BeginFile(LastNamespacePart(go_namespace_), true, is_enum, &code);
code += one_file_code;
const std::string filename =
GeneratedFileName(path_, file_name_, parser_.opts);
return SaveFile(filename.c_str(), code, false);
}

return true;
}

private:
Namespace go_namespace_;
Namespace *cur_name_space_;
const IdlNamer namer_;
Expand All @@ -176,7 +194,8 @@ class GoGenerator : public BaseGenerator {

code += "type " + namer_.Type(struct_def) + " struct {\n\t";

// _ is reserved in flatbuffers field names, so no chance of name conflict:
// _ is reserved in flatbuffers field names, so no chance of name
// conflict:
code += "_tab ";
code += struct_def.fixed ? "flatbuffers.Struct" : "flatbuffers.Table";
code += "\n}\n\n";
Expand Down Expand Up @@ -1012,6 +1031,8 @@ class GoGenerator : public BaseGenerator {
}

void GenNativeUnion(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;

std::string &code = *code_ptr;
code += "type " + NativeName(enum_def) + " struct {\n";
code += "\tType " + namer_.Type(enum_def) + "\n";
Expand All @@ -1020,6 +1041,8 @@ class GoGenerator : public BaseGenerator {
}

void GenNativeUnionPack(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;

std::string &code = *code_ptr;
code += "func (t *" + NativeName(enum_def) +
") Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {\n";
Expand All @@ -1040,6 +1063,8 @@ class GoGenerator : public BaseGenerator {
}

void GenNativeUnionUnPack(const EnumDef &enum_def, std::string *code_ptr) {
if (enum_def.generated) return;

std::string &code = *code_ptr;

code += "func (rcv " + namer_.Type(enum_def) +
Expand Down

0 comments on commit f54d867

Please sign in to comment.