Structured Objects For Anyone
... so optimized, feels amazing.
This repository holds the SofaBuffers code generator — the tool that turns a
declarative YAML/JSON object definition (validated against
schema/sofabuffers-schema-v1.json) into
idiomatic, typed source code for every supported language. The generated code
is a thin, zero-overhead wrapper that calls into the highly-optimized corelib
runtime for its language, so the hard part — a fast, portable, footprint-tuned
wire codec with a uniform streaming API — is owned by the corelibs, not the
generated code.
The generator (sofabgen) emits typed code for C, Go, Python, TypeScript,
C++, Rust, C#, Java, Kotlin, Zig, and Dart. Every backend is built against its real corelib,
JSON round-trips every field kind, and is byte-exact against the shared wire
vectors — so code generated for one language interoperates with any other.
sofabgen is a single static binary with no runtime dependencies. Every channel
below ships the same binary from the same
release, so they are
interchangeable — pick whichever fits how you already manage tools.
| Channel | Install | Best for |
|---|---|---|
| Script | curl -fsSL …/install.sh | sh |
a workstation or a container image |
| npm | npm install --save-dev @sofa-buffers/generator |
JS/TS projects, pinned per project |
| PyPI | uv tool install sofabgen |
Python projects, or a standalone CLI |
| GitHub Actions | uses: …/setup-sofabgen@v0.25.0 |
CI |
| Go toolchain | go install …/cmd/sofabgen@latest |
you already build Go |
| From source | go build -o sofabgen ./cmd/sofabgen |
hacking on the generator |
Prebuilt static binaries for Linux, Windows and macOS (x86 and ARM, 32- and 64-bit) are attached to every release. This grabs the latest — with OS/arch detection and SHA-256 verification:
curl -fsSL https://raw.githubusercontent.com/sofa-buffers/generator/main/install.sh | shSet SOFABGEN_VERSION=vX.Y.Z to pin a release, or SOFABGEN_INSTALL_DIR=<dir> to
choose where it lands.
A project-local dev dependency: the prebuilt binary ships as a per-platform
optional dependency, so there is no Go toolchain and no postinstall download,
and the binary is integrity-hashed in your lockfile. The package is
@sofa-buffers/generator, the command is sofabgen (see
packaging/npm/):
npm install --save-dev @sofa-buffers/generator
npx sofabgen --lang typescript --in messages/ --out src/generated/A plain CLI — one wheel per platform, each holding nothing but the binary, so pip
puts sofabgen straight on your PATH (no Python code, no build step; see
packaging/pypi/):
uv tool install sofabgen # or: pipx install sofabgen / pip install sofabgen
uvx sofabgen --lang python --in messages/ --out src/generated/The bundled composite action installs the CLI and puts it on PATH for later
steps:
- uses: sofa-buffers/generator/.github/actions/setup-sofabgen@v0.25.0
with:
version: v0.25.0 # optional; defaults to the latest release
- run: sofabgen --versiongo install github.com/sofa-buffers/generator/cmd/sofabgen@latestThe binary self-reports the installed version.
go build -o sofabgen ./cmd/sofabgen# Generate typed sources for one language from a definition.
sofabgen --lang go --in examples/messages/example.yaml --out out/go
# Generate for every language.
for lang in c cpp go python typescript rust csharp java kotlin zig dart; do
sofabgen --lang "$lang" --in examples/messages/example.yaml --out "out/$lang"
done
# Render the definitions as a self-contained HTML reference page instead:
sofabgen --lang docs --in examples/messages/example.yaml --out out/docs
# Scaffold a full buildable project + encode/decode harness:
# sofabgen --config myconfig.yaml --lang rust --in examples/messages/example.yaml --out out
# A folder is one schema: all its definition files, generated together.
sofabgen --lang python --in examples/messages/realworld --out out/pythonExamples:
examples/messages/example.yaml— a showcase exercising every field kind.examples/messages/realworld/— a realistic connected-vehicle telemetry schema split across multiple files with cross-file$ref.
Each backend has a one-command conformance harness at
tests/conformance/<lang>/run.sh that generates the example, builds it against the
real corelib, and round-trips it.
The "minimal-footprint vs. maximum-throughput" claim below is measured, not
asserted: tests/bench/ records instructions/op and embedded
.text for every (language × corelib) combination in a committed results.txt, so
a codegen change that costs or saves shows up as a diff.
- Reads an object/message definition in YAML (JSON also accepted).
- Validates it against the JSON Schema first — a hard, non-optional gate. Invalid input produces a clear, located error, a non-zero exit, and no output. Invalid definitions are never code-generated.
- Resolves
$refinto a shared-type graph and lowers the definition into a language-neutral Intermediate Representation (IR). - Emits one typed
serialize/deserializetype per object for the selected target language, tuned to that corelib's profile (minimal-footprint vs. maximum-throughput). - Ships as a single, statically-linked, cross-platform binary (
sofabgen).
The generator emits code against one corelib per language:
| Target | Corelib | Profile |
|---|---|---|
C (object.h) |
corelib-c-cpp |
Embedded, minimal footprint, no heap |
| C++ (embedded) | corelib-c-cpp (sofab.hpp) |
Embedded friendly |
| C++ (max speed) | corelib-cpp |
Max speed |
Rust (no_std) |
corelib-rs-no-std |
Embedded, no alloc by default |
| Rust (std) | corelib-rs |
Max speed |
| Go | corelib-go |
Max speed |
| Python | corelib-py |
Max speed |
| Java | corelib-java |
Max speed |
| Kotlin (Multiplatform) | corelib-kotlin-mp |
Max speed; one source set for JVM, JS and native |
| C# / .NET | corelib-cs |
Max speed |
| TypeScript | corelib-ts |
Max speed |
| Zig | corelib-zig |
Max speed |
| Dart | corelib-dart |
Max speed |
Because every corelib speaks the same wire format, code generated for one language interoperates with code generated for any other for free.
Beyond the language backends there is one non-code target: docs renders the
definitions as a self-contained HTML reference page (messages, field
tables, cross-linked named types — see
docs/generator/docs.md).
The CLI is deliberately tiny — everything configurable lives in a config file:
sofabgen --config <file> --lang <c|cpp|rust|go|python|java|kotlin|csharp|typescript|zig|dart|docs> \
[--in <file|dir>] [--out <dir>] [--format off|auto|require]| Argument | Required | Purpose |
|---|---|---|
--config <file> |
yes | YAML/JSON config carrying all other options |
--lang <target> |
yes | Which backend to generate |
--in <file|dir> |
no | Override the config's input: one definition file, or a folder whose definition files (.yaml/.yml/.json) are generated together as one schema — every message and type once; two different definitions sharing a name across its files are an error naming both |
--out <dir> |
no | Override the config's output folder |
--format <mode> |
no | Override the config's generic.run_formatter: off (default) runs no external formatter, auto runs the target's one when installed, require fails without it |
.
├── cmd/sofabgen/ # the `sofabgen` CLI entry point
├── generators/ # one backend per target (c, cpp, rust, golang, python, java, kotlin, csharp, typescript, zig, dart, docs)
├── internal/ # parser, validator, IR, analysis, generation pipeline
├── schema/ # the message-definition JSON Schema (draft-07)
├── examples/ # example definitions (config/ + messages/)
├── tests/ # conformance harnesses, the hermetic matrix, and bench/ (Ir/op + footprint)
├── docs/ # architecture & design
├── assets/ # logo & icon
├── .devcontainer/ # dev container: Go + the conformance-harness toolchains
└── LICENSE # MIT
The definition schema (schema/sofabuffers-schema-v1.json) is authoritative for
the input format: field types (u8–u64, i8–i64, fp32/fp64, boolean,
string, blob, array, enum, bitfield, struct, union), unique field
ids, $ref-able $defs, and the custom keywords uniqueIds and
defaultMatchesEnum.
A .devcontainer (Ubuntu 26.04 + Go, plus the toolchains the per-language
conformance harnesses shell out to: C/C++, Zig, Rust, Java/Maven, Kotlin/Gradle,
.NET, Python, Node) is provided. To use it locally, copy the secrets template first (the real
.env is gitignored):
cp .devcontainer/.env.example .devcontainer/.envThen open the folder in a devcontainer-aware editor, or build the image directly
via the scripts under .devcontainer/. The generator is written in Go — a
single static binary with frictionless GOOS/GOARCH cross-compilation.
docs/ARCHITECTURE.md— how the generator is structured: the parser → validator → IR → backend pipeline, the per-corelib decode models, and the design patterns (Composite / Visitor / Builder / Strategy) that hold it together.docs/PLAN.md— the design rationale: input format, corelib runtime contract, generated-code shape, optimization strategy, and configuration.
