Flatbuffer. Schema
(flatbuffer v0.5.2)
Copy Markdown
Schema definition and parser for FlatBuffers.
Handles parsing and processing of FlatBuffer schema files (.fbs), including type definitions, namespaces, and includes. Supports:
- Tables, structs, enums, and unions
- Basic types (bool, int, float, string, etc.)
- Vectors and references
- Schema includes and namespaces
- File identifiers
- Shared string fields
Table fields are stored once in a tuple indexed by their FlatBuffer field ID.
Each table also has a field_ids map from binary field name to ID for direct
lookup. By default, decoded field, struct, and enum names are pre-resolved to
atoms once during schema construction. Pass safe: true to keep those names
as binaries and avoid interning schema-controlled atoms.
Example
{:ok, schema} = Schema.from_file("schema.fbs")
# or
{:ok, schema} = Schema.from_string(schema_string)
# Optionally, you can supply a resolver for includes
{:ok, schema} = Schema.from_file("schema.fbs", resolver: &File.read/1)
Summary
Functions
Reads and parses a FlatBuffer schema from a file.
Parses a FlatBuffer schema from a string.
Types
@type struct_def() :: {:struct, %{members: [{field_name(), type_def()}]}}
@type table_def() :: {:table, %{fields: tuple(), field_ids: %{required(String.t()) => non_neg_integer()}}}
@type type_def() :: table_def() | struct_def() | union_def() | enum_def()
@type type_name() :: String.t()
@type type_ref() :: {:table, %{name: type_name()}} | {:struct, %{name: type_name()}} | {:enum, %{name: type_name()}} | {:union, %{name: type_name()}} | {:union_type, type_name()} | {:vector, type_ref()} | {:bool, %{default: boolean()}} | {:byte, %{default: integer()}} | {:ubyte, %{default: integer()}} | {:short, %{default: integer()}} | {:ushort, %{default: integer()}} | {:int, %{default: integer()}} | {:uint, %{default: integer()}} | {:long, %{default: integer()}} | {:ulong, %{default: integer()}} | {:float, %{default: float()}} | {:double, %{default: float()}} | {:string, %{optional(:default) => String.t(), optional(:shared) => boolean()}}
Functions
@spec from_file( file_name :: String.t(), opts :: [resolver: resolver_fn(), safe: boolean()] ) :: {:ok, t()} | from_errors()
Reads and parses a FlatBuffer schema from a file.
Parameters
file_name(String.t()): The path to the schema file.opts: Optional keyword list of options.:resolver(resolver_fn()): A function to resolve imports or includes within the schema.:safe(boolean()): Keep decoded schema-defined names as binaries instead of atoms. Defaults tofalse.
Returns
- The parsed schema, or an error tuple if the schema could not be parsed.
Examples
iex> Flatbuffer.Schema.from_file("path/to/schema.fbs")
{:ok, schema}
iex> Flatbuffer.Schema.from_file("path/to/schema.fbs", resolver: &my_resolver/1)
{:ok, schema}
@spec from_string( string :: String.t(), opts :: [resolver: resolver_fn(), safe: boolean()] ) :: {:ok, t()} | from_errors()
Parses a FlatBuffer schema from a string.
Parameters
string(String.t()): The FlatBuffer schema as a string.opts: Optional keyword list of options.:resolver(resolver_fn()): A function used to resolve schema dependencies.:safe(boolean()): Keep decoded schema-defined names as binaries instead of atoms. Defaults tofalse.
Returns
A parsed schema.
Examples
iex(1)> schema = """ ...(1)> table Table { ...(1)> field: int; ...(1)> } ...(1)> ...(1)> root_type Table; ...(1)> """ iex(2)> {:ok, parsed_schema} = Flatbuffer.Schema.from_string(schema)