Documentation
¶
Overview ¶
Package config provides typed configuration loading from YAML files with environment-variable overlay for the Krewire ecosystem.
Precedence is strictly: built-in zero values < file < environment. Later sources win.
Index ¶
- Constants
- func Load(path string, dst any) error
- func LoadDotEnv(path string) error
- func LoadOrDefault(path string, dst any) error
- func Override(dst any, lookup func(string) (string, bool), opts ...Option) error
- type DotEnvPair
- type Option
- type Vars
- func (v Vars) Delete(key string)
- func (v Vars) EnvOverride(prefix string) Vars
- func (v Vars) Get(key string) string
- func (v Vars) GetOr(key, fallback string) string
- func (v Vars) Keys() []string
- func (v Vars) Merge(other Vars) Vars
- func (v Vars) Save(path string) error
- func (v Vars) Set(key, value string)
- func (v Vars) WithDefaults(defaults Vars) Vars
Constants ¶
const DefaultPrefix = "APP"
DefaultPrefix is prepended to implicit environment keys derived from field names. Fields tagged `env:"KEY"` are always read from the exact key.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load reads the YAML file at path and unmarshals it into dst (a non-nil pointer to a struct), binding values by `yaml` tag or lowercased field name. A missing file leaves dst at its zero value and returns nil; a malformed file returns a wrapped error naming the path.
func LoadDotEnv ¶ added in v0.2.0
LoadDotEnv parses the .env file at path and exports its KEY=VALUE pairs into the process environment (KWL-2X1QZ CFG-DOTV-001). Variables already present in the process environment are never overwritten, so the real environment wins over .env. A missing file is not an error.
func LoadOrDefault ¶
LoadOrDefault behaves like Load and is provided for tooling that tolerates an absent configuration file: a missing file yields the struct's zero value and never an error.
func Override ¶
Override applies environment values on top of an already-loaded struct. A field tagged `env:"KEY"` is read from the exact key; an untagged field derives an implicit key from its yaml tag/name uppercased with `-` and `.` replaced by `_`, prefixed by DefaultPrefix (or the prefix from WithPrefix), joining nested struct segments with `_` (e.g. `Server.Addr` -> APP_SERVER_ADDR). Empty environment values are treated as unset. Slices and maps are not overlaid in this phase.
Types ¶
type DotEnvPair ¶ added in v0.2.0
DotEnvPair is one KEY=VALUE entry parsed from a .env file.
func ParseDotEnv ¶ added in v0.2.0
func ParseDotEnv(data []byte) ([]DotEnvPair, error)
ParseDotEnv parses .env file content into ordered KEY=VALUE pairs. Blank lines and # comments are skipped; an optional "export " prefix is accepted; values may be wrapped in single or double quotes and unquoted values drop trailing inline comments (KWL-2X1QZ CFG-DOTV-002).
type Option ¶
type Option func(*options)
Option configures loading behavior.
func WithPrefix ¶
WithPrefix overrides DefaultPrefix used for implicit environment keys.
type Vars ¶ added in v0.2.0
Vars is a flat key-value configuration store with dot-notation keys, loadable from and saveable to YAML files (KWL-2X1QZ CFG-KV-001).
func LoadVars ¶ added in v0.2.0
LoadVars reads the YAML file at path and flattens it into Vars using dot-notation keys. A missing file or empty path yields an empty Vars and nil error; a malformed file yields a wrapped error naming the path (KWL-2X1QZ CFG-KV-002).
func (Vars) EnvOverride ¶ added in v0.2.0
EnvOverride applies environment variables with the given prefix on top of v. Keys are lower-cased with underscores replaced by dots, e.g. APP_SERVER_PORT=8080 -> server.port (KWL-2X1QZ CFG-KV-004).
func (Vars) GetOr ¶ added in v0.2.0
GetOr returns the value for key when present and non-empty, otherwise fallback (KWL-2X1QZ CFG-KV-005).
func (Vars) Merge ¶ added in v0.2.0
Merge merges other into a copy of v, with other taking precedence (KWL-2X1QZ CFG-KV-004).
func (Vars) Save ¶ added in v0.2.0
Save writes the Vars map to a YAML file as a nested structure, creating parent directories as needed (KWL-2X1QZ CFG-KV-003).
func (Vars) WithDefaults ¶ added in v0.2.0
WithDefaults returns a copy of v filled with defaults for absent keys (KWL-2X1QZ CFG-KV-004).