mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 14:46:26 +00:00
Add a generic way to deserialize a flatbuffer in Go.
Similar to what protobufs does with its `Message` interface, introduce here such interface and create a generic `GetRootAs` method to deserialize a flatbuffer.
This commit is contained in:
@@ -144,6 +144,23 @@ static void InitializeExisting(const StructDef &struct_def,
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Implement the table accessor
|
||||
static void GenTableAccessor(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Table() flatbuffers.Table ";
|
||||
code += "{\n";
|
||||
|
||||
if (struct_def.fixed) {
|
||||
code += "\treturn rcv._tab.Table\n";
|
||||
} else {
|
||||
code += "\treturn rcv._tab\n";
|
||||
}
|
||||
code += "}\n\n";
|
||||
}
|
||||
|
||||
// Get the length of a vector.
|
||||
static void GetVectorLen(const StructDef &struct_def,
|
||||
const FieldDef &field,
|
||||
@@ -594,6 +611,10 @@ static void GenStruct(const StructDef &struct_def,
|
||||
// Generate the Init method that sets the field in a pre-existing
|
||||
// accessor object. This is to allow object reuse.
|
||||
InitializeExisting(struct_def, code_ptr);
|
||||
// Generate _tab accessor
|
||||
GenTableAccessor(struct_def, code_ptr);
|
||||
|
||||
// Generate struct fields accessors
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end();
|
||||
++it) {
|
||||
@@ -604,6 +625,7 @@ static void GenStruct(const StructDef &struct_def,
|
||||
GenStructMutator(struct_def, field, code_ptr);
|
||||
}
|
||||
|
||||
// Generate builders
|
||||
if (struct_def.fixed) {
|
||||
// create a struct constructor function
|
||||
GenStructBuilder(struct_def, code_ptr);
|
||||
|
||||
Reference in New Issue
Block a user