codegen

package
v0.5.34 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package codegen emits reflection-free apply functions from intermediate minitoml.Document keys.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmitApply added in v0.5.34

func EmitApply(b *bytes.Buffer, spec Spec, opts ApplyOptions) error

EmitApply writes one apply function into b. The imports it needs are fmt, minitoml, and time when SpecsNeedTime reports it; the caller owns the file header, so a pass that emits other things beside the function can.

func Generate

func Generate(packageName string, specs []Spec) ([]byte, error)

Generate emits Go source with Apply* functions that map minitoml.Document keys onto structs. Key paths are resolved at generation time; generated code does not use reflection or tag parsing.

func SpecsNeedTime added in v0.5.34

func SpecsNeedTime(specs []Spec) bool

SpecsNeedTime reports whether the apply functions for specs import time, which a duration field does.

Types

type ApplyOptions added in v0.5.34

type ApplyOptions struct {
	// FuncName is the emitted function's name. Empty means Apply<TypeName>.
	FuncName string
	// ErrorPrefix opens every error the function returns. Empty means minitoml.
	ErrorPrefix string
	// AllowEmptyPrefix admits a Spec whose Prefix is empty, so keys are read
	// from the document root rather than under a table.
	AllowEmptyPrefix bool
}

ApplyOptions reshapes one emitted apply function for a caller that is not configbind. The zero value emits the Apply<TypeName> form the fixture pins.

type Field

type Field struct {
	// GoName is the exported Go field name.
	GoName string
	// Key is the relative TOML/intermediate key segment under the parent prefix.
	Key string
	// Kind is the field type.
	Kind FieldKind
	// Nested holds child fields when Kind is FieldStruct or FieldStructSlice.
	// For FieldStructSlice they are the element struct's fields.
	Nested []Field
	// ElemType is the element struct's Go type name when Kind is FieldStructSlice.
	ElemType string
	// GoType is the Go type an integer field is assigned as (int64, uint32, or
	// a defined type over one of them). Empty means int, which keeps the
	// generated form of a plain int field unchanged. For FieldFloat it is
	// float64 or float32, and empty means float64.
	GoType string
	// Default is an optional default used when the key is absent (string form).
	Default string
	// Opt is an optional CLI name override ("long" or "long,short").
	Opt string
	// Env is an optional exact environment variable override, or "-" to disable it.
	Env string
	// Help is optional CLI/scaffold help text.
	Help string
	// Arg is a subcommand-only positional role: required, optional, or *.
	Arg string
	// DependsOn is the condition from a dependon tag: a parent config key, and
	// optionally an operator and the values it selects. This field is omitted from
	// provenance output while the condition fails.
	DependsOn string
	// Falsy is the enum choice from a falsy tag that means "off" for this field.
	// An empty value resolves to it, and it hides fields that depend on this one.
	Falsy string
	// Enum is the raw allowlist from an enum tag. configbind reads it to check the
	// values a dependon condition names against the choices the field can hold.
	Enum string
	// Secret is the disclosure mode from a secret tag: hide, mask, or show. On
	// a nested struct it covers every field of the subtree.
	Secret string
	// Summary is the rating from a summary tag: omit, meaning a short surface may
	// drop this key while nothing has set it. On a nested struct it covers every
	// field of the subtree.
	Summary string
}

Field describes one struct field for intermediate-form apply generation.

type FieldKind

type FieldKind int

FieldKind is the Go field kind supported by the generator.

const (
	// FieldString is a string field.
	FieldString FieldKind = iota
	// FieldBool is a bool field.
	FieldBool
	// FieldInt is an int field.
	FieldInt
	// FieldDuration is a time.Duration field parsed from a Go duration string.
	FieldDuration
	// FieldStringSlice is a []string field.
	FieldStringSlice
	// FieldStruct is a nested struct field.
	FieldStruct
	// FieldStructSlice is a slice of structs read from an array of tables.
	FieldStructSlice
	// FieldFloat is a float64 or float32 field. A TOML integer widens into it.
	FieldFloat
)

type IntFieldSpec added in v0.2.1

type IntFieldSpec struct {
	// Name is the Go type the generated code converts to.
	Name string
	// Signed selects strconv.ParseInt over strconv.ParseUint.
	Signed bool
	// BitSize is the strconv bit size. Zero means the platform-sized int or
	// uint, which is the value strconv itself takes for that case, so the
	// accepted range follows the build target instead of the generator host.
	BitSize int
}

IntFieldSpec describes how one integer field is parsed and assigned.

func IntFieldSpecOf added in v0.2.1

func IntFieldSpecOf(goType string) (IntFieldSpec, error)

IntFieldSpecOf resolves a Field.GoType. An empty name means int, so a field built before widths were carried keeps its exact generated form.

func (IntFieldSpec) ParseLiteral added in v0.2.1

func (s IntFieldSpec) ParseLiteral(text string) (string, error)

ParseLiteral reads one default value at the field's own width. The returned text is the value re-rendered as an untyped Go integer literal.

type Spec

type Spec struct {
	// TypeName is the Go struct type name (e.g. WebServiceConfig).
	TypeName string
	// Prefix is the Bind prefix / top-level TOML table (e.g. webservice).
	Prefix string
	// Fields are the struct fields.
	Fields []Field
}

Spec describes one Bind-style config struct and its prefix table name.

func WebServiceFixtureSpec

func WebServiceFixtureSpec() Spec

WebServiceFixtureSpec returns the representative Bind-style fixture used by tests and committed gen code.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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