Skip to content

Commit

Permalink
Go: make generated code more compliant to "go fmt" (#7907)
Browse files Browse the repository at this point in the history
Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
jdemeyer and dbaileychess authored Apr 26, 2023
1 parent 63495b9 commit 6c8a8d0
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 79 deletions.
13 changes: 10 additions & 3 deletions examples/go-echo/hero/Warrior.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions examples/go-echo/net/Request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions examples/go-echo/net/Response.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions tests/MyGame/Example/Ability.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/MyGame/Example/Any.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/MyGame/Example/AnyAmbiguousAliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/MyGame/Example/AnyUniqueAliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6c8a8d0

Please sign in to comment.