From e1a2f688e0d12385fd977cb45d58453d88d7c0e2 Mon Sep 17 00:00:00 2001 From: Michael Le Date: Tue, 13 Dec 2022 02:06:48 -0500 Subject: [PATCH] [Go] Fix bug where `bytes` wasn't being imported when using --gen-onefile flag (#7706) * Fix bug one file import bug * Create reset import function and add braces --- src/idl_gen_go.cpp | 22 ++++++++++++++-------- tests/MyGame/Example/Monster.go | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 650450fb6..0a04f19c4 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -103,10 +103,10 @@ class GoGenerator : public BaseGenerator { bool needs_imports = false; for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { - tracked_imported_namespaces_.clear(); - needs_math_import_ = false; - needs_bytes_import_ = false; - needs_imports = false; + if (!parser_.opts.one_file) { + needs_imports = false; + ResetImports(); + } std::string enumcode; GenEnum(**it, &enumcode); if ((*it)->is_union && parser_.opts.generate_object_based_api) { @@ -124,9 +124,7 @@ class GoGenerator : public BaseGenerator { for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); ++it) { - tracked_imported_namespaces_.clear(); - needs_math_import_ = false; - needs_bytes_import_ = false; + if (!parser_.opts.one_file) { ResetImports(); } std::string declcode; GenStruct(**it, &declcode); if (parser_.opts.one_file) { @@ -915,6 +913,7 @@ class GoGenerator : public BaseGenerator { code += "buf []byte) bool {\n"; code += "\tspan := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:])\n"; code += "\tstart := flatbuffers.UOffsetT(0)\n"; + if (IsString(field.value.type)) { code += "\tbKey := []byte(key)\n"; } code += "\tfor span != 0 {\n"; code += "\t\tmiddle := span / 2\n"; code += "\t\ttableOffset := flatbuffers.GetIndirectOffset(buf, "; @@ -924,7 +923,6 @@ class GoGenerator : public BaseGenerator { code += "\t\tobj.Init(buf, tableOffset)\n"; if (IsString(field.value.type)) { - code += "\t\tbKey := []byte(key)\n"; needs_bytes_import_ = true; code += "\t\tcomp := bytes.Compare(obj." + namer_.Function(field.name) + "()"; @@ -1462,6 +1460,7 @@ class GoGenerator : public BaseGenerator { StructBuilderBody(struct_def, "", code_ptr); EndBuilderBody(code_ptr); } + // Begin by declaring namespace and imports. void BeginFile(const std::string &name_space_name, const bool needs_imports, const bool is_enum, std::string *code_ptr) { @@ -1503,6 +1502,13 @@ class GoGenerator : public BaseGenerator { } } + // Resets the needed imports before generating a new file. + void ResetImports() { + tracked_imported_namespaces_.clear(); + needs_bytes_import_ = false; + needs_math_import_ = false; + } + // Save out the generated code for a Go Table type. bool SaveType(const Definition &def, const std::string &classcode, const bool needs_imports, const bool is_enum) { diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 6f8fae39b..0717aa676 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -579,12 +579,12 @@ func MonsterKeyCompare(o1, o2 flatbuffers.UOffsetT, buf []byte) bool { func (rcv *Monster) LookupByKey(key string, vectorLocation flatbuffers.UOffsetT, buf []byte) bool { span := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:]) start := flatbuffers.UOffsetT(0) + bKey := []byte(key) for span != 0 { middle := span / 2 tableOffset := flatbuffers.GetIndirectOffset(buf, vectorLocation+ 4 * (start + middle)) obj := &Monster{} obj.Init(buf, tableOffset) - bKey := []byte(key) comp := bytes.Compare(obj.Name(), bKey) if comp > 0 { span = middle