Instructions¶
These steps install the library, decode a document, and generate Mojo from a JSON Schema.
Install the published package¶
The linux-64 package is mojo-toml on
prefix.dev/leo-gan/leo-gan.
That installs toml.mojoc together with wire, runtime, and schema, and
the gld-tomlgen-mojo command. It needs mojo-compiler 1.1.
Build from a git checkout¶
If pixi install returns 401 on conda.modular.com, put PREFIX_API_KEY in
a local .env file and run scripts/ci-setup.sh. That file is gitignored.
Requires Mojo 1.1.0.
Decode a document¶
decode_toml returns a TomlDoc. The root is always a table. encode_toml
writes it back as text. The default layout uses standard table headers.
Generate Mojo¶
Write a JSON Schema file. The accepted keywords are type, properties,
required, items, $ref, $defs, definitions, enum, const, oneOf,
anyOf, $id, title, description, and $schema. Two TOML extras are
x-toml-inline and x-toml-compact. Datetime fields use "type": "datetime",
"datetime-local", "date-local", or "time-local".
pixi run mojo run -I src src/codegen/cli.mojo -- \
--schema testdata/schema/benchmark_v2.json \
--out tests/generated
The installed command is the same tool:
A property that is not required becomes Optional[T]. A required field whose
type refers back to the same struct is rejected. An optional self-reference is
emitted as Optional[Box[T]]. Mojo 1.1 still refuses to compile a struct that
names itself, so that form is what the generator writes and what
tests/test_schema.mojo checks in the source.
Tests¶
tests/test_suite.mojo walks the vendored toml-test 1.1 files. Invalid files
must fail. Valid files must match the expected JSON, and encoding them must
decode to the same values. conformance/run.sh runs the official toml-test
binary when it is on PATH, and exits 0 when it is not.