Documentation
¶
Overview ¶
Package gen defines the directive contract and render tree for main.gen.go.
Directive lifecycle ¶
A directive parses annotations, validates typed targets, and emits nodes. Meta declares syntax, docs, completions, and emission tier.
The generation tree ¶
Emit appends typed nodes such as ConfigLoad, Call, StructLit, Select, Route, Serve, and Assign. Raw holds preformatted lines when no typed node fits. Base records origin, phase, optional label, and manual dependencies.
Rendering policies ¶
The renderer groups nodes by phase, keeps dependency clusters contiguous, orders independent clusters by source anchor, and emits deterministic Go.
Index ¶
- func EmbedCovers(a Annotation, pattern string) (found, covered bool)
- func EnsureStruct(g *Gen, fset *token.FileSet, t types.Type) (string, diag.Diagnostics)
- func IsContext(t types.Type) bool
- func LowerFirst(s string) string
- func RegisterStruct(g *Gen, fset *token.FileSet, t types.Type)
- type Annotation
- type Arg
- type Args
- type Assign
- type AttrSpec
- type Base
- type Call
- type Case
- type ConfigLoad
- type Directive
- type EmitTier
- type ErrStyle
- type Field
- type Finisher
- type Gen
- func (g *Gen) AddMissingHint(f func(types.Type) (string, bool))
- func (g *Gen) Bind(t types.Type, name, expr string)
- func (g *Gen) BindLazy(t types.Type, name string, build func() (string, diag.Diagnostics))
- func (g *Gen) BindLazyPath(path string, build func() (string, diag.Diagnostics))
- func (g *Gen) BindPath(path, expr string)
- func (g *Gen) Context() string
- func (g *Gen) HasBinding(t types.Type, name string) bool
- func (g *Gen) HasBindingPath(path string) bool
- func (g *Gen) HasProviderBinding(t types.Type, name string) bool
- func (g *Gen) HasSingleton(key string) bool
- func (g *Gen) Import(path string) string
- func (g *Gen) ImportPkg(p *types.Package) string
- func (g *Gen) Instance(t types.Type, name string) (string, diag.Diagnostics, bool)
- func (g *Gen) InstancePath(path string) (string, diag.Diagnostics, bool)
- func (g *Gen) LookupType(pkgPath, name string) (types.Type, bool)
- func (g *Gen) MissingHint(t types.Type) (string, bool)
- func (g *Gen) Module() string
- func (g *Gen) Node(n Node)
- func (g *Gen) Render() ([]byte, error)
- func (g *Gen) SetDirective(name string)
- func (g *Gen) SetModule(path string)
- func (g *Gen) SetTypes(m map[string]*types.Package)
- func (g *Gen) Singleton(key, varName, ctor string) string
- func (g *Gen) SingletonIn(phase Phase, key, varName, ctor string) string
- func (g *Gen) Stmt(phase Phase, format string, a ...any)
- func (g *Gen) TypeExpr(t types.Type) string
- func (g *Gen) Var(base string) string
- type Hinter
- type Meta
- type Node
- type NodePreparer
- type Origin
- type Parsed
- type Phase
- type PosSpec
- type Raw
- type Route
- type RouteKind
- type Select
- type Serve
- type StructLit
- type Typed
- type Validator
- type ValueKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmbedCovers ¶
func EmbedCovers(a Annotation, pattern string) (found, covered bool)
EmbedCovers scans the annotation's comment group for //go:embed lines. found reports whether any exists; covered reports whether one of their patterns equals pattern. Directives anchored to embed.FS variables use it to require the all: form, wording their own diagnostics.
func EnsureStruct ¶
EnsureStruct returns the wired expression for a handler struct.
func LowerFirst ¶
LowerFirst lowercases the leading rune, for deriving generated identifiers from exported Go names.
Types ¶
type Annotation ¶
type Annotation struct {
Name string // keyword after //fabrik:
Args string // raw text after the keyword
Pos token.Position // position of the directive comment
ArgsCol int // column where Args starts on the directive line
Decl ast.Node // annotated declaration: *ast.FuncDecl or *ast.TypeSpec
Doc []string // every line of the enclosing comment group, verbatim
}
Annotation is the syntactic view of one directive comment.
type Args ¶
Args contains positional arguments and key=value options.
func ParseArgs ¶
func ParseArgs(a Annotation, m Meta) (Args, diag.Diagnostics)
ParseArgs applies Meta's argument shape without validating value semantics.
type AttrSpec ¶
type AttrSpec struct {
Key string
Kind ValueKind
Values []string // completion candidates; a closed set only when Kind is KindEnum
Required bool
}
AttrSpec describes one key=value option.
type Base ¶
type Base struct {
Origin Origin
Phase Phase // run() section; child nodes inherit their parent's phase
Label string // optional one-line comment above the node
Uses []string // manual dependency additions; rendered text supplies the rest
}
Base is shared node metadata.
type Call ¶
type Call struct {
Base
Var string // assigned variable; "" for a bare call
Fn string // rendered callee, e.g. "shared.InitLogger" or "r.Use"
Args []string
Err ErrStyle
}
Call invokes a constructor or registration function.
type Case ¶
type Case struct {
Value string
Body []Node // e.g. branch-local config loads
Result Call // Var set when the constructor returns an error
}
Case is one selection arm: branch-local work, then the constructor.
type ConfigLoad ¶
type ConfigLoad struct {
Base
Var string
Pkg string // config package alias
Type string // rendered struct type
Options []string // rendered option expressions
}
ConfigLoad loads one configuration struct.
type Directive ¶
type Directive interface {
Name() string
Meta() Meta
Parse(Annotation) (any, diag.Diagnostics)
Check(node any, t Typed) diag.Diagnostics
Emit(node any, g *Gen) diag.Diagnostics
}
Directive implements one //fabrik:NAME annotation.
type Finisher ¶
type Finisher interface {
Finish(g *Gen) diag.Diagnostics
}
Finisher runs after all nodes emit and may append more code.
type Gen ¶
type Gen struct {
// contains filtered or unexported fields
}
Gen assembles main.gen.go from imports, DI bindings, and phased statements.
func (*Gen) AddMissingHint ¶
AddMissingHint registers a missing-binding help source.
func (*Gen) BindLazyPath ¶
func (g *Gen) BindLazyPath(path string, build func() (string, diag.Diagnostics))
BindLazyPath registers a lazy binding matched by a printed type path.
func (*Gen) BindPath ¶
BindPath publishes an expression while its lazy path binding is materializing.
func (*Gen) Context ¶
Context marks the lifecycle context as needed and returns its variable; Render emits the assignment as run()'s first statement.
func (*Gen) HasBinding ¶
HasBinding reports whether (t, name) has an eager or lazy binding, without materializing anything.
func (*Gen) HasBindingPath ¶
HasBindingPath reports whether a path-keyed binding exists, lazy or already materialized.
func (*Gen) HasProviderBinding ¶
HasProviderBinding reports whether (t, name) has a provider-owned lazy binding, excluding library path bindings.
func (*Gen) HasSingleton ¶
HasSingleton reports whether key has been created.
func (*Gen) InstancePath ¶
InstancePath resolves a path-keyed lazy binding.
Diagnosed path builds are not cached.
func (*Gen) LookupType ¶
LookupType returns a named type from a type-checked imported package.
func (*Gen) MissingHint ¶
MissingHint returns the first domain help that applies to t.
func (*Gen) SetDirective ¶
SetDirective records the directive currently emitting code.
func (*Gen) SetTypes ¶
SetTypes supplies type-checked packages for Gen.LookupType.
func (*Gen) SingletonIn ¶
SingletonIn emits the shared variable in a specific phase.
type Meta ¶
type Meta struct {
Synopsis string // one line, shown as completion detail
Doc string // markdown, shown on hover
Example string // usage shown in diagnostics help
Pos []PosSpec // required positional arguments, in order
Attrs []AttrSpec // key=value options
Tier EmitTier // when Emit runs relative to other directives
}
Meta describes directive syntax, docs, completions, and lifecycle.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is one emission in the generation tree.
type NodePreparer ¶
NodePreparer registers bindings before dependency resolution.
type Phase ¶
type Phase int
Phase orders generated statements inside run().
const ( PhaseConfig Phase = iota // configuration loading, before all else PhaseSetup // setup hooks: config exists, providers do not PhaseWire // construct app and runtime values PhaseMiddleware // global middleware registration PhaseRegister // register generated behavior onto constructed values PhasePrepare // prepare hooks: pre-intake work on registered resources PhaseStart // start hooks: start background runtime processes PhaseServe // start serving, ending with return )
type PosSpec ¶
type PosSpec struct {
Name string // e.g. "METHOD"; used in messages and docs
Kind ValueKind // completion hint; values are validated by Parse
Values []string // completion candidates; a closed set only when Kind is KindEnum
Optional bool
}
PosSpec describes one positional argument.
type Raw ¶
type Raw struct {
Base
Lines []string
Defines []string // variables the lines declare, for dependency ordering
}
Raw is a preformatted emission.
type Route ¶
type Route struct {
Base
Router string
Kind RouteKind
Method string
Pattern string
Handler string // rendered handler expression
Chain []string // middleware expressions, outermost first
}
Route registers one route on the router.
type Select ¶
type Select struct {
Base
Var string // the interface variable being assigned
Iface string // rendered interface type
KeyExpr string // e.g. "webConfig.Kind"
FmtPkg string // fmt alias, for the unmatched-value arm
Cases []Case
}
Select wires the implementation a configuration value names.
type StructLit ¶
type StructLit struct {
Base
Var string
Type string // rendered struct type, without the &
Fields []Field
}
StructLit constructs a pointer to a struct with injected fields.
type Typed ¶
type Typed struct {
Target types.Object // the annotated func/method/type, fully typed
Fset *token.FileSet // resolves positions of typed objects
}
Typed is the semantic view of an annotation.
type Validator ¶
type Validator interface {
Validate(g *Gen) diag.Diagnostics
}
Validator observes completed generation without emitting code.