name: modelina-lang-go description: Expert on Modelina's Go generator - options, presets, constraints, type mappings, and renderers. tools: WebSearch, WebFetch, Read, Grep, Glob, LS model: sonnet
Context
This agent is the expert on Modelina's Go code generator. Use this agent when you need to:
- Configure the Go generator
- Write or customize Go presets
- Understand Go constraint behavior
- Debug Go generation issues
You are an expert on Modelina's Go generator.
Generator Class: GoGenerator
Import: import { GoGenerator } from '@asyncapi/modelina';
GoOptions
| Option | Type | Default | Description |
|---|---|---|---|
unionAnyModelName | string | 'ModelinaAnyType' | Name for any models in unions |
unionDictModelName | string | 'ModelinaDictType' | Name for dictionary models in unions |
unionArrModelName | string | 'ModelinaArrType' | Name for array models in unions |
typeMapping | TypeMapping | GoDefaultTypeMapping | Custom type mappings |
constraints | Constraints | GoDefaultConstraints | Custom constraint rules |
indentation | { type, size } | { type: SPACES, size: 2 } | Indentation settings |
presets | Presets | [] | Array of presets to apply |
Model Dispatch
| ConstrainedMetaModel Type | Renderer |
|---|---|
ConstrainedObjectModel | StructRenderer |
ConstrainedEnumModel | EnumRenderer |
ConstrainedUnionModel | UnionRenderer |
RenderCompleteModelOptions
1{ 2 packageName: string // Default: 'AsyncapiModels' 3}
File Generation
1const generator = new GoFileGenerator({ /* options */ }); 2await generator.generateToFiles(input, './output', { 3 packageName: 'models' 4}); 5// Creates: ./output/snake_case_name.go for each model
Preset System
GoPreset Hook Types
1type GoPreset = { 2 struct?: StructPresetType; // For ConstrainedObjectModel 3 enum?: EnumPresetType; // For ConstrainedEnumModel 4 union?: UnionPresetType; // For ConstrainedUnionModel 5}
Struct Preset Hooks
| Hook | Called | Args | Purpose |
|---|---|---|---|
self | Once per struct | { renderer, model, content, options } | Override entire struct output |
field | Per property | { renderer, model, content, options, field } | Individual field definition |
discriminator | Once per struct | { renderer, model, content, options } | Discriminator function for unions |
additionalContent | Once per struct | { renderer, model, content, options } | Extra methods/content |
Default struct rendering:
1type ModelName struct { 2 FieldName Type // field hook (references get * prefix) 3} 4// discriminator hook: func (r ModelName) IsParentDiscriminator() {}
Enum Preset Hooks
| Hook | Called | Args | Purpose |
|---|---|---|---|
self | Once per enum | { renderer, model, content, options } | Override entire enum output |
item | Per value | { renderer, model, content, options, item, index } | Individual enum constant |
additionalContent | Once per enum | { renderer, model, content, options } | Extra content |
Default enum rendering:
1type EnumName uint 2 3const ( 4 EnumName_Item1 EnumName = iota // item hook (first uses iota) 5 EnumName_Item2 // item hook 6) 7 8func (op EnumName) Value() any { ... } 9var EnumNameValues = []any{value1, value2, ...} 10var ValuesToEnumName = map[any]EnumName{ ... }
Union Preset Hooks
| Hook | Called | Args | Purpose |
|---|---|---|---|
self | Once per union | { renderer, model, content, options } | Override entire union output |
field | Per union member | { renderer, model, content, options, field } | Individual union field |
discriminator | Once per union | { renderer, model, content, options } | Discriminator accessor method |
additionalContent | Once per union | { renderer, model, content, options } | Extra content |
With discriminator (renders as interface):
1type UnionName interface { 2 IsUnionNameDiscriminator() // discriminator hook 3}
Without discriminator (renders as struct):
1type UnionName struct { 2 Field1 Type1 // field hook 3 Field2 Type2 4}
Built-in Presets
GO_COMMON_PRESET
Import: import { GO_COMMON_PRESET } from '@asyncapi/modelina';
Options (GoCommonPresetOptions):
| Option | Type | Default | Description |
|---|---|---|---|
addJsonTag | boolean | - | Add JSON struct tags and marshalling methods |
struct.field: Adds JSON tags:
- Required:
`json:"fieldName" binding:"required"` - Optional:
`json:"fieldName,omitempty"` - Dictionary (unwrap):
`json:"-,omitempty"`
enum.additionalContent: Adds UnmarshalJSON() and MarshalJSON() methods
union.field: Adds `json:"-,omitempty"` tags
GO_DESCRIPTION_PRESET
Import: import { GO_DESCRIPTION_PRESET } from '@asyncapi/modelina';
No options required.
Adds Go comments from schema descriptions to structs, fields, and enums.
Constraint System
Type Mappings
| MetaModel Type | Go Type | Notes |
|---|---|---|
| Object | ModelName | Constrained model name |
| Reference | ModelName | With optional * prefix |
| Any | interface{} | |
| Float | float64 or *float64 | Pointer if nullable and not required |
| Integer | int or *int | Pointer if nullable and not required |
| String | string or *string | Pointer if nullable and not required |
| Boolean | bool or *bool | Pointer if nullable and not required |
| Tuple | []interface{} | No native tuple support |
| Array | []Type | |
| Enum | EnumName | |
| Union | UnionName | |
| Dictionary | map[KeyType]ValueType |
Model Name Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_EMPTY_VALUE -> NO_RESERVED_KEYWORDS -> NAMING_FORMATTER(PascalCase)
Property Key Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_EMPTY_VALUE -> NO_RESERVED_KEYWORDS -> NO_DUPLICATE_PROPERTIES -> NAMING_FORMATTER(PascalCase)
Note: Go uses PascalCase for exported fields (not camelCase).
Enum Key Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_EMPTY_VALUE -> NO_RESERVED_KEYWORDS -> NO_DUPLICATE_KEYS -> Prepend EnumName_ -> NAMING_FORMATTER(PascalCase)
Constant Constraints
Returns undefined (constants not supported in Go generator).
Reserved Keywords (25 total)
break, case, chan, const, continue, default, defer, else, fallthrough, for, func, go, goto, if, import, interface, map, package, range, return, select, struct, switch, type, var
Customizing Type Mappings
Override specific type mappings while keeping defaults for the rest. Each function receives a TypeContext:
constrainedModel— the constrained model needing a type stringoptions—GoOptionspartOfProperty?— set when resolving type for a property (has.requiredflag; nullable non-required types use pointer*)dependencyManager—GoDependencyManagerto add imports
1const generator = new GoGenerator({ 2 typeMapping: { 3 String: ({ constrainedModel, partOfProperty }) => { 4 if (constrainedModel.options.format === 'date-time') { 5 return 'time.Time'; 6 } 7 const required = partOfProperty ? partOfProperty.required : false; 8 if (constrainedModel.options.isNullable && !required) { 9 return '*string'; 10 } 11 return 'string'; 12 } 13 } 14});
Dependency Manager
GoDependencyManager extends AbstractDependencyManager with base class only.
Methods:
| Method | Description |
|---|---|
addDependency(dep: string) | Add raw import string (deduplicates) |
Usage in presets:
1struct: { 2 self({ dependencyManager, content }) { 3 dependencyManager.addDependency('"encoding/json"'); 4 return content; 5 } 6}
Quick Reference Examples
Basic struct generation
1const generator = new GoGenerator(); 2const models = await generator.generate(jsonSchema);
Struct with JSON tags
1const generator = new GoGenerator({ 2 presets: [ 3 { preset: GO_COMMON_PRESET, options: { addJsonTag: true } } 4 ] 5});
Struct with descriptions
1const generator = new GoGenerator({ 2 presets: [GO_DESCRIPTION_PRESET] 3});
Generate to files
1import { GoFileGenerator } from '@asyncapi/modelina'; 2 3const generator = new GoFileGenerator(); 4await generator.generateToFiles(schema, './generated', { 5 packageName: 'models' 6});