Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Arrays in Structs for Golang #8250

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ class GoGenerator : public BaseGenerator {
// a nested struct, prefix the name with the field name.
StructBuilderArgs(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else if (IsArray(field.value.type)) {
std::string &code = *code_ptr;
code += std::string(", ") + nameprefix;
code += namer_.Variable(field);
code += " []" + GenTypeGet(field.value.type.VectorType());
} else {
std::string &code = *code_ptr;
code += std::string(", ") + nameprefix;
Expand Down Expand Up @@ -653,6 +658,19 @@ class GoGenerator : public BaseGenerator {
if (IsStruct(field.value.type)) {
StructBuilderBody(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else if (IsArray(field.value.type)) {
code += "\tfor i := " + std::to_string(field.value.type.fixed_length) +
"; i >= 0; i-- {\n";
code += "\t\tif len(" + namer_.Variable(field) + ") < i+1 {\n";
code += "\t\t\tbuilder.Place" +
namer_.Method(GenTypeBasic(field.value.type.VectorType())) +
"(0)\n";
code += "\t\t} else {\n";
code += "\t\t\tbuilder.Place" +
namer_.Method(GenTypeBasic(field.value.type.VectorType())) +
"(data[i])\n";
code += "\t\t}\n";
code += "\t}\n";
} else {
code += "\tbuilder.Prepend" + GenMethod(field) + "(";
code += CastToBaseType(field.value.type,
Expand Down Expand Up @@ -782,6 +800,40 @@ class GoGenerator : public BaseGenerator {
}
break;
}
case BASE_TYPE_ARRAY: {
std::string &code = *code_ptr;
auto vectortype = field.value.type.VectorType();
if (vectortype.base_type == BASE_TYPE_STRUCT) {
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
// TODO(michaeltle): Support querying fixed struct by key.
// Currently, we only support keyed tables.
if (!vectortype.struct_def->fixed &&
vectortype.struct_def->has_key) {
GetMemberOfVectorOfStructByKey(struct_def, field, code_ptr);
}
} else {
GenReceiver(struct_def, code_ptr);
code += " " + namer_.Function(field);
code += "(j int) " + GenTypeGet(vectortype) + " ";
code += OffsetPrefix(field);
code += "\t\ta := rcv._tab.Vector(o)\n";
code += "\t\treturn " +
CastToEnum(field.value.type,
GenGetter(field.value.type) +
"(a + flatbuffers.UOffsetT(j*" +
NumToString(InlineSize(vectortype)) + "))");
code += "\n\t}\n";
if (IsString(vectortype)) {
code += "\treturn nil\n";
} else if (vectortype.base_type == BASE_TYPE_BOOL) {
code += "\treturn false\n";
} else {
code += "\treturn 0\n";
}
code += "}\n\n";
}
break;
}
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: FLATBUFFERS_ASSERT(0);
}
Expand Down Expand Up @@ -1387,6 +1439,7 @@ class GoGenerator : public BaseGenerator {
switch (type.base_type) {
case BASE_TYPE_STRING: return "rcv._tab.ByteVector";
case BASE_TYPE_UNION: return "rcv._tab.Union";
case BASE_TYPE_ARRAY:
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
default: return "rcv._tab.Get" + namer_.Function(GenTypeBasic(type));
}
Expand Down Expand Up @@ -1505,6 +1558,8 @@ class GoGenerator : public BaseGenerator {
} else if (type.base_type == BASE_TYPE_UNION) {
return "*" +
WrapInNameSpaceAndTrack(type.enum_def, NativeName(*type.enum_def));
} else if (IsArray(type)) {
return "[]" + NativeType(type.VectorType());
}
FLATBUFFERS_ASSERT(0);
return std::string();
Expand Down
3 changes: 2 additions & 1 deletion src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,8 @@ bool Parser::SupportsAdvancedArrayFeatures() const {
return (opts.lang_to_generate &
~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava |
IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson |
IDLOptions::kBinary | IDLOptions::kRust | IDLOptions::kTs)) == 0;
IDLOptions::kBinary | IDLOptions::kRust | IDLOptions::kTs |
IDLOptions::kGo)) == 0;
}

bool Parser::Supports64BitOffsets() const {
Expand Down
101 changes: 101 additions & 0 deletions tests/array_in_struct_test/go/MyGame/Example/ArrayStruct.go

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

77 changes: 77 additions & 0 deletions tests/array_in_struct_test/go/MyGame/Example/ArrayTable.go

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

82 changes: 82 additions & 0 deletions tests/array_in_struct_test/go/MyGame/Example/NestedStruct.go

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

32 changes: 32 additions & 0 deletions tests/array_in_struct_test/go/MyGame/Example/TestEnum.go

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