diff options
author | Jeroen Demeyer <jeroen.demeyer@be-mobile.com> | 2023-04-26 07:19:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-26 05:19:07 +0000 |
commit | 6c8a8d054d27b5149d2daf90609a47214e55d633 (patch) | |
tree | 02c1ba821a1c1eb2ff96372ec5f1e06a14bb15db /src | |
parent | 63495b935a06cba8f3b09e21fb79f08d50f06648 (diff) | |
download | flatbuffers-6c8a8d054d27b5149d2daf90609a47214e55d633.tar.gz flatbuffers-6c8a8d054d27b5149d2daf90609a47214e55d633.tar.bz2 flatbuffers-6c8a8d054d27b5149d2daf90609a47214e55d633.zip |
Go: make generated code more compliant to "go fmt" (#7907)
Co-authored-by: Derek Bailey <derekbailey@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/idl_gen_go.cpp | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index f2ffc3e7..0f2882b7 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -541,7 +541,7 @@ class GoGenerator : public BaseGenerator { GenReceiver(struct_def, code_ptr); code += " " + namer_.Field(field) + "ByKey"; code += "(obj *" + TypeName(field); - code += ", key " + NativeType(key_field.value.type) + ") bool" + + code += ", key " + NativeType(key_field.value.type) + ") bool " + OffsetPrefix(field); code += "\t\tx := rcv._tab.Vector(o)\n"; code += "\t\treturn "; @@ -920,8 +920,8 @@ class GoGenerator : public BaseGenerator { code += "o1, o2 flatbuffers.UOffsetT, buf []byte) bool {\n"; code += "\tobj1 := &" + namer_.Type(struct_def) + "{}\n"; code += "\tobj2 := &" + namer_.Type(struct_def) + "{}\n"; - code += "\tobj1.Init(buf, flatbuffers.UOffsetT(len(buf)) - o1)\n"; - code += "\tobj2.Init(buf, flatbuffers.UOffsetT(len(buf)) - o2)\n"; + code += "\tobj1.Init(buf, flatbuffers.UOffsetT(len(buf))-o1)\n"; + code += "\tobj2.Init(buf, flatbuffers.UOffsetT(len(buf))-o2)\n"; if (IsString(field.value.type)) { code += "\treturn string(obj1." + namer_.Function(field.name) + "()) < "; code += "string(obj2." + namer_.Function(field.name) + "())\n"; @@ -943,13 +943,13 @@ class GoGenerator : public BaseGenerator { code += "key " + NativeType(field.value.type) + ", "; code += "vectorLocation flatbuffers.UOffsetT, "; 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"; 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, "; - code += "vectorLocation+ 4 * (start + middle))\n"; + code += "vectorLocation+4*(start+middle))\n"; code += "\t\tobj := &" + namer_.Type(struct_def) + "{}\n"; code += "\t\tobj.Init(buf, tableOffset)\n"; @@ -1060,8 +1060,8 @@ class GoGenerator : public BaseGenerator { code += "\t\treturn &" + WrapInNameSpaceAndTrack(&enum_def, NativeName(enum_def)) + - "{ Type: " + namer_.EnumVariant(enum_def, ev) + - ", Value: x.UnPack() }\n"; + "{Type: " + namer_.EnumVariant(enum_def, ev) + + ", Value: x.UnPack()}\n"; } code += "\t}\n"; code += "\treturn nil\n"; @@ -1074,7 +1074,7 @@ class GoGenerator : public BaseGenerator { code += "func (t *" + NativeName(struct_def) + ") Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {\n"; - code += "\tif t == nil { return 0 }\n"; + code += "\tif t == nil {\n\t\treturn 0\n\t}\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { const FieldDef &field = **it; @@ -1144,8 +1144,7 @@ class GoGenerator : public BaseGenerator { if (field.value.type.struct_def->fixed) continue; code += "\t" + offset + " := t." + field_field + ".Pack(builder)\n"; } else if (field.value.type.base_type == BASE_TYPE_UNION) { - code += "\t" + offset + " := t." + field_field + ".Pack(builder)\n"; - code += "\t\n"; + code += "\t" + offset + " := t." + field_field + ".Pack(builder)\n\n"; } else { FLATBUFFERS_ASSERT(0); } @@ -1261,7 +1260,7 @@ class GoGenerator : public BaseGenerator { code += "func (rcv *" + struct_type + ") UnPack() *" + NativeName(struct_def) + " {\n"; - code += "\tif rcv == nil { return nil }\n"; + code += "\tif rcv == nil {\n\t\treturn nil\n\t}\n"; code += "\tt := &" + NativeName(struct_def) + "{}\n"; code += "\trcv.UnPackTo(t)\n"; code += "\treturn t\n"; @@ -1273,7 +1272,7 @@ class GoGenerator : public BaseGenerator { code += "func (t *" + NativeName(struct_def) + ") Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {\n"; - code += "\tif t == nil { return 0 }\n"; + code += "\tif t == nil {\n\t\treturn 0\n\t}\n"; code += "\treturn Create" + namer_.Type(struct_def) + "(builder"; StructPackArgs(struct_def, "", code_ptr); code += ")\n"; @@ -1317,7 +1316,7 @@ class GoGenerator : public BaseGenerator { code += "func (rcv *" + namer_.Type(struct_def) + ") UnPack() *" + NativeName(struct_def) + " {\n"; - code += "\tif rcv == nil { return nil }\n"; + code += "\tif rcv == nil {\n\t\treturn nil\n\t}\n"; code += "\tt := &" + NativeName(struct_def) + "{}\n"; code += "\trcv.UnPackTo(t)\n"; code += "\treturn t\n"; @@ -1505,15 +1504,17 @@ class GoGenerator : public BaseGenerator { code += "package " + name_space_name + "\n\n"; if (needs_imports) { code += "import (\n"; + // standard imports, in alphabetical order for go fmt if (needs_bytes_import_) code += "\t\"bytes\"\n"; - // math is needed to support non-finite scalar default values. - if (needs_math_import_) { code += "\t\"math\"\n"; } - if (is_enum) { code += "\t\"strconv\"\n"; } if (!parser_.opts.go_import.empty()) { code += "\tflatbuffers \"" + parser_.opts.go_import + "\"\n"; } else { code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; } + // math is needed to support non-finite scalar default values. + if (needs_math_import_) { code += "\t\"math\"\n"; } + if (is_enum) { code += "\t\"strconv\"\n"; } + if (tracked_imported_namespaces_.size() > 0) { code += "\n"; for (auto it = tracked_imported_namespaces_.begin(); |