Skip to content

Commit

Permalink
Support file_identifier in Go (#7904)
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 56ecc1f commit 63495b9
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 13 deletions.
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.

28 changes: 28 additions & 0 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
20 changes: 20 additions & 0 deletions tests/MyGame/Example/Monster.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/Example/Referrable.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/Example/Stat.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/Example/TestSimpleTableWithEnum.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/Example/TypeAliases.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/Example2/Monster.go

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

8 changes: 8 additions & 0 deletions tests/MyGame/InParentNamespace.go

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

Loading

0 comments on commit 63495b9

Please sign in to comment.