Skip to content

Go target

Use the Go target for Go services and CLIs that need generated structs and JSON/YAML load/save helpers.

emit-targets:
- type: Go
output-dir: "generated/go"
test-dir: "generated/go/tests"
import-path: "fixtures"
package-name: "fixtures"
protocol-scaffolds: "compile-only"
OptionWhy it matters
output-dirWhere generated Go source is written.
test-dirWhere generated Go tests are written.
import-pathImport path used by generated tests.
package-namePackage name for generated Go files.
protocol-scaffoldsEmits compile-only test scaffolds for generated protocols.

Generated Go surfaces include structs, load/save helpers, JSON/YAML helpers, provider wire mapping helpers when configured, compile-only protocol scaffolds, and generated tests.

root, err := fixtures.FixtureRootFromJSON(jsonText)
if err != nil {
return err
}
saved, err := root.ToJSON()

For models with scalar coercions, the generated TypeFromJSON and TypeFromYAML helpers preserve scalar shorthand input instead of requiring callers to wrap values in objects:

reference, err := fixtures.FixtureReferenceFromJSON(`"ref-coerced"`)
if err != nil {
return err
}

LoadType accepts parsed map[string]interface{} values for object-shaped input. When a model declares scalar coercions, the JSON/YAML helpers parse into interface{} and pass the parsed value through to LoadType, so direct loads and text helpers follow the same coercion path.

Nested object loads and polymorphic collection loads propagate child load errors. Abstract polymorphic bases return Go errors for unknown or missing discriminator values, while non-abstract default variants continue to handle fallback cases.

Generated Go tests exercise load/save roundtrips, nested objects, scalar arrays, dictionaries, provider wire names, scalar coercion helpers, malformed JSON handling, and polymorphic child resolution.

The fixture validation gate also runs Go formatting and runtime checks:

Terminal window
npm run generate:fixtures --workspace @typra/emitter
npm run validate:fixtures --workspace @typra/emitter

During fixture validation, Typra runs gofmt -l, go vet ./..., go test ./..., and an executable conformance check for the generated Go module.