Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
faucct authored Apr 26, 2023
2 parents 63a0cee + d6d83c3 commit 87d6f84
Show file tree
Hide file tree
Showing 27 changed files with 358 additions and 97 deletions.
9 changes: 9 additions & 0 deletions CMake/BuildFlatBuffers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function(build_flatbuffers flatbuffers_schemas
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
elseif(TARGET flatbuffers::flatc)
set(FLATC_TARGET flatbuffers::flatc)
set(FLATC flatbuffers::flatc)
else()
set(FLATC_TARGET flatc)
set(FLATC flatc)
Expand Down Expand Up @@ -211,6 +214,9 @@ function(flatbuffers_generate_headers)
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
elseif(TARGET flatbuffers::flatc)
set(FLATC_TARGET flatbuffers::flatc)
set(FLATC flatbuffers::flatc)
else()
set(FLATC_TARGET flatc)
set(FLATC flatc)
Expand Down Expand Up @@ -382,6 +388,9 @@ function(flatbuffers_generate_binary_files)
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
elseif(TARGET flatbuffers::flatc)
set(FLATC_TARGET flatbuffers::flatc)
set(FLATC flatbuffers::flatc)
else()
set(FLATC_TARGET flatc)
set(FLATC flatc)
Expand Down
1 change: 1 addition & 0 deletions CMake/flatbuffers-config.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include("${CMAKE_CURRENT_LIST_DIR}/FlatBuffersTargets.cmake" OPTIONAL)
include("${CMAKE_CURRENT_LIST_DIR}/FlatcTargets.cmake" OPTIONAL)
include("${CMAKE_CURRENT_LIST_DIR}/FlatBuffersSharedTargets.cmake" OPTIONAL)
include("${CMAKE_CURRENT_LIST_DIR}/BuildFlatBuffers.cmake" OPTIONAL)
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.

20 changes: 20 additions & 0 deletions go/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ func GetSizePrefix(buf []byte, offset UOffsetT) uint32 {
func GetIndirectOffset(buf []byte, offset UOffsetT) UOffsetT {
return offset + GetUOffsetT(buf[offset:])
}

// GetBufferIdentifier returns the file identifier as string
func GetBufferIdentifier(buf []byte) string {
return string(buf[SizeUOffsetT:][:fileIdentifierLength])
}

// GetBufferIdentifier returns the file identifier as string for a size-prefixed buffer
func GetSizePrefixedBufferIdentifier(buf []byte) string {
return string(buf[SizeUOffsetT+sizePrefixLength:][:fileIdentifierLength])
}

// BufferHasIdentifier checks if the identifier in a buffer has the expected value
func BufferHasIdentifier(buf []byte, identifier string) bool {
return GetBufferIdentifier(buf) == identifier
}

// BufferHasIdentifier checks if the identifier in a buffer has the expected value for a size-prefixed buffer
func SizePrefixedBufferHasIdentifier(buf []byte, identifier string) bool {
return GetSizePrefixedBufferIdentifier(buf) == identifier
}
8 changes: 8 additions & 0 deletions grpc/examples/go/greeter/models/HelloReply.go

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

8 changes: 8 additions & 0 deletions grpc/examples/go/greeter/models/HelloRequest.go

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

61 changes: 45 additions & 16 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ class GoGenerator : public BaseGenerator {
const std::string size_prefix[] = { "", "SizePrefixed" };
const std::string struct_type = namer_.Type(struct_def);

bool has_file_identifier = (parser_.root_struct_def_ == &struct_def) &&
parser_.file_identifier_.length();

if (has_file_identifier) {
code += "const " + struct_type + "Identifier = \"" +
parser_.file_identifier_ + "\"\n\n";
}

for (int i = 0; i < 2; i++) {
code += "func Get" + size_prefix[i] + "RootAs" + struct_type;
code += "(buf []byte, offset flatbuffers.UOffsetT) ";
Expand All @@ -312,6 +320,26 @@ class GoGenerator : public BaseGenerator {
}
code += "\treturn x\n";
code += "}\n\n";

code += "func Finish" + size_prefix[i] + struct_type +
"Buffer(builder *flatbuffers.Builder, offset "
"flatbuffers.UOffsetT) {\n";
if (has_file_identifier) {
code += "\tidentifierBytes := []byte(" + struct_type + "Identifier)\n";
code += "\tbuilder.Finish" + size_prefix[i] +
"WithFileIdentifier(offset, identifierBytes)\n";
} else {
code += "\tbuilder.Finish" + size_prefix[i] + "(offset)\n";
}
code += "}\n\n";

if (has_file_identifier) {
code += "func " + size_prefix[i] + struct_type +
"BufferHasIdentifier(buf []byte) bool {\n";
code += "\treturn flatbuffers." + size_prefix[i] +
"BufferHasIdentifier(buf, " + struct_type + "Identifier)\n";
code += "}\n\n";
}
}
}

Expand Down Expand Up @@ -513,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 @@ -892,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 @@ -915,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 @@ -1032,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 @@ -1046,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 @@ -1116,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 @@ -1233,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 @@ -1245,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 @@ -1289,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 @@ -1477,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.

Loading

0 comments on commit 87d6f84

Please sign in to comment.