Documentation
¶
Overview ¶
Package config provides a first-class configuration loader that binds environment variables, config files, and secret sources into typed Go structs with validation.
Apps currently roll their own os.Getenv calls. This package removes that class of bugs with a single typed binding step.
Usage:
type AppConfig struct {
Port int `config:"PORT" default:"8080" validate:"min=1,max=65535"`
DBURL string `config:"DATABASE_URL" required:"true"`
Debug bool `config:"DEBUG" default:"false"`
LogLevel string `config:"LOG_LEVEL" default:"info" validate:"oneof=debug info warn error"`
}
var cfg AppConfig
if err := config.Load(&cfg); err != nil {
log.Fatal(err)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load populates the config struct from the given sources (defaulting to EnvSource if none are provided). Struct fields use `config:"KEY"` tags to specify the source key, `default:"VALUE"` for defaults, and `required:"true"` for mandatory fields.
Nested struct fields recurse with a SCREAMING_SNAKE prefix derived from the parent field name (e.g. `DB DBConfig` reads keys like DB_HOST).
Fields tagged `sensitive:"true"` have their raw value redacted from any error messages.
If the config (or a pointer to it) implements ConfigValidator, Validate is called after binding and its error is returned.
Supported field types: string, int, int64, float64, bool, Duration, and nested structs.
Types ¶
type ChainedSource ¶
type ChainedSource []Source
ChainedSource tries multiple sources in order, returning the first hit.
type ConfigValidator ¶
type ConfigValidator interface {
Validate() error
}
ConfigValidator is implemented by config structs that want a post-binding validation hook. Validate runs after every field has been populated; returning a non-nil error aborts Load.
Renamed from Validator to avoid collision with entity.Entity's own Validate() error method — a config struct that doubled as an entity would otherwise accidentally satisfy this interface.
type EnvSource ¶
type EnvSource struct{}
EnvSource reads from environment variables. This is the default source.
type Source ¶
type Source interface {
// Get returns the value for the given key, or ("", false) if not found.
Get(key string) (string, bool)
}
Source provides configuration values. The default source reads from environment variables. Custom sources (files, secret managers, etc.) implement this interface.