Documentation
¶
Overview ¶
Package flatschema infers a FlatBuffers schema from sample buffers.
FlatBuffers data carries no schema. A buffer is a vtable of slot offsets and the bytes they point at, and the names and widths of the fields live only in the .fbs file that generated it. When you have the data and not the file, the usual route is to read the bytes by hand, write down which slot held what, and build the schema up one field at a time.
This does that pass for you. It walks one or more buffers, records what each slot looks like, merges the observations, and emits a .fbs skeleton that flatc will compile.
What comes out is a starting point, not a recovered original, and the reasons are in the format rather than in this package:
- Field names are not in the data at all. Fields come out as f4, f6 and so on, named for their slot.
- A scalar's width is not in the data either. A slot holding four bytes is a uint, an int, a float, or four bytes of something else, and only the schema knew which.
- A slot is only visible when a buffer populates it. More samples means more of the schema, which is why Infer takes several.
The one thing it will not do is guess quietly. Every field that was inferred rather than read carries a comment saying what was seen.
Index ¶
Constants ¶
const MaxDepth = 12
MaxDepth is how far nested tables are followed when no depth is given.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Field ¶
type Field struct {
// Slot is the vtable slot, counting from 4 in steps of 2.
Slot uint16
// Kinds is what the slot looked like, and how often. More than one entry
// means the samples disagreed, which is worth a human deciding rather than
// this package picking.
Kinds map[flatread.Kind]int
// Seen is how many tables of this type populated the slot, out of Tables on
// the type. A slot every sample sets is likelier to be required than one
// that showed up once.
Seen int
// Samples holds a few rendered values, for the comment in the output. It is
// how a reader recognises a field that this package can only call f12.
Samples []string
}
Field is one slot of one table, as it appeared across the samples.
type Options ¶
type Options struct {
// MaxDepth limits how far nested tables are followed. 0 means [MaxDepth].
MaxDepth int
// Elems limits how many elements of a table vector are examined. 0 means
// all of them.
//
// Every element of a vector shares one schema, so the first is usually
// enough. Reading them all is worth the time anyway: elements populate
// different slots, and a field only one element sets is a field that would
// otherwise be missed.
Elems int
}
Options controls InferWith.
type Schema ¶
type Schema struct {
// Types in the order they were first reached, so the root comes first and
// the output reads top down.
Types []*Type
// Buffers is how many samples went in, and Identifier the file identifier
// if the samples carried one and agreed on it.
Buffers int
Identifier string
// contains filtered or unexported fields
}
Schema is what was inferred.
type Type ¶
type Type struct {
// Path is the slot route from the root. The root itself has none.
Path []uint16
// Tables counts how many tables of this type were walked.
Tables int
// Fields is keyed by slot.
Fields map[uint16]*Field
}
Type is one table shape, identified by where it sits rather than by a name, since the buffer holds no names.
Two tables are the same type when they are reached by the same route from the root: the table in root slot 10 is one type, and the tables in the vector in root slot 12 are another. Element indices are not part of the route, because every element of a vector shares a schema.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
flatschema
command
Command flatschema infers a FlatBuffers schema from sample buffers.
|
Command flatschema infers a FlatBuffers schema from sample buffers. |