Skip to content

Message Definitions

Messages are defined in Protocol Buffer (.proto) files. Struct Frame uses these definitions to generate serialization code for each target language.

Why Proto Files

Proto files provide:

  • Language-neutral message definitions
  • Type safety across language boundaries
  • Familiar syntax for developers who know Protocol Buffers
  • Tooling support (syntax highlighting, linting)

Struct Frame uses proto syntax but generates different code than Google’s Protocol Buffers. By default messages are fixed-size packed structs; variable-length encoding is opt-in per message with option variable = true (see below).

Packages

Packages group related messages and prevent name collisions:

package sensor_system;
message SensorReading {
option msgid = 1;
float value = 1;
}

Generated code uses the package name as a prefix or namespace depending on language.

Messages

Messages define the structure of data to be serialized:

message DeviceStatus {
option msgid = 1;
uint32 device_id = 1;
float battery = 2;
bool online = 3;
}

Message Options

msgid (required for top-level messages)

message Heartbeat {
option msgid = 42;
uint64 timestamp = 1;
}

Message IDs must be unique within a package. Without a package ID the range is 0-65535; when the package sets option pkgid, the range is 0-255 and the full 16-bit wire ID is (pkgid << 8) | msgid. IDs above 255 only fit profiles that carry a package-ID byte (Bulk and Network); encoding such a message with a 1-byte-ID profile (Standard, Sensor, IPC) fails instead of truncating the ID. When several packages are compiled together, the effective IDs must be unique: a package without its own pkgid inherits its importer’s, so its message IDs share that importer’s ID space.

variable (optional, enables variable-length encoding)

message SensorData {
option msgid = 1;
option variable = true; // Encode only used bytes
repeated uint8 readings = 1 [max_size=100];
}

With variable encoding, arrays and strings only transmit actual used bytes instead of the full max_size. This reduces bandwidth when fields are partially filled.

