Documentation
¶
Overview ¶
Package cfg is the typed vocabulary a Go library uses to declare its configuration. Each wrapper carries the decoded value alongside its schema info (description, default, validation) for one field. Only wrappers in this package satisfy the Value constraint, so a library author cannot place a bare Go type as the element of a List, Map, or Set.
Index ¶
- func Decode(ct *ConfigurationType, raw map[string]any) (any, error)
- func ValidateConfigurationType(ct *ConfigurationType) error
- type Any
- type Boolean
- type ConfigurationType
- type Field
- type Integer
- type List
- type Map
- type Null
- type Number
- type Object
- type String
- type Validator
- type ValidatorDesc
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode(ct *ConfigurationType, raw map[string]any) (any, error)
Decode populates a fresh instance returned by ct.New() with values from raw. Field names match in kebab form (AssumeRole reads from "assume-role"). A non-pointer wrapper field is required; a pointer to a wrapper is optional and falls back to the wrapper's Default when the key is absent. An unknown key in raw is an error.
Atomic wrappers (String, Integer, Number, Boolean), List, Map, Object, and nested struct recursion are supported. A struct composes another configuration through a named field only; an anonymous field is an error, so a field is never silently keyed by its type name.
func ValidateConfigurationType ¶
func ValidateConfigurationType(ct *ConfigurationType) error
ValidateConfigurationType verifies that every reachable field in a fresh New() instance is a wrapper from this package or a struct whose fields recurse to wrappers. Call this from a library's unit tests to catch misuse at go-test time. The runtime calls it at library load to fail fast on a misdeclared configuration.
Types ¶
type ConfigurationType ¶
ConfigurationType registers a library's configuration. New returns a pointer to a fresh configuration struct. The decoder populates its fields from the operator's config.ub. The struct's fields must be wrapper types from this package or nested structs whose fields are wrapper types; the schema walker rejects any other field type at library load. Description appears in the schema commands.
type Field ¶ added in v0.6.0
type Field struct {
Name string
Type string
Optional bool
Description string
// Fields lists an object-typed field's own fields in declaration
// order, so help output can show nested structure. Empty for any
// other type.
Fields []Field
}
Field describes one field of a configuration struct, for the factory's own help output.
func Describe ¶ added in v0.6.0
func Describe(ct *ConfigurationType) []Field
Describe lists the fields of the configuration struct behind ct in declaration order, walking the value New returns the same way the decoder does: kebab-case names, a pointer field is optional, and a nested struct reads as an object whose own fields fill Fields. Anonymous fields are skipped here; Decode rejects them. Descriptions come from whatever the zero value sets. Returns nil when there is no configuration to describe.
type List ¶
Element supplies the schema info (description, default, validator) applied to each item. Its own Value is ignored.
type Object ¶
Object wraps a user struct in a position that requires a Value, such as the element type of a List or Map. T must be a struct whose fields are wrapper types or nested structs; the schema walker rejects any other field type at library load. An optional nested struct at the top level of a Configuration uses a plain *Struct and does not need this wrapper.
type Validator ¶
type Validator interface {
Check(any) error
Describe() ValidatorDesc
}
Validator runs after decode to check that a value meets a library author's constraints. Describe returns a structured form so the schema commands and editor tooling can render the constraint declaratively rather than as an opaque function.
type ValidatorDesc ¶
ValidatorDesc names a validator's kind and parameters. Standard kinds include "pattern", "range", "length", "enum", "all", and "custom"; introspection tools render the known kinds to their UB modifier and fall back to the description text for "custom".