summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeroen Demeyer <jeroen.demeyer@be-mobile.com>2023-04-26 07:15:09 +0200
committerGitHub <noreply@github.com>2023-04-26 05:15:09 +0000
commit63495b935a06cba8f3b09e21fb79f08d50f06648 (patch)
treef777a0ba0d2b0c7b26b053edf01b9e833fb96241 /src
parent56ecc1f548f4bb4143b7a01db9b01d331e468977 (diff)
downloadflatbuffers-63495b935a06cba8f3b09e21fb79f08d50f06648.tar.gz
flatbuffers-63495b935a06cba8f3b09e21fb79f08d50f06648.tar.bz2
flatbuffers-63495b935a06cba8f3b09e21fb79f08d50f06648.zip
Support file_identifier in Go (#7904)
Co-authored-by: Derek Bailey <derekbailey@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_go.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp
index 6a66b5c6..f2ffc3e7 100644
--- a/src/idl_gen_go.cpp
+++ b/src/idl_gen_go.cpp
@@ -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) ";
@@ -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";
+ }
}
}