I am building a node in flatbuffers that contain different object types and a signature of that object.
------------------
| Node |
| object_sig |
| -------- |
| | Object | |
| -------- |
------------------
Pseudocode
The high level would look something like this.
builder := flatbuffers.NewBuilder(0)
bufs.ObjectStart(builder)
bufs.ObjectAddField(builder, field)
objectOff := bufs.ObjectEnd(builder)
obj := // TODO: get object
sig := ObjectSignFields(obj)
sigOff := builder.CreateByteVector(sig)
bufs.NodeStart(builder)
bufs.NodeAddSig(builder, sigOff)
bufs.NodeAddObject(builder, objectOff)
builder.Finish(bufs.NodeEnd(builder))
return builder.FinishedBytes()
Current Approach
I would like to access the object before the builder is finalized.
From reading the docs (and Go source), the method I have come up with is to clone the builder by copying its byte buffer to a new builder. There are a handful of non-public fields in the struct that do not get copied over but none of those are touched when calling Finish()
.
shadow := flatbuffers.NewBuilder(0)
shadow.Bytes = make([]byte, len(builder.Bytes))
copy(shadow.Bytes, builder.Bytes)
shadow.Finish(objectOff)
obj := bufs.GetRootAsObject(shadow.FinishedBytes(), 0)
Questions
Is there a more official way to access an object before the builder is finalized?
Will the current approach be stable?