Documentation
¶
Overview ¶
Package configo fills a configuration struct from any number of value sources — environment variables, CLI arguments, in-memory maps, or anything implementing Source.
Every field declares, per source, the key it should be read from. There is no central key schema and no naming convention: the struct tag is the mapping.
type Config struct {
Host string `configo:"env=APP_HOST,cli=--host,default=localhost"`
}
The configo tag ¶
A tag is a comma separated list of sourceTag=key pairs. The sourceTag selects the source and must match that source's Source.Tag; the key is the lookup key for that source alone, so each source keeps its own naming convention. A source a field does not mention is skipped for that field. Fields without a configo tag and unexported fields are ignored. A pair without "=" is a malformed tag and Parse returns an error.
Priorities ¶
Parser.Parse walks the sources in registration order and assigns every value it finds, so the last source that resolves a field wins. Order the sources from lowest to highest priority:
parser := configo.NewParser(
configo.WithSources(
configo.Default, // lowest
configo.NewMapSource("file", fromYAML),
configo.Environment,
configo.NewCliSource("cli", os.Args[1:]), // highest
),
)
var cfg Config
if err := parser.Parse(&cfg); err != nil {
log.Fatal(err)
}
Fields that no source can supply are collected and reported together, so a misconfigured deployment fails once with a complete list instead of one key at a time. A source that finds a value but fails to convert it (for example APP_PORT=abc for an int field) aborts immediately with that error.
The package level Parse uses Default followed by Environment, so environment variables override defaults.
Built-in sources ¶
- Environment (tag "env") reads os.LookupEnv.
- Default (tag "default") echoes the key back as the value, so default=8080 is the literal 8080.
- NewCliSource parses command line arguments.
- NewMapSource serves pre-typed values from a map.
Supported field types ¶
bool, all sized and unsized int/uint variants, float32, float64, string, pointers to those, slices of those, and maps with string keys and those element types. Pointer fields are allocated on demand. Any other type (channels, functions, pointers to structs, non-string map keys) is rejected with an error.
String based sources split collections on commas; maps additionally split each entry on the first "=":
type Config struct {
Hosts []string `configo:"env=APP_HOSTS"` // APP_HOSTS=a,b,c
Weights map[string]int `configo:"env=APP_WEIGHTS"` // APP_WEIGHTS=a=1,b=2
}
Nested structs ¶
A tagged struct field contributes its key to a parent path that is handed to the source alongside the leaf key, letting hierarchical sources address nested values:
type Database struct {
Host string `configo:"yaml=host"`
}
type Config struct {
Database Database `configo:"yaml=database"`
}
Here the source is asked for key "host" with parents ["database"]. It is up to the source to decide what that means — joining it into "database.host", walking a tree, and so on. Environment, Default and the source returned by NewCliSource ignore the parent path and look up the leaf key directly.
Custom sources ¶
Implement Source to plug in a new backend. A source reports a missing key by returning ErrNotFoundInSource, which is what makes Parser.Parse fall through to the next source. For a fundamentally string based backend — files, remote key-value stores, templates — NewStringSource does all the parsing and conversion and only needs a LookupFunc; that is how Environment and NewCliSource are built.
Index ¶
- Variables
- func Parse(cfg any) error
- type LookupFunc
- type MapSource
- func (m MapSource) GetBool(key string, parents []string) (bool, error)
- func (m MapSource) GetBoolMap(key string, parents []string) (map[string]bool, error)
- func (m MapSource) GetBoolSlice(key string, parents []string) ([]bool, error)
- func (m MapSource) GetFloat(key string, parents []string) (float64, error)
- func (m MapSource) GetFloatMap(key string, parents []string) (map[string]float64, error)
- func (m MapSource) GetFloatSlice(key string, parents []string) ([]float64, error)
- func (m MapSource) GetInt(key string, parents []string) (int, error)
- func (m MapSource) GetIntMap(key string, parents []string) (map[string]int, error)
- func (m MapSource) GetIntSlice(key string, parents []string) ([]int, error)
- func (m MapSource) GetString(key string, parents []string) (string, error)
- func (m MapSource) GetStringMap(key string, parents []string) (map[string]string, error)
- func (m MapSource) GetStringSlice(key string, parents []string) ([]string, error)
- func (m MapSource) GetUint(key string, parents []string) (uint, error)
- func (m MapSource) GetUintMap(key string, parents []string) (map[string]uint, error)
- func (m MapSource) GetUintSlice(key string, parents []string) ([]uint, error)
- func (m MapSource) Tag() string
- type Option
- type Parser
- type Source
- type StringSource
- func (s StringSource) GetBool(key string, parents []string) (bool, error)
- func (s StringSource) GetBoolMap(key string, parents []string) (map[string]bool, error)
- func (s StringSource) GetBoolSlice(key string, parents []string) ([]bool, error)
- func (s StringSource) GetFloat(key string, parents []string) (float64, error)
- func (s StringSource) GetFloatMap(key string, parents []string) (map[string]float64, error)
- func (s StringSource) GetFloatSlice(key string, parents []string) ([]float64, error)
- func (s StringSource) GetInt(key string, parents []string) (int, error)
- func (s StringSource) GetIntMap(key string, parents []string) (map[string]int, error)
- func (s StringSource) GetIntSlice(key string, parents []string) ([]int, error)
- func (s StringSource) GetString(key string, parents []string) (string, error)
- func (s StringSource) GetStringMap(key string, parents []string) (map[string]string, error)
- func (s StringSource) GetStringSlice(key string, parents []string) ([]string, error)
- func (s StringSource) GetUint(key string, parents []string) (uint, error)
- func (s StringSource) GetUintMap(key string, parents []string) (map[string]uint, error)
- func (s StringSource) GetUintSlice(key string, parents []string) ([]uint, error)
- func (s StringSource) Tag() string
Constants ¶
This section is empty.
Variables ¶
var ErrNotFoundInSource = errors.New("not found in source")
Functions ¶
Types ¶
type LookupFunc ¶
LookupFunc resolves a key to its raw string representation. The second return value reports whether the key was present at all; a found-but-empty value is ("", true). StringSource turns a false into ErrNotFoundInSource so the Parser falls through to the next source.
type MapSource ¶
type MapSource struct {
// contains filtered or unexported fields
}
func (MapSource) GetBoolMap ¶
func (MapSource) GetBoolSlice ¶
func (MapSource) GetFloatMap ¶
func (MapSource) GetFloatSlice ¶
func (MapSource) GetIntSlice ¶
func (MapSource) GetStringMap ¶
func (MapSource) GetStringSlice ¶
func (MapSource) GetUintMap ¶
func (MapSource) GetUintSlice ¶
type Source ¶
type Source interface {
Tag() string
GetBool(key string, parents []string) (bool, error)
GetString(key string, parents []string) (string, error)
GetInt(key string, parents []string) (int, error)
GetUint(key string, parents []string) (uint, error)
GetFloat(key string, parents []string) (float64, error)
GetBoolSlice(key string, parents []string) ([]bool, error)
GetStringSlice(key string, parents []string) ([]string, error)
GetIntSlice(key string, parents []string) ([]int, error)
GetUintSlice(key string, parents []string) ([]uint, error)
GetFloatSlice(key string, parents []string) ([]float64, error)
GetBoolMap(key string, parents []string) (map[string]bool, error)
GetStringMap(key string, parents []string) (map[string]string, error)
GetIntMap(key string, parents []string) (map[string]int, error)
GetUintMap(key string, parents []string) (map[string]uint, error)
GetFloatMap(key string, parents []string) (map[string]float64, error)
}
func NewCliSource ¶
type StringSource ¶
type StringSource struct {
// contains filtered or unexported fields
}
StringSource adapts a string based backend to the Source interface. It handles all conversions itself: scalars via the strconv package, slices by splitting on commas, and maps by additionally splitting each entry on the first "=".
func NewStringSource ¶
func NewStringSource(tag string, lookup LookupFunc) StringSource
NewStringSource builds a Source that answers every lookup through lookup.
func (StringSource) GetBool ¶
func (s StringSource) GetBool(key string, parents []string) (bool, error)
func (StringSource) GetBoolMap ¶
func (StringSource) GetBoolSlice ¶
func (s StringSource) GetBoolSlice(key string, parents []string) ([]bool, error)
func (StringSource) GetFloat ¶
func (s StringSource) GetFloat(key string, parents []string) (float64, error)
func (StringSource) GetFloatMap ¶
func (StringSource) GetFloatSlice ¶
func (s StringSource) GetFloatSlice(key string, parents []string) ([]float64, error)
func (StringSource) GetInt ¶
func (s StringSource) GetInt(key string, parents []string) (int, error)
func (StringSource) GetIntSlice ¶
func (s StringSource) GetIntSlice(key string, parents []string) ([]int, error)
func (StringSource) GetString ¶
func (s StringSource) GetString(key string, parents []string) (string, error)
func (StringSource) GetStringMap ¶
func (StringSource) GetStringSlice ¶
func (s StringSource) GetStringSlice(key string, parents []string) ([]string, error)
func (StringSource) GetUint ¶
func (s StringSource) GetUint(key string, parents []string) (uint, error)
func (StringSource) GetUintMap ¶
func (StringSource) GetUintSlice ¶
func (s StringSource) GetUintSlice(key string, parents []string) ([]uint, error)
func (StringSource) Tag ¶
func (s StringSource) Tag() string