Skip to content

Code Generation

Oliver Atkinson edited this page May 31, 2026 · 1 revision

Code Generation

lexicon-generate turns a composed lexicon into source or interchange artifacts.

swift run lexicon-generate commerce.lexicon \
	--type swift,kotlin,go,rust,ts,json,json-ld

The --type option accepts a comma-separated list.

Command Output
swift Swift source that imports SwiftLexicon.
swift-standalone Stand-alone Swift source.
kotlin Stand-alone Kotlin source.
go Stand-alone Go source.
rust Stand-alone Rust source.
ts Stand-alone TypeScript source.
json JSON classes and mixins snapshot.
json-ld SKOS JSON-LD.

Output Paths

swift run lexicon-generate commerce.lexicon --type go -o Generated/commerce

-o/--output is a path without the final extension. The generator appends the extension for the selected type.

Examples:

Type Output with -o Generated/commerce
swift Generated/commerce.swift
go Generated/commerce.go
rust Generated/commerce.rs
ts Generated/commerce.ts
json Generated/commerce.json
json-ld Generated/commerce.jsonld

Do not generate two targets with the same extension to the same output stem in one command. For example, generate swift and swift-standalone separately because both write .swift.

Composition

Generation composes imports before emitting code:

@ ./shared-commerce.lexicon

commerce:
	api:
		@ ./storefront-api.lexicon

If composition conflicts are reported, generation fails instead of choosing a partial output.

Choosing a Target

Use swift when generated Swift should depend on the SwiftLexicon runtime and event APIs.

Use swift-standalone, kotlin, go, rust, or ts when you want a single generated file with no Lexicon package runtime dependency in the target language.

Use json when another tool wants the resolved class/mixin graph.

Use json-ld when ontology, knowledge-graph, or documentation tooling wants SKOS-compatible JSON-LD.

Build Integration

For Swift packages, prefer the build-tool plugins:

.target(
	name: "CommerceVocabulary",
	dependencies: [
		.product(name: "SwiftLexicon", package: "Lexicon")
	],
	plugins: [
		.plugin(name: "SwiftLibraryGeneratorPlugin", package: "Lexicon")
	]
)

The plugin scans the target directory for .lexicon files and generates Swift into SwiftPM's plugin work directory.

Use SwiftStandAloneGeneratorPlugin when the generated source should not depend on SwiftLexicon:

.target(
	name: "CommerceVocabulary",
	plugins: [
		.plugin(name: "SwiftStandAloneGeneratorPlugin", package: "Lexicon")
	]
)

For non-Swift projects, run lexicon-generate from the native build tool:

  • go generate for Go.
  • build.rs or a Makefile target for Rust.
  • Gradle Exec task for Kotlin.
  • npm script for TypeScript.

Keep generated files in a deterministic directory and commit them if the target build should not need Swift installed.

Naming Model

Generated code preserves exact lemma IDs while adapting selectors to target-language rules:

  • Swift and Kotlin use backticks for reserved words.
  • Go exports TitleCase selectors and keeps exact lowercase IDs through ID().
  • Rust uses raw identifiers such as r#type and also provides an exact-path macro.
  • TypeScript uses class fields and __ for the exact ID.

See the individual language pages for examples.

Clone this wiki locally