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

OptionTypeDefaultDescription
unionAnyModelNamestring'ModelinaAnyType'Name for any models in unions
unionDictModelNamestring'ModelinaDictType'Name for dictionary models in unions
unionArrModelNamestring'ModelinaArrType'Name for array models in unions
typeMappingTypeMappingGoDefaultTypeMappingCustom type mappings
constraintsConstraintsGoDefaultConstraintsCustom constraint rules
indentation{ type, size }{ type: SPACES, size: 2 }Indentation settings
presetsPresets[]Array of presets to apply

Model Dispatch

ConstrainedMetaModel TypeRenderer
ConstrainedObjectModelStructRenderer
ConstrainedEnumModelEnumRenderer
ConstrainedUnionModelUnionRenderer

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

HookCalledArgsPurpose
selfOnce per struct{ renderer, model, content, options }Override entire struct output
fieldPer property{ renderer, model, content, options, field }Individual field definition
discriminatorOnce per struct{ renderer, model, content, options }Discriminator function for unions
additionalContentOnce 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

HookCalledArgsPurpose
selfOnce per enum{ renderer, model, content, options }Override entire enum output
itemPer value{ renderer, model, content, options, item, index }Individual enum constant
additionalContentOnce 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

HookCalledArgsPurpose
selfOnce per union{ renderer, model, content, options }Override entire union output
fieldPer union member{ renderer, model, content, options, field }Individual union field
discriminatorOnce per union{ renderer, model, content, options }Discriminator accessor method
additionalContentOnce 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):

OptionTypeDefaultDescription
addJsonTagboolean-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 TypeGo TypeNotes
ObjectModelNameConstrained model name
ReferenceModelNameWith optional * prefix
Anyinterface{}
Floatfloat64 or *float64Pointer if nullable and not required
Integerint or *intPointer if nullable and not required
Stringstring or *stringPointer if nullable and not required
Booleanbool or *boolPointer if nullable and not required
Tuple[]interface{}No native tuple support
Array[]Type
EnumEnumName
UnionUnionName
Dictionarymap[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 string
  • optionsGoOptions
  • partOfProperty? — set when resolving type for a property (has .required flag; nullable non-required types use pointer *)
  • dependencyManagerGoDependencyManager to 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:

MethodDescription
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});