cfg

package
v0.6.0-8a Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 5 Imported by: 0

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

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.

This first cut supports atomic wrappers (String, Integer, Number, Boolean) and nested struct recursion. Collection and Object wrappers are not yet handled.

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 Any

type Any struct {
	Value       any
	Description string
	Validate    Validator
}

type Boolean

type Boolean struct {
	Value       bool
	Description string
	Default     bool
}

type ConfigurationType

type ConfigurationType struct {
	Description string
	New         func() any
}

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 Integer

type Integer struct {
	Value       int64
	Description string
	Default     int64
	Validate    Validator
}

type List

type List[T Value] struct {
	Value       []T
	Description string
	Default     []T
	Element     T
	Validate    Validator
}

Element supplies the schema info (description, default, validator) applied to each item. Its own Value is ignored.

type Map

type Map[T Value] struct {
	Value       map[string]T
	Description string
	Default     map[string]T
	Element     T
	Validate    Validator
}

type Null

type Null struct {
	Description string
}

type Number

type Number struct {
	Value       float64
	Description string
	Default     float64
	Validate    Validator
}

type Object

type Object[T any] struct {
	Value       T
	Description string
	Validate    Validator
}

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 String

type String struct {
	Value       string
	Description string
	Default     string
	Validate    Validator
}

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

type ValidatorDesc struct {
	Kind   string
	Params map[string]any
}

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".

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value is the constraint for any type that may appear as the element of a List, Map, or Set, or as a standalone field on a Configuration. The unexported marker keeps the constraint closed to wrappers defined here.

Directories

Path Synopsis
Package validate provides standard Validator constructors for cfg configuration fields.
Package validate provides standard Validator constructors for cfg configuration fields.

Jump to

Keyboard shortcuts

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