generate

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(dir, filename string) error

func RenderFile

func RenderFile(w io.Writer, pkg string, structs []StructInfo) error

func RenderFileJen

func RenderFileJen(w io.Writer, pkg string, structs []StructInfo) error

Types

type DecodeOp

type DecodeOp interface {
	// contains filtered or unexported methods
}

DecodeOp represents a single decode operation in the IR. The builder constructs a tree of DecodeOps with all scoping decisions resolved. The renderer walks the tree and emits Go source with minimal context.

type DelegateMap

type DelegateMap struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	ElemType   string // "pkg.Type"
	ImportPath string // full import path for the package
	UseRootAPI bool   // FindSubTables vs FindSubTablesInContainer
}

DelegateMap delegates decoding of map[string]cross-package struct per sub-table.

type DelegateSlice

type DelegateSlice struct {
	Target       string
	Key          string
	DottedKey    string // full dotted key for FindArrayTableNodes
	Tgt          TargetPath
	TKey         TOMLKey
	TDottedKey   TOMLKey
	TypeName     string // "pkg.Type"
	ImportPath   string // full import path for the package
	SlicePointer bool
}

DelegateSlice delegates decoding of []cross-package struct per array table entry.

type DelegateStruct

type DelegateStruct struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string // "pkg.Type"
	ImportPath string // full import path for the package
	Pointer    bool   // allocate *Type before delegating
	UseRootAPI bool   // FindTable vs FindTableInContainer
}

DelegateStruct delegates decoding to another package's DecodeXInto.

type EncodeDelegateMap

type EncodeDelegateMap struct {
	Tgt        TargetPath
	TKey       TOMLKey
	ElemType   string // "pkg.Type"
	ImportPath string
	UseRootAPI bool
}

EncodeDelegateMap loops, EnsureSubTable, calls pkg.EncodeXFrom.

type EncodeDelegateSlice

type EncodeDelegateSlice struct {
	Tgt          TargetPath
	TKey         TOMLKey // bare key
	TDottedKey   TOMLKey // full dotted key
	TypeName     string
	ImportPath   string
	SlicePointer bool
}

EncodeDelegateSlice loops, get/create container, calls pkg.EncodeXFrom.

type EncodeDelegateStruct

type EncodeDelegateStruct struct {
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string // "pkg.Type"
	ImportPath string
	Pointer    bool
	UseRootAPI bool
}

EncodeDelegateStruct creates table then calls pkg.EncodeXFrom.

type EncodeOp

type EncodeOp interface {
	// contains filtered or unexported methods
}

EncodeOp represents a single encode operation in the IR.

type EncodeValidate

type EncodeValidate struct {
	DataTarget TargetPath
}

EncodeValidate calls data.Validate() before encoding.

type FieldInfo

type FieldInfo struct {
	GoName       string
	TomlKey      string
	Kind         FieldKind
	ElemType     string // wrapper type name for primitive aliases (e.g., "types.Version")
	TypeName     string
	InnerInfo    *StructInfo
	OmitEmpty    bool
	Multiline    bool
	ImportPath   string // import path needed for ElemType (e.g., "example.com/pkg/types")
	SlicePointer bool   // true when slice element is a pointer (e.g., []*Struct)
}

FieldInfo describes a single field within a struct.

type FieldKind

type FieldKind int

FieldKind classifies how a struct field should be encoded/decoded.

const (
	FieldPrimitive                FieldKind = iota // string, int, int64, float64, bool
	FieldStruct                                    // nested struct with toml tags
	FieldPointerStruct                             // *SomeStruct
	FieldSlicePrimitive                            // []int, []string
	FieldSliceStruct                               // []Server (array-of-tables)
	FieldCustom                                    // implements TOMLUnmarshaler
	FieldPointerPrimitive                          // *bool, *int, etc.
	FieldMapStringString                           // map[string]string
	FieldTextMarshaler                             // implements encoding.TextMarshaler/TextUnmarshaler
	FieldMapStringStruct                           // map[string]SomeStruct
	FieldSliceTextMarshaler                        // []TextMarshalerType
	FieldMapStringMapStringString                  // map[string]NamedMap where NamedMap is map[string]string
	FieldDelegatedStruct                           // cross-package struct — delegate to its DecodeInto/EncodeFrom
	FieldPointerDelegatedStruct                    // pointer to cross-package struct — delegate
	FieldSliceDelegatedStruct                      // []cross-package struct — delegate per element
	FieldMapStringDelegatedStruct                  // map[string]cross-package struct — delegate per entry
)

