generate

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BuildVersion = "dev"
	BuildCommit  = "unknown"
)

BuildVersion and BuildCommit identify the tommy build that produced a generated file. They are stamped into the generated header (RenderFile) so a binary↔library version skew — a stale `tommy` codegen binary run against a newer tommy library — is visible in the output rather than silent until compile (tommy#125).

cmd/tommy/main sets these from the ldflags-injected main.version / main.commit (eng-versioning(7): single-binary repos embed -X main.version / main.commit). They default to dev/unknown for in-process use (the ./generate tests, `go run`, or a `go build` without ldflags). A dirty build's commit carries a "-dirty" suffix (flake passes self.dirtyShortRev), so even uncommitted codegen is distinguishable.

Functions

func Generate

func Generate(dir, filename string) error

func OutputPath added in v0.4.2

func OutputPath(dir, filename string) string

OutputPath returns the generated-file path for a source file: <base>_tommy.go in the same directory. Exposed so callers can report or diff the target without re-deriving the convention.

func Render added in v0.4.2

func Render(dir, filename string) ([]byte, error)

Render analyzes filename and returns the formatted generated-file content WITHOUT writing it. Generate writes Render's output; `tommy generate --check` diffs it against the on-disk file. Deterministic for a fixed input + tommy build (the header carries BuildVersion/BuildCommit), so a content mismatch means the file is stale or was produced by a different tommy.

func RenderFile

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

RenderFile renders the generated *_tommy.go file for the given structs.

Types

type FieldInfo

type FieldInfo struct {
	GoName    string
	TomlKey   string
	Type      spkType
	OmitEmpty bool
	Multiline bool
}

FieldInfo describes a single field within a struct. Type carries the compositional classification (#85); GoName/TomlKey and the tag-derived OmitEmpty/Multiline are the field metadata the type cannot supply.

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 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) SegmentCount added in v0.3.0

func (k TOMLKey) SegmentCount() int

SegmentCount returns the number of dot-separated TOML key segments this key denotes, known structurally at codegen time. A KeyVar (a spliced runtime map key) counts as exactly one segment no matter what its runtime value contains — so a dotted map key like "k.0" stays one segment, which the runtime strings.Count(joined, ".") miscounts (#103, the nested-map decode bug). Each dot inside a KeyLit part is a segment separator. Panics on a KeyPrefix part: the keyPrefix parameter's segment count is not known at codegen time, and the only caller (compMapStruct) is reached only with KeyPrefix-free keys.

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.

Jump to

Keyboard shortcuts

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