You can also enable variable-length encoding for every message in the project at code-generation time by passing the --no-packed CLI flag. This is equivalent to setting option variable = true; on every message and additionally drops the packed-struct (#pragma pack(1)) wrappers in the generated C/C++ output. Use it on platforms that don’t support packed structs or that have strict alignment requirements. It does not change byte order: all multi-byte values are little-endian on the wire, and the generated C/C++ code assumes a little-endian host. See the CLI reference for details.

pkgid (optional package-level option)

package sensors;
option pkgid = 5;

Enables extended message addressing with 16-bit message IDs (256 packages × 256 messages = 65,536 total).

Data Types

TypeSizeDescription
int81 byteSigned -128 to 127
uint81 byteUnsigned 0 to 255
int162 bytesSigned -32768 to 32767
uint162 bytesUnsigned 0 to 65535
int324 bytesSigned integer
uint324 bytesUnsigned integer
int648 bytesSigned large integer
uint648 bytesUnsigned large integer
float4 bytesIEEE 754 single precision
double8 bytesIEEE 754 double precision
bool1 bytetrue or false
stringsee belowText with size (fixed) or max_size (variable)
bytessee belowRaw bytes; same wire format and options as string

All types use little-endian byte order.

Field Default Values

A field can declare a default value with [default = ...], using the same bracket-option syntax as size/max_size/flatten:

message DeviceConfig {
option msgid = 1;
uint32 retry_ms = 1 [default = 1000];
bool enabled = 2 [default = true];
Mode mode = 3 [default = ACTIVE];
string name = 4 [size = 16, default = "device"];
}

Supported types: all integer types, bool, float, double, enum fields (using the bare enum member name, e.g. default = ACTIVE), and fixed- or bounded-size string/bytes fields (using a quoted string literal).

Not supported — rejected at generation time with a validation error:

  • repeated (array) fields
  • nested message-typed fields
  • fields inside a oneof

The generator validates the literal against the field’s type (wrong literal type, integer range overflow, unknown enum member, or a string/bytes value longer than size/max_size are all rejected with a specific error message).

What a default changes:

  • A freshly-constructed message (or one built with a constructor/init argument omitted) gets the default value for that field instead of the type’s zero value.
  • Decoding an older or truncated payload (see Wire Evolution below) applies the default to any field the sender didn’t actually transmit.
  • A value explicitly set to zero/false/empty — whether at construction or because the sender actually transmitted it — is never overwritten back to the default.

What a default does not change: wire layout, field offsets, message size, magic bytes, or CRC behavior. A default-valued field is still always present on the wire and still always serialized — it is purely an initialization and decode-fallback value, not a “this field can be omitted from the wire” marker (optional-style omission is not part of this feature).

Note: because the underlying .proto grammar parser types option values generically (bool/int/float/string), a quoted string default whose text happens to look like a number or boolean (e.g. default = "1000") is round-tripped through that type and reconstructed — this preserves the value but can normalize unusual formatting (e.g. "True" becomes "true"). Prefer defaults whose text doesn’t look like another literal type if exact formatting matters.

Getting a message with its defaults, per language

For DefaultsMessage in package default_values with u8_val = 200:

LanguageConstruct with defaultsRead the field
CDefaultValuesDefaultsMessage m; DefaultValuesDefaultsMessage_init(&m); (a bare declaration is not initialised)m.u8_val
C++default_values::DefaultsMessage m; (default member initialisers; = {} also works)m.u8_val
PythonDefaultsMessage(); pass a keyword to override one field, e.g. DefaultsMessage(u8_val=5)m.u8_val
TypeScript / JavaScriptnew DefaultsMessage() or new DefaultsMessage({ u8Val: 5 })m.u8Val
C#new DefaultsMessage() (property initialisers)m.U8Val
RustDefaultsMessage::default(), or DefaultsMessage { u8_val: 5, ..Default::default() }m.u8_val

Decoding applies the same values: a field that a shorter, older payload did not carry is filled from the schema default in every language, and a field the sender did transmit is never replaced. In C, Msg_deserialize runs Msg_init first and then overlays the received bytes, so no caller-side padding is needed.

Strings

Strings require a size specification.

Fixed-size string

string device_name = 1 [size=16];

Always uses 16 bytes, padded with nulls if shorter.

Variable-size string

string description = 1 [max_size=256];

Stores up to 256 bytes plus a length prefix. The prefix is 1 byte when max_size ≤ 255 and 2 bytes (uint16) when max_size is larger, so this example uses a 2-byte prefix.

Sizes count bytes of UTF-8, not characters, and longer values are truncated to fit. In Python, string fields accept str (encoded as UTF-8) or bytes and always hold bytes; decoded messages return bytes, so call .decode() (and .rstrip(b"\0") for fixed-size strings) to get text:

msg = Status(device_name="sensor-1") # or b"sensor-1"
name = Status.deserialize(data).device_name.rstrip(b"\0").decode()

Arrays

All repeated fields must specify a size. Arrays can contain primitive types, enums, strings, or nested messages.

Fixed arrays

repeated float matrix = 1 [size=9]; // Always 9 floats (3x3 matrix)

Bounded arrays (variable count)

repeated int32 readings = 1 [max_size=100]; // 0-100 integers

Includes a count prefix before the array data. Arrays with max_size ≤ 255 use a 1-byte (uint8) count; arrays with max_size > 255 use a 2-byte (uint16) count.

String arrays

repeated string names = 1 [max_size=10, element_size=32];

Array of up to 10 strings, each up to 32 characters.

Arrays of nested messages

message Waypoint {
double lat = 1;
double lon = 2;
}
message Route {
option msgid = 5;
string name = 1 [size=32];
repeated Waypoint waypoints = 2 [max_size=20];
}

Array of up to 20 waypoint messages. Each Waypoint is embedded inline.

Enums

enum SensorType {
TEMPERATURE = 0;
HUMIDITY = 1;
PRESSURE = 2;
}
message SensorReading {
option msgid = 1;
SensorType type = 1;
float value = 2;
}

Enums are stored as uint8 (1 byte).

Enum to String Conversion

Enums can be converted to strings using language built-in features or generated helper functions. The examples assume the schema above is in a file with package sensor_system;. Only C prefixes the type with the package name; the other languages put it in a namespace or module instead. TypeScript and JavaScript spell enum members in PascalCase, so their string is Temperature; the other languages give TEMPERATURE.

C (generated helper function)

#include "sensor_system.structframe.h"
SensorSystemSensorType type = SENSOR_TYPE_TEMPERATURE;
const char* type_str = SensorSystemSensorType_to_string(type);
// Returns: "TEMPERATURE"

C++ (generated helper function)

#include "sensor_system.structframe.hpp"
using namespace structframe::sensor_system;
SensorType type = SensorType::Temperature;
const char* type_str = SensorType_to_string(type);
// Returns: "TEMPERATURE"

Python (built-in Enum)

from structframe.sensor_system import SensorType
type = SensorType.TEMPERATURE
# Use the built-in .name property
type_str = type.name
# Returns: "TEMPERATURE"

TypeScript (built-in reverse mapping)

import { SensorType } from './sensor-system.structframe';
const type = SensorType.Temperature;
// TypeScript numeric enums support reverse mapping
const typeStr = SensorType[type];
// Returns: "Temperature"

JavaScript (Object.keys lookup)

const { SensorType } = require('./sensor-system.structframe');
const type = SensorType.Temperature;
// Find the key by value
const typeStr = Object.keys(SensorType).find(
key => SensorType[key] === type
);
// Returns: "Temperature"

C# (built-in ToString)

using StructFrame.SensorSystem;
SensorType type = SensorType.TEMPERATURE;
// Use the built-in enum ToString method
string typeStr = type.ToString();
// Returns: "TEMPERATURE"

Nested Enums

Enums can be defined inside a message body, scoping them to that message. This keeps the type closely associated with the message that uses it.

message MotorCommand {
option msgid = 42;
enum Direction {
FORWARD = 0;
REVERSE = 1;
BRAKE = 2;
}
Direction dir = 1;
uint16 speed_rpm = 2;
}

Enums are stored as uint8 regardless of where they are defined.

Nested Messages

message Position {
double lat = 1;
double lon = 2;
}
message Vehicle {
option msgid = 1;
uint32 id = 1;
Position pos = 2;
}

Nested messages are embedded inline.

Import Statements

Import proto definitions from other files:

types.proto
package common;
message Position {
double lat = 1;
double lon = 2;
}
vehicle.proto
import "types.proto";
package fleet;
message Vehicle {
option msgid = 1;
uint32 id = 1;
common.Position pos = 2;
}

Flatten Option

Flatten nested message fields into the parent. Only the Python to_dict() output and the GraphQL schema are flattened; the generated classes and structs in every language keep the nested field:

message Status {
Position pos = 1 [flatten=true];
float battery = 2;
}

In the dictionary and in GraphQL the keys are lat, lon, … instead of pos.lat; in code you still access status.pos.lat. Flattening is rejected when a child field name collides with a field of the parent.

Oneof (Union Types)

The oneof construct creates a union type where only one of the fields can be populated at a time. All fields within the oneof share the same memory, with the total size equal to the largest field.

Basic Oneof

message SensorReading {
option msgid = 10;
uint32 timestamp = 1;
oneof value {
float temperature = 1;
int32 pressure = 2;
uint16 humidity = 3;
}
}

Memory Layout:

  • The value union occupies 4 bytes (size of the largest type: float or int32)
  • Only one field (temperature, pressure, or humidity) should be set at a time
  • A uint8 field order discriminator is automatically generated (stores 1, 2, or 3 based on which field is active)

Oneof with Message Types

When all fields in a oneof are messages with msgid, an auto-discriminator using message IDs is generated:

message CommandA {
option msgid = 100;
uint8 param1 = 1;
}
message CommandB {
option msgid = 101;
uint16 param1 = 1;
uint16 param2 = 2;
}
message CommandWrapper {
option msgid = 200;
uint32 sequence = 1;
oneof command {
CommandA cmd_a = 1;
CommandB cmd_b = 2;
}
}

Memory Layout:

  • command_discriminator (uint16_t) - auto-generated, stores the message ID of the active field
  • command union - size of the largest message type

Discriminator Option

You can control discriminator behavior with the discriminator option inside a oneof:

oneof payload {
option discriminator = field_order; // or: msgid, none, auto
CmdA cmd_a = 1;
CmdB cmd_b = 2;
}

Field numbers in oneof: variants are numbered 1..M independently within the oneof — the same 1-based sequential rule that applies to message-level fields.

OptionDiscriminator TypeSizeDescription
auto (default)Depends on fieldsvariesUses msgid if all fields are messages with msgid, otherwise field_order
msgidMessage IDuint16 (2 bytes)Forces use of message IDs. Error if any field lacks msgid
field_orderField order (1-based)uint8 (1 byte)Uses declaration order: 1st field = 1, 2nd = 2, etc.
noneNo discriminator0 bytesDisables discriminator. Application must track active field

Default Auto-Discriminator Behavior

Oneof Field TypesDefault DiscriminatorSize
All messages with msgidmsgid (uint16)2 bytes
Mixed or all primitives/enumsfield_order (uint8)1 byte

Examples:

// Auto: Uses msgid discriminator (uint16) - all fields have msgid
oneof command {
CommandA cmd_a = 1; // msgid = 100
CommandB cmd_b = 2; // msgid = 101
}
// Auto: Uses field_order discriminator (uint8) - primitives don't have msgid
oneof value {
float temperature = 1; // discriminator = 1
int32 pressure = 2; // discriminator = 2
}
// Explicit: Force field_order even with messages that have msgid
oneof payload {
option discriminator = field_order;
CommandA cmd_a = 1; // discriminator = 1
CommandB cmd_b = 2; // discriminator = 2
}
// Explicit: Disable discriminator entirely
oneof data {
option discriminator = none;
uint32 integer_val = 1;
float float_val = 2;
}

Using Discriminators

With msgid discriminator:

// C++ - check which message is active by message ID
if (wrapper.command_discriminator == CommandA::MSG_ID) {
// Access wrapper.command.cmd_a
}

With field_order discriminator:

For field_order discriminators, a typed enum is generated named {MessageName}{OneofName}Field:

// C++ - use the generated enum for type-safe discriminator checks
if (sensor.value_discriminator == SensorValueField::TEMPERATURE) {
// temperature is active
} else if (sensor.value_discriminator == SensorValueField::PRESSURE) {
// pressure is active
}

Multiple Oneofs

A message can contain multiple oneof fields:

message MultiUnionMessage {
option msgid = 50;
oneof input {
float analog_value = 1;
bool digital_value = 2;
}
oneof output {
uint16 pwm_duty = 3;
bool relay_state = 4;
}
}

Each oneof is independent and contributes its own union size to the message.

Variable Oneofs

When a parent message has option variable = true;, any oneof with a discriminator is serialized in trimmed mode by default: only the active variant’s bytes are transmitted. For forward-compatible protocols, the oneof can additionally be marked option variable = true; to include a length prefix.

Requirement: The parent message must have option variable = true; for either encoding mode to take effect.

Trimmed Mode (Default for Variable Messages)

A discriminated oneof inside a variable message transmits only the active variant’s bytes. The receiver derives the size from the discriminator value.

Wire format:

[discriminator: 1–2 bytes] [variant_bytes: active variant size]
message SensorCommand {
option msgid = 30;
option variable = true;
uint8 header = 1;
oneof data {
option discriminator = "field_order";
// No 'option variable = true' — trimmed mode (default)
SmallPayload small = 1; // 3 bytes on the wire when active
LargePayload large = 2; // 32 bytes on the wire when active
}
}

Variable Mode (Length-Prefixed)

Add option variable = true; to the oneof for a fully forward-compatible format. A uint16 LE length field is inserted before the variant bytes, allowing receivers to skip unknown variants without error.

Wire format:

[discriminator: 1–2 bytes] [payload_length: uint16 LE] [variant_bytes: payload_length bytes]
message CommandMessage {
option msgid = 20;
option variable = true;
uint8 header = 1;
oneof data {
option discriminator = "field_order";
option variable = true; // Add length prefix for forward compatibility
SmallPayload small = 1; // 3 bytes on the wire
LargePayload large = 2; // 32 bytes on the wire
}
}

Oneof Size Controls

option max_size = N; — Reserve Space for Future Variants

Valid for non-variable (trimmed) oneofs only.

Increases the in-memory union buffer to at least N bytes without changing the wire size of existing variants. Use this when you plan to add larger variants in future schema versions.

oneof data {
option discriminator = "field_order";
// Trimmed mode — no 'option variable = true'
option max_size = 64; // Reserve 64 bytes in struct; future variants up to 64 bytes are safe
SmallPayload small = 1; // Still 3 bytes on the wire
}

max_size is not applicable to variable oneofs (option variable = true), because variable encoding transmits each variant at its own size regardless of the in-memory union buffer.

option min_size = N; — Guarantee a Minimum Wire Payload Size

Valid when the parent message is variable = true, and either:

  • the oneof has option variable = true;, or
  • the oneof is the only variable-length field in the message (trimmed oneof as the trailing variable-size region).

Pads the serialized payload to at least N bytes. Use this when a receiver compiled against an older schema expects a fixed-length payload.

On a variable oneof — padding is inside the length prefix; the receiver reads payload_length and ignores trailing zeros:

oneof data {
option discriminator = "field_order";
option variable = true;
option min_size = 8; // Length prefix will report at least 8
SmallPayload small = 1; // 3 bytes → wire sends [disc][0x08 0x00][3 bytes + 5 zero bytes]
LargePayload large = 2; // 8 bytes → wire sends [disc][0x08 0x00][8 bytes]
}

On a trimmed oneof — padding extends the trimmed payload; the receiver derives the length from the known variant size:

oneof data {
option discriminator = "field_order";
// Trimmed mode
option min_size = 8; // Trimmed wire payload is always at least 8 bytes
SmallPayload small = 1; // 3 bytes → padded to 8 bytes on the wire
LargePayload large = 2; // 8 bytes → transmitted as-is
}

Rules:

  • min_size must be ≤ the oneof’s buffer size (max(variant_sizes), or max_size if set). Use max_size to increase the buffer first if needed.
  • Padding bytes are always zero.

Wire evolution of variants in a trimmed oneof:

  • A variant message may grow through its own extensions_start block. Older senders omit the new bytes (a newer receiver fills them with schema defaults, or zero); newer senders add bytes that an older receiver ignores.
  • This works only for the last oneof of a variable message: its payload runs to the end of the frame, so the receiver treats every remaining byte as the active variant. The buffer passed to deserialize must therefore be exactly the message payload (frame parsers already guarantee this).
  • A trimmed oneof followed by another oneof cannot evolve, because the receiver cannot tell where its payload ends. Do not add extension fields to variants of a non-trailing trimmed oneof.
  • MIN_SIZE is the smallest valid payload across the known variants. struct-frame diff reports growth of a trailing trimmed oneof through variant extension fields as wire-compatible.

Option Summary

OptionApplies toEffect
variable = trueAny discriminated oneof in a variable messageAdds uint16 LE length prefix before variant bytes
max_size = NNon-variable (trimmed) oneofs onlySets minimum in-memory union buffer size
min_size = NVariable oneofs, or trimmed oneof as the sole variable-length field in a variable messagePads wire payload to at least N bytes

Envelope Messages (Oneof with is_envelope)

Envelope messages are container messages that wrap other messages for unified handling. They use the is_envelope option along with a oneof field to contain different message types.

Why Envelope Messages?

Envelope messages are useful when:

  • You have multiple command/response types but want a single message ID for routing
  • You need envelope-level metadata (sequence numbers, timestamps, priorities)
  • You want type-safe wrappers with SDK helper methods
  • You’re implementing a command/response protocol with multiple operation types

Defining Envelope Messages

// Individual command messages (each must have a msgid)
message ADCCommand {
option msgid = 112;
uint8 channel = 1;
uint16 sample_rate = 2;
bool enable = 3;
}
message DACCommand {
option msgid = 113;
uint8 channel = 1;
uint16 output_value = 2;
bool enable = 3;
}
// Envelope message
message CommandEnvelope {
option msgid = 200;
option is_envelope = true; // Enables envelope helper methods
// Envelope-level fields (metadata)
uint32 sequence_number = 1;
uint8 priority = 2;
bool run_immediately = 3;
// Union of all command types - exactly one will be populated
oneof command {
ADCCommand adc = 1;
DACCommand dac = 2;
}
}

Validation Rules

Envelope messages have these requirements:

  • Must have option is_envelope = true
  • Must have exactly one oneof field
  • All types in the oneof must be message types (not primitives or enums)
  • If using msgid discriminator (auto or explicit), all message types must have msgid

Discriminator Behavior

Envelope messages use the same discriminator system as regular oneof fields (see Discriminator Options above), but with the following defaults:

Discriminator ModeWhen UsedSizeValue
msgid (default)All inner messages have msgid2 bytes (uint16)Message ID of wrapped payload
field_orderAny inner message lacks msgid1 byte (uint8)1-based field order index

You can explicitly set the discriminator mode on the oneof:

message CommandEnvelope {
option is_envelope = true;
oneof command {
option discriminator = "field_order"; // Force field_order even if all have msgid
ADCCommand adc = 1;
DACCommand dac = 2;
}
}

Wire Format:

[envelope fields...] [discriminator (1-2 bytes)] [union payload (max field size)]

The discriminator is automatically set by the wrap() method and read during deserialization.

Generated SDK Helper Methods

The SDK generates convenient helper methods for envelope messages:

C++ (msgid discriminator)

// Wrap a command into an envelope - template ensures type safety
envelope_test::ADCCommand adc_cmd{.channel = 1, .sample_rate = 1000, .enable = true};
auto envelope = envelope_test::CommandEnvelope::wrap(
42, // sequence_number
1, // priority
true, // run_immediately
adc_cmd // payload (type validated at compile-time via T::MSG_ID)
);
// Only valid payload types are accepted - invalid types cause compile error:
// auto bad = envelope_test::CommandEnvelope::wrap(42, 1, true, some_other_msg); // Won't compile!
// Get payload message ID to determine which type was wrapped
uint16_t payload_id = envelope.getPayloadMessageId();
// Access the wrapped payload directly via the oneof field
if (payload_id == envelope_test::ADCCommand::MSG_ID) {
auto& adc = envelope.command.adc;
// Use adc.channel, adc.sample_rate, etc.
}

C++ (field_order discriminator)

// Separate wrap() overloads for each payload type
auto envelope = MyEnvelope::wrap(42, 1, true, adc_cmd); // Sets discriminator to 1
auto envelope2 = MyEnvelope::wrap(42, 1, true, dac_cmd); // Sets discriminator to 2
// Get field order (1-based) of the wrapped payload
uint8_t field_order = envelope.getPayloadFieldOrder();

Python

# Wrap a command into an envelope
adc_cmd = ADCCommand(channel=1, sample_rate=1000, enable=True)
envelope = CommandEnvelope.wrap(
payload=adc_cmd, # Runtime type check ensures valid payload
sequence_number=42,
priority=1,
run_immediately=True
)
# Invalid payload types raise TypeError:
# envelope = CommandEnvelope.wrap(some_other_msg, ...) # TypeError!
# Unwrap - returns the correct message type based on discriminator
payload = envelope.unwrap()
if payload:
print(f"Got payload: {type(payload).__name__}")
# Get discriminator value (method name depends on discriminator type)
payload_id = envelope.get_payload_message_id() # For msgid discriminator
# field_order = envelope.get_payload_field() # For field_order discriminator

TypeScript

// Wrap a command into an envelope - union type ensures type safety
const adcCmd = new ADCCommand();
adcCmd.channel = 1;
adcCmd.sampleRate = 1000;
adcCmd.enable = true;
// payload type is: ADCCommand | DACCommand | ...
const envelope = CommandEnvelope.wrap(adcCmd, 42, 1, true);
// Get payload message ID to determine which type was wrapped
const payloadId = envelope.getPayloadMessageId();

C# (namespace StructFrame.EnvelopeTest)

// Wrap a command into an envelope - overloads provide type safety
var adcCmd = new ADCCommand { Channel = 1, SampleRate = 1000, Enable = true };
var envelope = CommandEnvelope.Wrap(adcCmd, 42, 1, true);
// Get payload message ID to determine which type was wrapped
var payloadId = envelope.GetPayloadMessageId();

Hardcoded Magic Bytes

In normal operation the generator calculates two magic bytes for each message based on the types, positions, and sizes of its base fields. These bytes seed the checksum and act as a structural fingerprint for the message.

Use option magic_bytes to override this calculation with a fixed pair of values. This is useful when migrating an existing message across schema changes that would otherwise alter the magic bytes — for example, renaming an enum, moving an enum definition inline, or restructuring fields that are logically unchanged on the wire.

message JobStatusMessage {
option msgid = 53;
option magic_bytes = "0xA3, 0x7F"; // Preserve magic from original schema
enum Status {
JOB_START = 0;
JOB_END = 1;
JOB_ABORT = 2;
option extensions_start = 3;
JOB_FAILED = 3;
JOB_END_WARN = 4;
}
Status status = 1;
uint8 id = 2;
uint8 job_type = 3;
double time = 4;
}

Rules:

  • Both values must be in the range 1–255 (zero bytes are rejected).
  • Values can be written in hex (0xAB) or decimal (171).
  • Exactly two comma-separated values are required.
  • Changing the override value triggers regeneration (it is included in the generation hash).

When to use this: Prefer keeping the calculated magic bytes — they serve as automatic structural validation. Only use magic_bytes when you intentionally need two schema revisions to be treated as wire-compatible by existing parsers, and you have verified that the wire layout is genuinely unchanged.

Syntax Details

  • One package per file. Declare package first; option pkgid and import follow it. Several files may belong to the same package.
  • Imports are resolved relative to the directory of the schema given on the command line first, then relative to the importing file. A type is looked up in its own package and then in the packages that file imports directly; qualify it (other.Type) when two imported packages define the same name.
  • Qualified option names. Field options also accept the forms (sf.size), (struct_frame.size) and likewise for max_size, element_size, default and flatten.
  • option envelope = true is accepted as an alias of is_envelope.
  • Discriminator values are auto, msgid, field_order and none; false, off and disabled are accepted as aliases of none.
  • The extension marker. Inside a oneof, a standalone comment line that starts with Extension:: marks the following variant as the first extension variant (equivalent to option extensions_start). A trailing comment on a variant, or prose that merely mentions the marker, is ignored.
  • Reserved words. Field, oneof and variant names that are reserved words in a target language (for example class, self, delete, fn) are rejected with an error naming the language; rename the field or map it with --name-overrides.

Validation Rules

The generator enforces:

  • The file must parse: a syntax error, a missing import or a duplicate message, enum, oneof or field name is an error (and the generator exits non-zero)
  • Message IDs unique within package (0-65535 without a package ID, 0-255 with one), and unique across packages once the package ID is applied
  • Package IDs unique across packages (0-255); every package needs one when several are compiled together, except packages that inherit their importer’s
  • A type name must resolve unambiguously: types come from the current package or its directly imported packages, and a name defined in two imported packages must be qualified (pkg.Type)
  • Unknown options (a typo such as [defualt = 5] or option msg_id) produce a warning with a suggestion and are ignored
  • Enum values must be distinct and fit one byte (0-255)
  • size and max_size are mutually exclusive; element_size only applies to repeated string/bytes
  • A oneof needs at least one variant, and min_size may not exceed max_size
  • Variants of a msgid-discriminated oneof must have distinct message IDs
  • Generated names must not collide or be reserved words in the target languages (see Generated Names); use --name-overrides to rename
  • Field numbers must be 1..N in declaration order (no gaps, no duplicates)
  • All arrays must have size or max_size
  • All strings must have size or max_size
  • String arrays need both max_size and element_size
  • Array and string max_size up to 65535 (the count/length prefix is 1 byte up to 255, otherwise 2 bytes)
  • Envelope messages must have exactly one oneof field
  • Envelope oneof fields must be message types (not primitives/enums)
  • Envelope oneof using msgid discriminator must have messages with msgid
  • For message and oneof scopes, extensions_start must be ≥ 2 (at least one non-extension base field required)
  • For message and oneof scopes, extensions_start must equal an existing field number in the same scope
  • extensions_start is also accepted inside enums; enum entries with values >= extensions_start are excluded from the generation hash, but remain generated enum members
  • Message-level extensions_start cannot be combined with oneof fields — the wire serializes fields before oneofs, so extension fields would not be trailing. Use option extensions_start inside the oneof to add extension variants instead

Wire Evolution (Extension Fields)

option extensions_start = N; allows a message (or a oneof inside a message) to be extended with new fields in the future without breaking older receivers. It is also accepted inside enums to mark future enum values without changing the wire representation.

How it works

  • Fields with number < extensions_start are base fields — always present on the wire, always covered by the magic-byte checksum seed.
  • Fields with number >= extensions_start are extension fields — newer senders include them; older receivers that received a shorter payload fill them with their schema default on reception (zero if the field has no declared default — this is the behavior for every extension field until you add [default = ...] to it).
  • Enum values with number >= extensions_start are extension values — they are still emitted by every generator and still occupy the enum’s normal one-byte representation. Unlike message and oneof scopes, enum scope does not require N to match an existing value or be at least 2.
  • The magic bytes (CRC seed) are computed only from base fields. Adding extension fields never changes the magic bytes, so older parsers still validate the base portion correctly.
  • Extension bytes are still mixed into the full CRC after the magic seed, so corruption of extension data is detected.

Profile requirement: Truncated/extended payloads can only be detected when the frame carries a length field. Use a length-bearing profile (Standard, Bulk, or Network). On profiles without a length field (Sensor, IPC) extension fields are treated as ordinary fixed bytes — both sender and receiver must agree on the full message size.

Message-level extensions

Declare option extensions_start = N; anywhere in the message body, then list extension fields with numbers >= N. Messages containing a oneof cannot use message-level extension fields (the wire serializes fields before oneofs, so the extension bytes would not be trailing) — use oneof-level extensions instead:

message StatusReport {
option msgid = 10;
// Base fields (always on the wire)
uint32 device_id = 1;
uint8 status_code = 2;
uint16 error_flags = 3;
// Extension fields — present in v2+ firmware; older receivers zero-fill them
option extensions_start = 4;
uint32 uptime_seconds = 4;
float cpu_temperature = 5;
string firmware_version = 6 [max_size=32];
}

What changes on the wire:

SenderReceiverResult
v1 (base only)v1 (base only)✓ Normal fixed-size frame
v2 (base + ext)v1 (base only)✓ Receiver ignores extra trailing bytes
v1 (base only)v2 (base + ext)✓ Receiver fills extension fields with their schema default (zero if none declared)
v2 (base + ext)v2 (base + ext)✓ Full round-trip

Oneof-level extensions (extension variants)

A oneof block can also have extension variants. Older receivers that do not know about the new discriminator value simply ignore the unknown variant.

message AdcCommand {
option msgid = 100;
uint8 channel = 1;
}
message DacCommand {
option msgid = 101;
uint8 channel = 1;
}
// Added in v2
message PwmCommand {
option msgid = 102;
uint8 channel = 1;
uint16 duty_cycle = 2;
}
// Added in v3
message I2cCommand {
option msgid = 103;
uint8 address = 1;
uint8 reg = 2;
}
message JobMessage {
option msgid = 200;
bool run_immediately = 1;
oneof job {
option discriminator = "field_order";
// Base variants — understood by all receiver versions
AdcCommand adc = 1;
DacCommand dac = 2;
// Extension variants — receivers that do not know these simply report
// an unknown discriminator and skip the payload.
option extensions_start = 3;
PwmCommand pwm = 3;
I2cCommand i2c = 4;
}
}

extensions_start inside a oneof works identically to the message-level option:

  • base_size of the message uses the largest base variant size, not the largest extension variant size.
  • Magic bytes are computed from base variants only.
  • The discriminator field itself is always a base field (present in every frame).

Enum-level extensions

An enum can use extensions_start to mark values introduced by newer schema versions:

enum JobStatus {
option extensions_start = 3;
UNKNOWN = 0;
QUEUED = 1;
RUNNING = 2;
// Added in a later schema version
COMPLETE = 3;
FAILED = 4;
}

This option does not add extension bytes or change enum serialization: enum fields remain one byte, and all declared values are generated. Values at or above extensions_start are omitted from the generation hash, so adding them does not change that hash. The enum parser accepts an integer value without requiring it to match an existing enum member or to be at least 2.

BASE_SIZE constant

The generator emits a BASE_SIZE constant alongside MAX_SIZE for every message. For messages without extensions BASE_SIZE == MAX_SIZE. You can use BASE_SIZE in your application to compute how many bytes a legacy sender would transmit:

C

// WIRE_EVOLUTION_STATUS_REPORT_BASE_SIZE — non-extension payload bytes
// WIRE_EVOLUTION_STATUS_REPORT_MAX_SIZE — full payload with all extensions

Python

from structframe.my_package import StatusReport
print(StatusReport.BASE_SIZE) # 7 (device_id + status_code + error_flags)
print(StatusReport.MAX_SIZE) # 48 (+ uptime_seconds 4 + cpu_temperature 4 + firmware_version 33)

Checking wire compatibility

Run struct_frame diff OLD.sf NEW.sf in CI to verify that a new revision only adds extensions: it reports changed message IDs, magic bytes, field types and numbers, enum values, extension blocks and order-sensitive oneof variants, and lists source-only renames and file moves separately.

Complete wire-evolution example

package device_control;
// v1 command — base fields only
// v2 adds extension fields; v1 receivers ignore them gracefully
message DeviceCommand {
option msgid = 1;
// Base fields: always present, define the magic bytes
uint8 device_id = 1;
uint8 command = 2;
uint16 param = 3;
// Extension fields: v2+ only; v1 receivers zero-fill on reception
option extensions_start = 4;
uint32 timeout_ms = 4;
uint8 retry_count = 5;
}
// Envelope that adds new job types without breaking v1 dispatchers
message JobEnvelope {
option msgid = 50;
option is_envelope = true;
uint32 sequence = 1;
oneof job {
// v1 job types
DeviceCommand device_cmd = 1;
// v2 job types — older dispatchers see an unknown discriminator
option extensions_start = 2;
DeviceCommand maintenance_cmd = 2;
}
}

Comments and Documentation

Comments in a .sf file become documentation in the generated code. Every comment has exactly one owner, decided by where it is written:

KindWhere it is writtenDocuments
File docsOwn-line comments before the package statementThe generated file (license headers, overviews). They are emitted in each generated file’s header and are never attached to the first declaration.
Leading docsOwn-line comments directly before a message, enum, enum value, field, oneof or oneof variantThe next declaration
Trailing docsA comment on the same line, after a declarationThat declaration
// Copyright (c) Example Corp. <- file docs
// Robot telemetry definitions. <- file docs
package robot;
// Current robot state. <- leading docs of RobotState
enum RobotState {
IDLE = 0; // Waiting <- trailing docs of IDLE
// Driving. <- leading docs of MOVING
MOVING = 1;
}

Comments before an option or import statement, and comments left dangling at the end of a scope, document nothing and are dropped. Multi-line comments keep their line order in every generated language.

Documentation takes part in incremental generation: changing any comment that reaches generated output (including a file header) regenerates the files even though the schema structure is unchanged. Use --force only to regenerate unchanged input.

Generated Names

Source identifiers are converted to each language’s conventions with one shared tokenizer. It splits on _, -, lower→upper transitions and acronym boundaries (BLEHost → BLE + Host), so mixed-case names keep their acronyms instead of being flattened:

Source namePascalCase name (C++/TS enum entries, C# properties, JS enum keys, type names)SCREAMING_SNAKE name (C#/Python/Rust enum entries, C constants)
BleMCuHostBleMCuHostBLE_M_CU_HOST
BleHostBleHostBLE_HOST
GateCurrentGateCurrentGATE_CURRENT
BLEHostBLEHostBLE_HOST
gate_currentGateCurrentGATE_CURRENT

TypeScript/JavaScript property accessors start with a lower-case letter (gateCurrent, bleHostId) but keep the remaining acronym casing (BLEHost stays BLEHost).

Names written entirely in one case (BLE_HOST_OK, ble_host) carry no acronym information, so they are title-cased (BleHostOk) as before.

Upgrading from 0.10.x: acronym-aware conversion changes the generated names of identifiers that mix cases. For example a field sensorId is now SensorId in C# (previously Sensorid), an enum entry GateCurrent is GATE_CURRENT in C/Python/Rust/C# (previously GATECURRENT) and GateCurrent in C++ (previously Gatecurrent). Schemas that only use snake_case or SCREAMING_SNAKE names are unaffected. To keep an old spelling, pin it with --name-overrides.

When no heuristic can produce the name you want, pass --name-overrides FILE with a JSON file. Overrides are keyed by all or a language (c, cpp, ts, js, py, gql, csharp, rust); each entry maps a source name path to the exact generated name:

{
"all": { "BleMCuHost": "BleMcuHost" },
"csharp": { "Telemetry.gate_current": "GateCurrentAmps" },
"cpp": { "demo.Mode.RUN": "Running" }
}

Keys are dotted source paths (Name, Message.field, Message.oneof.variant, Enum.ENTRY, Message.Nested, Message.Nested.ENTRY), optionally prefixed with the package. A language entry wins over all, and a package-qualified key wins over an unqualified one. Unknown keys, invalid identifiers and collisions are errors. Overrides only rename generated symbols: message IDs, magic bytes and the wire layout are unchanged, and the overrides file is part of the generation hash.

Output Ordering

Generated output does not depend on the order in which files were imported or declarations were written, so reorganizing a schema into several files produces a reviewable diff:

  • Packages are emitted dependency-first (imported packages before importers).
  • Within a package, enums are sorted by name and messages are emitted dependencies-first, with ties broken by name.
  • Order-sensitive content is never reordered: fields, oneof variants (their field_order discriminator values are the 1-based declaration index), enum entries and message-nested enums keep their declaration order.

Complete Example

package robot_control;
enum RobotState {
IDLE = 0;
MOVING = 1;
ERROR = 2;
}
message Position {
double lat = 1;
double lon = 2;
float altitude = 3;
}
message RobotStatus {
option msgid = 1;
uint32 robot_id = 1;
string name = 2 [size=16];
RobotState state = 3;
Position current_pos = 4;
float battery_percent = 5;
repeated float joint_angles = 6 [size=6];
string error_msg = 7 [max_size=128];
}