type ForArrayTable

type ForArrayTable struct {
	Key          string // relative TOML key
	DottedKey    string // full dotted key for FindArrayTableNodes
	TKey         TOMLKey
	TDottedKey   TOMLKey
	TypeName     string // element type
	ImportPath   string // import path if cross-package
	Target       string // slice target
	Tgt          TargetPath
	SlicePointer bool
	TrackHandles bool // emit handle code (top-level receiver only)
	Fields       []DecodeOp
}

ForArrayTable iterates array table nodes and decodes inner fields per entry.

type ForEncodeArrayTable

type ForEncodeArrayTable struct {
	Tgt          TargetPath
	TKey         TOMLKey // bare key for AppendArrayTableEntry
	TDottedKey   TOMLKey // full dotted key for FindArrayTableNodes
	TypeName     string
	ImportPath   string
	SlicePointer bool
	TrackHandles bool // use decode handles in receiver context
	Fields       []EncodeOp
}

ForEncodeArrayTable loops over a slice, reusing handles or appending entries.

type ForEncodeMapStringMapStringString

type ForEncodeMapStringMapStringString struct {
	Tgt      TargetPath
	TKey     TOMLKey
	TypeName string // named type for inner map (empty for plain)
}

ForEncodeMapStringMapStringString loops outer map, EnsureSubTable, DeleteAll, loop inner.

type ForEncodeMapStringStruct

type ForEncodeMapStringStruct struct {
	Tgt          TargetPath
	TKey         TOMLKey
	TypeName     string
	SlicePointer bool
	UseRootAPI   bool
	Fields       []EncodeOp
}

ForEncodeMapStringStruct loops over map entries, EnsureSubTable per entry.

type ForMapStringStruct

type ForMapStringStruct struct {
	Key          string
	TKey         TOMLKey
	TypeName     string
	Target       string
	Tgt          TargetPath
	SlicePointer bool
	UseRootAPI   bool // FindSubTables vs FindSubTablesInContainer
	Fields       []DecodeOp
}

ForMapStringStruct iterates sub-tables and decodes inner fields per map entry.

type GetCustom

type GetCustom struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string
	ImportPath string
}

GetCustom decodes a custom unmarshaler via GetRawFromContainer + UnmarshalTOML.

type GetMapStringMapStringString

type GetMapStringMapStringString struct {
	Target   string
	Key      string
	Tgt      TargetPath
	TKey     TOMLKey
	TypeName string // named type for inner map (empty for plain map[string]string)
}

GetMapStringMapStringString decodes map[string]map[string]string via FindSubTables.

type GetMapStringString

type GetMapStringString struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	UseRootAPI bool // FindTable vs FindTableInContainer
}

GetMapStringString decodes map[string]string via FindTable + GetStringMapFromTable.

type GetPrimitive

type GetPrimitive struct {
	Target     string // Go expression for assignment (e.g. "d.data.Name")
	Key        string // TOML key relative to container
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string // Go type for generic param
	ElemType   string // type alias conversion (empty if none)
	ImportPath string // import path for ElemType if cross-package
	Pointer    bool   // wrap in &v
}

GetPrimitive decodes a primitive value via GetFromContainer[T].

type GetSlicePrimitive

type GetSlicePrimitive struct {
	Target       string
	Key          string
	Tgt          TargetPath
	TKey         TOMLKey
	ElemType     string // element type for the generic param
	TypeName     string // named type conversion (empty if none)
	ImportPath   string // import path for TypeName if cross-package
	SlicePointer bool   // []*T
}

GetSlicePrimitive decodes a primitive slice via GetFromContainer[[]T].

type GetSliceTextMarshaler

type GetSliceTextMarshaler struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string
	ImportPath string
}

GetSliceTextMarshaler decodes a slice of TextUnmarshalers via GetFromContainer[[]string].

type GetTextMarshaler

type GetTextMarshaler struct {
	Target     string
	Key        string
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string
	ImportPath string
}

