[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
This commit is contained in:
Michael Le
2022-12-13 02:06:48 -05:00
committed by GitHub
parent c0797b22ae
commit e1a2f688e0
2 changed files with 15 additions and 9 deletions

View File

@@ -103,10 +103,10 @@ class GoGenerator : public BaseGenerator {
bool needs_imports = false; bool needs_imports = false;
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) { ++it) {
tracked_imported_namespaces_.clear(); if (!parser_.opts.one_file) {
needs_math_import_ = false; needs_imports = false;
needs_bytes_import_ = false; ResetImports();
needs_imports = false; }
std::string enumcode; std::string enumcode;
GenEnum(**it, &enumcode); GenEnum(**it, &enumcode);
if ((*it)->is_union && parser_.opts.generate_object_based_api) { 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(); for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) { it != parser_.structs_.vec.end(); ++it) {
tracked_imported_namespaces_.clear(); if (!parser_.opts.one_file) { ResetImports(); }
needs_math_import_ = false;
needs_bytes_import_ = false;
std::string declcode; std::string declcode;
GenStruct(**it, &declcode); GenStruct(**it, &declcode);
if (parser_.opts.one_file) { if (parser_.opts.one_file) {
@@ -915,6 +913,7 @@ class GoGenerator : public BaseGenerator {
code += "buf []byte) bool {\n"; code += "buf []byte) bool {\n";
code += "\tspan := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:])\n"; code += "\tspan := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:])\n";
code += "\tstart := flatbuffers.UOffsetT(0)\n"; code += "\tstart := flatbuffers.UOffsetT(0)\n";
if (IsString(field.value.type)) { code += "\tbKey := []byte(key)\n"; }
code += "\tfor span != 0 {\n"; code += "\tfor span != 0 {\n";
code += "\t\tmiddle := span / 2\n"; code += "\t\tmiddle := span / 2\n";
code += "\t\ttableOffset := flatbuffers.GetIndirectOffset(buf, "; code += "\t\ttableOffset := flatbuffers.GetIndirectOffset(buf, ";
@@ -924,7 +923,6 @@ class GoGenerator : public BaseGenerator {
code += "\t\tobj.Init(buf, tableOffset)\n"; code += "\t\tobj.Init(buf, tableOffset)\n";
if (IsString(field.value.type)) { if (IsString(field.value.type)) {
code += "\t\tbKey := []byte(key)\n";
needs_bytes_import_ = true; needs_bytes_import_ = true;
code += code +=
"\t\tcomp := bytes.Compare(obj." + namer_.Function(field.name) + "()"; "\t\tcomp := bytes.Compare(obj." + namer_.Function(field.name) + "()";
@@ -1462,6 +1460,7 @@ class GoGenerator : public BaseGenerator {
StructBuilderBody(struct_def, "", code_ptr); StructBuilderBody(struct_def, "", code_ptr);
EndBuilderBody(code_ptr); EndBuilderBody(code_ptr);
} }
// Begin by declaring namespace and imports. // Begin by declaring namespace and imports.
void BeginFile(const std::string &name_space_name, const bool needs_imports, void BeginFile(const std::string &name_space_name, const bool needs_imports,
const bool is_enum, std::string *code_ptr) { 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. // Save out the generated code for a Go Table type.
bool SaveType(const Definition &def, const std::string &classcode, bool SaveType(const Definition &def, const std::string &classcode,
const bool needs_imports, const bool is_enum) { const bool needs_imports, const bool is_enum) {

View File

@@ -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 { func (rcv *Monster) LookupByKey(key string, vectorLocation flatbuffers.UOffsetT, buf []byte) bool {
span := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:]) span := flatbuffers.GetUOffsetT(buf[vectorLocation - 4:])
start := flatbuffers.UOffsetT(0) start := flatbuffers.UOffsetT(0)
bKey := []byte(key)
for span != 0 { for span != 0 {
middle := span / 2 middle := span / 2
tableOffset := flatbuffers.GetIndirectOffset(buf, vectorLocation+ 4 * (start + middle)) tableOffset := flatbuffers.GetIndirectOffset(buf, vectorLocation+ 4 * (start + middle))
obj := &Monster{} obj := &Monster{}
obj.Init(buf, tableOffset) obj.Init(buf, tableOffset)
bKey := []byte(key)
comp := bytes.Compare(obj.Name(), bKey) comp := bytes.Compare(obj.Name(), bKey)
if comp > 0 { if comp > 0 {
span = middle span = middle