[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -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) {

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 {
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