Documentation
¶
Overview ¶
Package env provides explicit, batch-validated parsing of environment variables into typed Go values.
Unlike struct-tag binding libraries, every variable is read with an explicit function call, keeping the mapping between env vars and fields easy to read and grep. A Parser accumulates errors as it goes so a single Err() call reports every missing or malformed variable in one pass, rather than failing on the first one.
Values are read through a Lookuper, which decouples parsing from the process environment. The default source is the OS environment; tests and layered configuration can inject a MapLookuper instead, avoiding global state such as os.Setenv / t.Setenv.
Index ¶
- Constants
- func RequiredValidated[T any](p *Parser, key string, parse func(string) (T, error)) T
- func Validated[T any](p *Parser, key string, def T, parse func(string) (T, error)) T
- type Lookuper
- type MapLookuper
- type OSLookuper
- type Option
- type Parser
- func (p *Parser) AllOrNone(keys ...string)
- func (p *Parser) Bool(key string, def bool) bool
- func (p *Parser) CSV(key string, def []string) []string
- func (p *Parser) CSVSep(key, sep string, def []string) []string
- func (p *Parser) Duration(key string, def time.Duration) time.Duration
- func (p *Parser) Err() error
- func (p *Parser) Float64(key string, def float64) float64
- func (p *Parser) Int(key string, def int) int
- func (p *Parser) Int32(key string, def int32) int32
- func (p *Parser) Int64(key string, def int64) int64
- func (p *Parser) IntSlice(key string, def []int) []int
- func (p *Parser) MutuallyExclusive(keys ...string)
- func (p *Parser) OneOf(key, def string, allowed ...string) string
- func (p *Parser) PositiveDuration(key string, def time.Duration) time.Duration
- func (p *Parser) PositiveFloat64(key string, def float64) float64
- func (p *Parser) PositiveInt(key string, def int) int
- func (p *Parser) PositiveInt64(key string, def int64) int64
- func (p *Parser) RequiredBool(key string) bool
- func (p *Parser) RequiredCSV(key string) []string
- func (p *Parser) RequiredCSVSep(key, sep string) []string
- func (p *Parser) RequiredDuration(key string) time.Duration
- func (p *Parser) RequiredFloat64(key string) float64
- func (p *Parser) RequiredInt(key string) int
- func (p *Parser) RequiredInt32(key string) int32
- func (p *Parser) RequiredInt64(key string) int64
- func (p *Parser) RequiredIntSlice(key string) []int
- func (p *Parser) RequiredOneOf(key string, allowed ...string) string
- func (p *Parser) RequiredPositiveDuration(key string) time.Duration
- func (p *Parser) RequiredPositiveFloat64(key string) float64
- func (p *Parser) RequiredPositiveInt(key string) int
- func (p *Parser) RequiredPositiveInt64(key string) int64
- func (p *Parser) RequiredString(key string) string
- func (p *Parser) RequiredTime(key string) time.Time
- func (p *Parser) RequiredTimeLayout(key, layout string) time.Time
- func (p *Parser) RequiredUint(key string) uint
- func (p *Parser) RequiredUint64(key string) uint64
- func (p *Parser) RequiredWith(key string, dependents ...string)
- func (p *Parser) String(key, def string) string
- func (p *Parser) Time(key string, def time.Time) time.Time
- func (p *Parser) TimeLayout(key, layout string, def time.Time) time.Time
- func (p *Parser) Uint(key string, def uint) uint
- func (p *Parser) Uint64(key string, def uint64) uint64
Constants ¶
const DefaultSeparator = ","
DefaultSeparator is the delimiter used by CSV and IntSlice when no explicit separator is supplied.
Variables ¶
This section is empty.
Functions ¶
func RequiredValidated ¶
RequiredValidated returns the value of key converted by parse. If key is unset or parse returns an error, it records an error and returns the zero value of T.
func Validated ¶
Validated returns the value of key converted by parse, or def if key is unset or parse returns an error. A parse error on a present value fires the bad-default hook. It never records an error.
parse may both convert and validate: returning a non-nil error rejects the value. This is the generic escape hatch for types and rules the built-in accessors do not cover (e.g. *url.URL, net.IP, a bounded int).
It is a package-level function rather than a method because Go methods cannot declare their own type parameters.
Types ¶
type Lookuper ¶
type Lookuper interface {
// Lookup returns the value for key and whether key was set. A set key with
// an empty value returns ("", true).
Lookup(key string) (value string, ok bool)
}
Lookuper resolves an environment variable key to its raw string value and reports whether the key was present at all. Distinguishing "present but empty" from "absent" is intentional: some callers treat KEY="" as a meaningful explicit value, others as unset (see WithEmptyAsUnset).
type MapLookuper ¶
MapLookuper reads from an in-memory map. A nil or missing entry is reported as unset. It is intended for tests and for composing layered configuration without touching the real environment.
type OSLookuper ¶
type OSLookuper struct{}
OSLookuper reads from the process environment via os.LookupEnv. It is the default source used by New.
type Option ¶
type Option func(*Parser)
Option configures a Parser.
func WithBadDefaultHook ¶
WithBadDefaultHook registers a callback invoked when a defaulted accessor finds a present value that fails to parse. The accessor still returns its default; the hook lets callers log the typo instead of silently swallowing it. fn is called with the key, the raw value, and the parse error.
func WithEmptyAsUnset ¶
func WithEmptyAsUnset() Option
WithEmptyAsUnset makes a set-but-empty variable (KEY="") be treated as if the variable were absent: required accessors report it missing, and defaulted accessors return their default. This restores the semantics of callers that consider an empty value meaningless.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser reads environment variables through a Lookuper and accumulates any errors encountered. Required accessors that fail append an error; defaulted accessors never append an error but report malformed values through the onBadDefault hook before falling back to the supplied default.
The zero value is not usable; construct a Parser with New or From.
A Parser is not safe for concurrent use. Typical usage builds a config struct from a single goroutine at startup and then checks Err once.
func (*Parser) AllOrNone ¶ added in v0.2.0
AllOrNone records one error unless the named keys are all set or all unset. It is the constraint for values that only make sense together, such as a TLS certificate and its private key. The error names every key and lists which of them are set. It respects WithEmptyAsUnset: a set-but-empty variable counts as unset. Zero or one key always satisfies the constraint and records nothing.
func (*Parser) Bool ¶
Bool returns the value of key parsed as a bool, or def if key is unset or malformed. A malformed value fires the bad-default hook. Accepted values are those of strconv.ParseBool: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
func (*Parser) CSV ¶
CSV returns the value of key split on DefaultSeparator, with each element trimmed of surrounding whitespace and empty elements dropped. It returns def if key is unset. If key is set but yields no non-empty elements, an empty (non-nil) slice is returned rather than def — a set value always overrides the default. It never records an error.
func (*Parser) Duration ¶
Duration returns the value of key parsed as a time.Duration (e.g. "300ms", "1.5h", "2h45m"), or def if key is unset or malformed. A malformed value fires the bad-default hook.
func (*Parser) Err ¶
Err returns the accumulated errors joined into a single error, or nil if no required accessor has failed. Call it once after reading all variables.
func (*Parser) Float64 ¶
Float64 returns the value of key parsed as a float64, or def if key is unset or malformed. A malformed value fires the bad-default hook.
func (*Parser) Int ¶
Int returns the value of key parsed as a base-10 int, or def if key is unset or malformed. A malformed value invokes the bad-default hook (if set) and is otherwise silently replaced by def.
func (*Parser) Int32 ¶
Int32 returns the value of key parsed as a base-10 int32, or def if key is unset or malformed (including overflow). A malformed value fires the bad-default hook.
func (*Parser) Int64 ¶
Int64 returns the value of key parsed as a base-10 int64, or def if key is unset or malformed. A malformed value fires the bad-default hook.
func (*Parser) IntSlice ¶
IntSlice returns the value of key split on DefaultSeparator with each element parsed as a base-10 int. It returns def if key is unset. A malformed element fires the bad-default hook and causes def to be returned. It never records an error.
func (*Parser) MutuallyExclusive ¶ added in v0.2.0
MutuallyExclusive records one error when more than one of the named keys is set. It is the constraint for values that name competing sources of the same thing, such as two alternative binding stores. The error names every key and lists which of them are set. It respects WithEmptyAsUnset: a set-but-empty variable counts as unset. Zero or one key never conflicts and records nothing.
func (*Parser) OneOf ¶
OneOf returns the value of key if it is one of allowed, otherwise def. It returns def when key is unset. When key is set to a value outside allowed, the bad-default hook fires and def is returned. It never records an error.
def is not required to be a member of allowed; callers are responsible for supplying a sensible default.
func (*Parser) PositiveDuration ¶ added in v0.3.0
PositiveDuration returns the value of key parsed as a time.Duration greater than zero, or def if key is unset, malformed, or not positive. A rejected present value fires the bad-default hook.
func (*Parser) PositiveFloat64 ¶ added in v0.3.0
PositiveFloat64 returns the value of key parsed as a float64 greater than zero, or def if key is unset, malformed, or not positive. A rejected present value fires the bad-default hook.
func (*Parser) PositiveInt ¶ added in v0.3.0
PositiveInt returns the value of key parsed as a base-10 int greater than zero, or def if key is unset, malformed, or not positive. A rejected present value fires the bad-default hook.
func (*Parser) PositiveInt64 ¶ added in v0.3.0
PositiveInt64 returns the value of key parsed as a base-10 int64 greater than zero, or def if key is unset, malformed, or not positive. A rejected present value fires the bad-default hook.
func (*Parser) RequiredBool ¶
RequiredBool returns the value of key parsed as a bool. If key is unset or malformed it records an error and returns false.
func (*Parser) RequiredCSV ¶
RequiredCSV returns the value of key split like CSV. If key is unset it records a missing error and returns nil. A key that is set but yields no non-empty elements is not an error and returns an empty slice.
func (*Parser) RequiredCSVSep ¶
RequiredCSVSep behaves like RequiredCSV but splits on the supplied separator.
func (*Parser) RequiredDuration ¶
RequiredDuration returns the value of key parsed as a time.Duration. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredFloat64 ¶
RequiredFloat64 returns the value of key parsed as a float64. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredInt ¶
RequiredInt returns the value of key parsed as a base-10 int. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredInt32 ¶
RequiredInt32 returns the value of key parsed as a base-10 int32. If key is unset or malformed (including overflow) it records an error and returns 0.
func (*Parser) RequiredInt64 ¶
RequiredInt64 returns the value of key parsed as a base-10 int64. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredIntSlice ¶
RequiredIntSlice returns the value of key split on DefaultSeparator with each element parsed as a base-10 int. If key is unset, or any element fails to parse, it records an error and returns nil.
func (*Parser) RequiredOneOf ¶
RequiredOneOf returns the value of key if it is one of allowed. If key is unset it records a missing error; if key is set to a value outside allowed it records a validation error. On failure it returns "".
func (*Parser) RequiredPositiveDuration ¶ added in v0.3.0
RequiredPositiveDuration returns the value of key parsed as a time.Duration greater than zero. If key is unset, malformed, or not positive it records an error and returns 0.
func (*Parser) RequiredPositiveFloat64 ¶ added in v0.3.0
RequiredPositiveFloat64 returns the value of key parsed as a float64 greater than zero. If key is unset, malformed, or not positive it records an error and returns 0.
func (*Parser) RequiredPositiveInt ¶ added in v0.3.0
RequiredPositiveInt returns the value of key parsed as a base-10 int greater than zero. If key is unset, malformed, or not positive it records an error and returns 0.
func (*Parser) RequiredPositiveInt64 ¶ added in v0.3.0
RequiredPositiveInt64 returns the value of key parsed as a base-10 int64 greater than zero. If key is unset, malformed, or not positive it records an error and returns 0.
func (*Parser) RequiredString ¶
RequiredString returns the value of key. If key is unset it records a missing error and returns "".
func (*Parser) RequiredTime ¶
RequiredTime returns the value of key parsed as a time.Time using RFC 3339. If key is unset or malformed it records an error and returns the zero time.
func (*Parser) RequiredTimeLayout ¶
RequiredTimeLayout returns the value of key parsed as a time.Time using the supplied reference layout. If key is unset or malformed it records an error and returns the zero time.
func (*Parser) RequiredUint ¶
RequiredUint returns the value of key parsed as a base-10 unsigned int. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredUint64 ¶
RequiredUint64 returns the value of key parsed as a base-10 uint64. If key is unset or malformed it records an error and returns 0.
func (*Parser) RequiredWith ¶ added in v0.2.0
RequiredWith records one error per unset dependent when key is set. It is the constraint for values that become required once a feature key turns the feature on. When key is unset the dependents are not checked and nothing is recorded. It respects WithEmptyAsUnset: a set-but-empty key or dependent counts as unset.
func (*Parser) String ¶
String returns the value of key, or def if key is unset (or empty when WithEmptyAsUnset is in effect). It never records an error.
func (*Parser) Time ¶
Time returns the value of key parsed as a time.Time using RFC 3339, or def if key is unset or malformed. A malformed value fires the bad-default hook. Use TimeLayout for a non-RFC3339 layout.
func (*Parser) TimeLayout ¶
TimeLayout returns the value of key parsed as a time.Time using the supplied reference layout (see the time package), or def if key is unset or malformed.