GetTextMarshaler decodes a TextUnmarshaler via GetFromContainer[string] + UnmarshalText.

type InEncodePointerTable

type InEncodePointerTable struct {
	Tgt      TargetPath
	TKey     TOMLKey
	TypeName string
	Fields   []EncodeOp
}

InEncodePointerTable checks nil, creates table, recurses inner fields.

type InEncodeTable

type InEncodeTable struct {
	TKey       TOMLKey
	UseRootAPI bool
	Fields     []EncodeOp
}

InEncodeTable creates/ensures a table then recurses inner fields.

type InPointerTable

type InPointerTable struct {
	Key         string
	TKey        TOMLKey
	TypeName    string
	Target      string // Go expression for pointer assignment
	Tgt         TargetPath
	TableFields []DecodeOp // fields decoded when explicit table found
	FlatFields  []DecodeOp // fields decoded at parent container (flat-key fallback)
}

InPointerTable finds a [table], allocates *Struct, decodes fields; else tries flat-key fallback for implicit parent tables.

type InTable

type InTable struct {
	Key        string
	TKey       TOMLKey
	UseRootAPI bool // FindTable vs FindTableInContainer
	Fields     []DecodeOp
}

InTable finds a [table] and decodes inner fields within it.

type KeyKind

type KeyKind int

KeyKind distinguishes the parts of a TOML key.

const (
	KeyLit    KeyKind = iota // static string segment
	KeyVar                   // runtime variable (e.g. "_mk")
	KeyPrefix                // the `keyPrefix` function parameter
)

type KeyPart

type KeyPart struct {
	Kind  KeyKind
	Value string // literal text or variable name
}

KeyPart is one segment of a TOML key.

type SegKind

type SegKind int

SegKind distinguishes field access from array index in a target path.

const (
	SegDot   SegKind = iota // .Name
	SegIndex                // [i]
)

type SetCustom

type SetCustom struct {
	Tgt  TargetPath
	TKey TOMLKey
}

SetCustom calls MarshalTOML() then SetInContainer.

type SetMapStringString

type SetMapStringString struct {
	Tgt        TargetPath
	TKey       TOMLKey
	UseRootAPI bool
}

SetMapStringString writes map[string]string via EnsureTable + DeleteAll + loop.

type SetPointerPrimitive

type SetPointerPrimitive struct {
	Tgt      TargetPath
	TKey     TOMLKey
	TypeName string // underlying type (string, int, etc.)
}

SetPointerPrimitive writes *T if non-nil.

type SetPrimitive

type SetPrimitive struct {
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string // Go type (string, int, bool, float64, etc.)
	ElemType   string // type alias underlying type (empty if none)
	ImportPath string
	OmitEmpty  bool
	Multiline  bool // use """ multiline string syntax
}

SetPrimitive writes a primitive value with zero-value/omitempty logic.

type SetSlicePrimitive

type SetSlicePrimitive struct {
	Tgt          TargetPath
	TKey         TOMLKey
	ElemType     string // element type for slice
	TypeName     string // named type conversion
	ImportPath   string
	SlicePointer bool // []*T
	OmitEmpty    bool
}

SetSlicePrimitive writes a primitive slice.

type SetSliceTextMarshaler

type SetSliceTextMarshaler struct {
	Tgt        TargetPath
	TKey       TOMLKey
	TypeName   string
	ImportPath string
}

SetSliceTextMarshaler marshals each element then writes []string.

type SetTextMarshaler

type SetTextMarshaler struct {
	Tgt       TargetPath
	TKey      TOMLKey
	OmitEmpty bool
}

SetTextMarshaler calls MarshalText() then SetInContainer.

type StructInfo

type StructInfo struct {
	Name        string
	Fields      []FieldInfo
	Validatable bool
}

StructInfo describes a struct that needs code generation.

func Analyze

func Analyze(dir, filename string) ([]StructInfo, error)

Analyze inspects the given Go source file for structs with //go:generate tommy generate directives and returns their metadata.

type TOMLKey

type TOMLKey struct {
	Parts []KeyPart
}

TOMLKey represents a TOML key path used for table header matching and consumed-key tracking. It can contain static literals, runtime variables (e.g. map iteration key), and the keyPrefix function parameter.

