Documentation
¶
Index ¶
- Variables
- func Generate(dir, filename string) error
- func OutputPath(dir, filename string) string
- func Render(dir, filename string) ([]byte, error)
- func RenderFile(w io.Writer, pkg string, structs []StructInfo) error
- type FieldInfo
- type KeyKind
- type KeyPart
- type SegKind
- type StructInfo
- type TOMLKey
- func (k TOMLKey) BareKey() string
- func (k TOMLKey) Dot(seg string) TOMLKey
- func (k TOMLKey) DotPrefix(seg string) TOMLKey
- func (k TOMLKey) IsStatic() bool
- func (k TOMLKey) Jen() *jen.Statement
- func (k TOMLKey) JenLen() *jen.Statement
- func (k TOMLKey) Lit(s string) TOMLKey
- func (k TOMLKey) SegmentCount() int
- func (k TOMLKey) Static() string
- func (k TOMLKey) Var(name string) TOMLKey
- func (k TOMLKey) VarSuffix() string
- type TargetPath
- type TargetSeg
Constants ¶
This section is empty.
Variables ¶
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 OutputPath ¶ added in v0.4.2
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
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 ¶
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 SegKind ¶
type SegKind int
SegKind distinguishes field access from array index in a target path.
type StructInfo ¶
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 ¶
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 ¶
StaticKey creates a key from a dotted string (e.g. "servers.name"). An empty string produces an empty key.
func (TOMLKey) BareKey ¶
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 ¶
Dot appends a literal segment with a dot separator. If the key is empty, the segment is added without a leading dot.
func (TOMLKey) Jen ¶
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 ¶
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) SegmentCount ¶ added in v0.3.0
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 ¶
Static returns the key as a static string. Panics if the key contains dynamic parts (KeyVar or KeyPrefix).
func (TOMLKey) VarSuffix ¶
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").