config

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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

func Load(path string, dst any) error

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

func LoadDotEnv(path string) error

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

func LoadOrDefault(path string, dst any) error

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

func Override(dst any, lookup func(string) (string, bool), opts ...Option) error

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

type DotEnvPair struct {
	Key   string
	Value string
}

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

func WithPrefix(prefix string) Option

WithPrefix overrides DefaultPrefix used for implicit environment keys.

type Vars added in v0.2.0

type Vars map[string]string

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

func LoadVars(path string) (Vars, error)

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) Delete added in v0.2.0

func (v Vars) Delete(key string)

Delete removes a key.

func (Vars) EnvOverride added in v0.2.0

func (v Vars) EnvOverride(prefix string) Vars

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) Get added in v0.2.0

func (v Vars) Get(key string) string

Get returns the value for key, or empty string if not found.

func (Vars) GetOr added in v0.2.0

func (v Vars) GetOr(key, fallback string) string

GetOr returns the value for key when present and non-empty, otherwise fallback (KWL-2X1QZ CFG-KV-005).

func (Vars) Keys added in v0.2.0

func (v Vars) Keys() []string

Keys returns all keys in sorted order.

func (Vars) Merge added in v0.2.0

func (v Vars) Merge(other Vars) Vars

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

func (v Vars) Save(path string) error

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) Set added in v0.2.0

func (v Vars) Set(key, value string)

Set sets a key-value pair.

func (Vars) WithDefaults added in v0.2.0

func (v Vars) WithDefaults(defaults Vars) Vars

WithDefaults returns a copy of v filled with defaults for absent keys (KWL-2X1QZ CFG-KV-004).

Jump to

Keyboard shortcuts

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