func PrefixedKey

func PrefixedKey(dotted string) TOMLKey

PrefixedKey creates a key that starts with the keyPrefix runtime parameter. The dotted argument is appended as a literal (e.g. PrefixedKey("auth") → keyPrefix + "auth").

func StaticKey

func StaticKey(dotted string) TOMLKey

StaticKey creates a key from a dotted string (e.g. "servers.name"). An empty string produces an empty key.

func (TOMLKey) BareKey

func (k TOMLKey) BareKey() string

BareKey returns the last segment after the final dot separator. For "servers.settings.max_conns", returns "max_conns". Works on dynamic keys by examining the last literal part.

func (TOMLKey) Dot

func (k TOMLKey) Dot(seg string) TOMLKey

Dot appends a literal segment with a dot separator. If the key is empty, the segment is added without a leading dot.

func (TOMLKey) DotPrefix

func (k TOMLKey) DotPrefix(seg string) TOMLKey

DotPrefix appends a literal dot-terminated segment (e.g. "servers.").

func (TOMLKey) IsStatic

func (k TOMLKey) IsStatic() bool

IsStatic returns true if the key contains only literal parts.

func (TOMLKey) Jen

func (k TOMLKey) Jen() *jen.Statement

Jen returns a jennifer expression for this key. Static keys become Lit("key"). Dynamic keys become concatenation expressions like Lit("targets.").Op("+").Id("_mk").Op("+").Lit(".auth").

func (TOMLKey) JenLen

func (k TOMLKey) JenLen() *jen.Statement

JenLen returns a jennifer expression for the length of this key's string value. Static keys use a literal int. Dynamic keys use len() calls.

func (TOMLKey) Lit

func (k TOMLKey) Lit(s string) TOMLKey

Lit appends a literal part.

func (TOMLKey) Static

func (k TOMLKey) Static() string

Static returns the key as a static string. Panics if the key contains dynamic parts (KeyVar or KeyPrefix).

func (TOMLKey) Var

func (k TOMLKey) Var(name string) TOMLKey

Var appends a runtime variable part (e.g. "_mk" for map iteration key).

func (TOMLKey) VarSuffix

func (k TOMLKey) VarSuffix() string

VarSuffix returns a CamelCase suffix derived from the full key path, suitable for making generated variable names unique across nesting levels. "haustoria.caldav" -> "HaustoriaCaldav", "exec-command" -> "ExecCommand". Dynamic key parts (KeyVar, KeyPrefix) are skipped since runtime variables already provide uniqueness through iteration.

type TargetPath

type TargetPath struct {
	Receiver string // "" for locals ("entry"), "d" for receiver methods
	Root     string // "data", "entry", etc.
	Segs     []TargetSeg
}

TargetPath represents a Go lvalue for assignment in generated code. It produces both a string representation (for backward-compatible renderers) and a jennifer Code tree.

func LocalTarget

func LocalTarget(name string) TargetPath

LocalTarget creates a target rooted at a local variable (e.g. entry).

func ReceiverTarget

func ReceiverTarget(recv, root string) TargetPath

ReceiverTarget creates a target rooted at a receiver field (e.g. d.data).

func (TargetPath) Dot

func (t TargetPath) Dot(name string) TargetPath

Dot appends a field access segment.

func (TargetPath) Index

func (t TargetPath) Index(varName string) TargetPath

Index appends an array index segment.

func (TargetPath) Jen

func (t TargetPath) Jen() *jen.Statement

Jen returns a jennifer Code tree for this target path.

func (TargetPath) String

func (t TargetPath) String() string

String returns the Go expression as a string (e.g. "d.data.Servers[i].Host").

type TargetSeg

type TargetSeg struct {
	Kind SegKind
	Name string // field name for SegDot, variable name for SegIndex
}

TargetSeg is one step in a target path chain.

type Validate

type Validate struct {
	DataExpr   string // "d.data" or "data"
	DataTarget TargetPath
}

Validate calls data.Validate() if the struct implements the Validate interface.

type WithFoundVar

type WithFoundVar struct {
	Inner    DecodeOp
	FoundVar string
}

WithFoundVar wraps an op to emit "foundVar = true" alongside it. Used inside InPointerTable flat-key fallback.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL