Skip to content

Structs

Structs are a short naming for structures and define a series of data elements with their respective type. Those individual elements can be named or unnamed.

In schema they are declared with the struct keyword followed by the name and the types contained.

The name must start with an uppercase ASCII character (A-Z), and may be followed by zero or more upper- and lowercase ASCII characters and digits (A-Z, a-z, 0-9).

Note that acronyms should be written in strict CamelCase, meaning Html instead of HTML or Api instead of API.

Individual fields in both named and unnamed form are separated by a comma ,, and it’s recommended to even give the last field a trailing comma. This allows for simpler diffs in version control systems.

The likely most common form is a named struct. Named means that each element is represented as a field with a name to identify it.

To declare the struct as named the content is contained in curly braces {...}.

A single field is defined as name: type @id, the name, its type and ID. Field names must start with a lowercase ASCII character (a-z) and may be followed by zero or more lowercase ASCII characters, digits and underscores (a-z, 0-9, _).

Here is a basic named schema with two fields field1 and field2. The first one is a 32-bit unsigned integer and assigned the ID 1. The second one is a 16-bit unsigned integer and assigned the ID 2.

struct Sample {
field1: u32 @1,
field2: u16 @2,
}

These samples describe how the schema would be defined in each language, when generating the code for it.

struct Sample {
pub field1: u32,
pub field2: u16,
// N fields...
}

As Go allows to create new instances without declaring a value for each field (they default to the zero value), an additional constructor is created.

An additional constructor is required, to ensure all fields are properly initialized when creating new instances.

In Python the @dataclass attribute is used to define the fields of a class.

This variant is very similar to named structs, but in contrast lack a field name. They can be convenient if the data type is rather compact and explicit field names aren’t needed. For example a position with the horizontal and vertical offset.

To declare the struct as unnamed the content is contained in parenthesis (...).

A single field is defined as type @id, the name and ID.

struct Sample(u32 @1, u16 @2)

These samples describe how the schema would be defined in each language, when generating the code for it.

struct Sample(u32, u16);

In addition to the above, a struct can completely omit field definitions. That is call a unit struct and doesn’t carry any data. It doesn’t take any space in encoded form either.

For most languages, this type doesn’t take up any memory either. Creating some vector or list of said type would require zero bytes.

Instead, it’s only the type that carries information.

struct Sample

These samples describe how the schema would be defined in each language, when generating the code for it.

struct Sample;
struct Pair<K, V> {
key: K @1,
value: V @2,
}

These samples describe how the schema would be defined in each language, when generating the code for it.

struct Pair<K, V> {
pub key: K,
pub value: V,
}