Documentation ¶
Index ¶
- type Conf
- type KeyMap
- type Koanf
- func (ko *Koanf) All() map[string]interface{}
- func (ko *Koanf) Bool(path string) bool
- func (ko *Koanf) BoolMap(path string) map[string]bool
- func (ko *Koanf) Bools(path string) []bool
- func (ko *Koanf) Bytes(path string) []byte
- func (ko *Koanf) Copy() *Koanf
- func (ko *Koanf) Cut(path string) *Koanf
- func (ko *Koanf) Delete(path string)
- func (ko *Koanf) Delim() string
- func (ko *Koanf) Duration(path string) time.Duration
- func (ko *Koanf) Exists(path string) bool
- func (ko *Koanf) Float64(path string) float64
- func (ko *Koanf) Float64Map(path string) map[string]float64
- func (ko *Koanf) Float64s(path string) []float64
- func (ko *Koanf) Get(path string) interface{}
- func (ko *Koanf) Int(path string) int
- func (ko *Koanf) Int64(path string) int64
- func (ko *Koanf) Int64Map(path string) map[string]int64
- func (ko *Koanf) Int64s(path string) []int64
- func (ko *Koanf) IntMap(path string) map[string]int
- func (ko *Koanf) Ints(path string) []int
- func (ko *Koanf) KeyMap() KeyMap
- func (ko *Koanf) Keys() []string
- func (ko *Koanf) Load(p Provider, pa Parser, opts ...Option) error
- func (ko *Koanf) MapKeys(path string) []string
- func (ko *Koanf) Marshal(p Parser) ([]byte, error)
- func (ko *Koanf) Merge(in *Koanf) error
- func (ko *Koanf) MergeAt(in *Koanf, path string) error
- func (ko *Koanf) MustBoolMap(path string) map[string]bool
- func (ko *Koanf) MustBools(path string) []bool
- func (ko *Koanf) MustBytes(path string) []byte
- func (ko *Koanf) MustDuration(path string) time.Duration
- func (ko *Koanf) MustFloat64(path string) float64
- func (ko *Koanf) MustFloat64Map(path string) map[string]float64
- func (ko *Koanf) MustFloat64s(path string) []float64
- func (ko *Koanf) MustInt(path string) int
- func (ko *Koanf) MustInt64(path string) int64
- func (ko *Koanf) MustInt64Map(path string) map[string]int64
- func (ko *Koanf) MustInt64s(path string) []int64
- func (ko *Koanf) MustIntMap(path string) map[string]int
- func (ko *Koanf) MustInts(path string) []int
- func (ko *Koanf) MustString(path string) string
- func (ko *Koanf) MustStringMap(path string) map[string]string
- func (ko *Koanf) MustStrings(path string) []string
- func (ko *Koanf) MustStringsMap(path string) map[string][]string
- func (ko *Koanf) MustTime(path, layout string) time.Time
- func (ko *Koanf) Print()
- func (ko *Koanf) Raw() map[string]interface{}
- func (ko *Koanf) Set(key string, val interface{}) error
- func (ko *Koanf) Slices(path string) []*Koanf
- func (ko *Koanf) Sprint() string
- func (ko *Koanf) String(path string) string
- func (ko *Koanf) StringMap(path string) map[string]string
- func (ko *Koanf) Strings(path string) []string
- func (ko *Koanf) StringsMap(path string) map[string][]string
- func (ko *Koanf) Time(path, layout string) time.Time
- func (ko *Koanf) Unmarshal(path string, o interface{}) error
- func (ko *Koanf) UnmarshalWithConf(path string, o interface{}, c UnmarshalConf) error
- type Option
- type Parser
- type Provider
- type UnmarshalConf
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conf ¶
type Conf struct { // Delim is the delimiter to use // when specifying config key paths, for instance a . for `parent.child.key` // or a / for `parent/child/key`. Delim string // StrictMerge makes the merging behavior strict. // Meaning when loading two files that have the same key, // the first loaded file will define the desired type, and if the second file loads // a different type will cause an error. StrictMerge bool }
Conf is the Koanf configuration.
type KeyMap ¶
KeyMap represents a map of flattened delimited keys and the non-delimited parts as their slices. For nested keys, the map holds all levels of path combinations. For example, the nested structure `parent -> child -> key` will produce the map: parent.child.key => [parent, child, key] parent.child => [parent, child] parent => [parent]
type Koanf ¶
type Koanf struct {
// contains filtered or unexported fields
}
Koanf is the configuration apparatus.
func New ¶
New returns a new instance of Koanf. delim is the delimiter to use when specifying config key paths, for instance a . for `parent.child.key` or a / for `parent/child/key`.
func NewWithConf ¶
NewWithConf returns a new instance of Koanf based on the Conf.
func (*Koanf) All ¶
All returns a map of all flattened key paths and their values. Note that it uses maps.Copy to create a copy that uses json.Marshal which changes the numeric types to float64.
func (*Koanf) Bool ¶
Bool returns the bool value of a given key path or false if the path does not exist or if the value is not a valid bool representation. Accepted string representations of bool are the ones supported by strconv.ParseBool.
func (*Koanf) BoolMap ¶
BoolMap returns the map[string]bool value of a given key path or an empty map[string]bool if the path does not exist or if the value is not a valid bool map.
func (*Koanf) Bools ¶
Bools returns the []bool slice value of a given key path or an empty []bool slice if the path does not exist or if the value is not a valid bool slice.
func (*Koanf) Bytes ¶
Bytes returns the []byte value of a given key path or an empty []byte slice if the path does not exist or if the value is not a valid string.
func (*Koanf) Cut ¶
Cut cuts the config map at a given key path into a sub map and returns a new Koanf instance with the cut config map loaded. For instance, if the loaded config has a path that looks like parent.child.sub.a.b, `Cut("parent.child")` returns a new Koanf instance with the config map `sub.a.b` where everything above `parent.child` are cut out.
func (*Koanf) Delete ¶
Delete removes all nested values from a given path. Clears all keys/values if no path is specified. Every empty, key on the path, is recursively deleted.
func (*Koanf) Duration ¶
Duration returns the time.Duration value of a given key path assuming that the key contains a valid numeric value.
func (*Koanf) Float64 ¶
Float64 returns the float64 value of a given key path or 0 if the path does not exist or if the value is not a valid float64.
func (*Koanf) Float64Map ¶
Float64Map returns the map[string]float64 value of a given key path or an empty map[string]float64 if the path does not exist or if the value is not a valid float64 map.
func (*Koanf) Float64s ¶
Float64s returns the []float64 slice value of a given key path or an empty []float64 slice if the path does not exist or if the value is not a valid float64 slice.
func (*Koanf) Get ¶
Get returns the raw, uncast interface{} value of a given key path in the config map. If the key path does not exist, nil is returned.
func (*Koanf) Int ¶
Int returns the int value of a given key path or 0 if the path does not exist or if the value is not a valid int.
func (*Koanf) Int64 ¶
Int64 returns the int64 value of a given key path or 0 if the path does not exist or if the value is not a valid int64.
func (*Koanf) Int64Map ¶
Int64Map returns the map[string]int64 value of a given key path or an empty map[string]int64 if the path does not exist or if the value is not a valid int64 map.
func (*Koanf) Int64s ¶
Int64s returns the []int64 slice value of a given key path or an empty []int64 slice if the path does not exist or if the value is not a valid int slice.
func (*Koanf) IntMap ¶
IntMap returns the map[string]int value of a given key path or an empty map[string]int if the path does not exist or if the value is not a valid int map.
func (*Koanf) Ints ¶
Ints returns the []int slice value of a given key path or an empty []int slice if the path does not exist or if the value is not a valid int slice.
func (*Koanf) KeyMap ¶
KeyMap returns a map of flattened keys and the individual parts of the key as slices. eg: "parent.child.key" => ["parent", "child", "key"].
func (*Koanf) Keys ¶
Keys returns the slice of all flattened keys in the loaded configuration sorted alphabetically.
func (*Koanf) Load ¶
Load takes a Provider that either provides a parsed config map[string]interface{} in which case pa (Parser) can be nil, or raw bytes to be parsed, where a Parser can be provided to parse. Additionally, options can be passed which modify the load behavior, such as passing a custom merge function.
func (*Koanf) MapKeys ¶
MapKeys returns a sorted string list of keys in a map addressed by the given path. If the path is not a map, an empty string slice is returned.
func (*Koanf) Marshal ¶
Marshal takes a Parser implementation and marshals the config map into bytes, for example, to TOML or JSON bytes.
func (*Koanf) Merge ¶
Merge merges the config map of a given Koanf instance into the current instance.
func (*Koanf) MergeAt ¶
MergeAt merges the config map of a given Koanf instance into the current instance as a sub map, at the given key path. If all or part of the key path is missing, it will be created. If the key path is `""`, this is equivalent to Merge.
func (*Koanf) MustBoolMap ¶
MustBoolMap returns the map[string]bool value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustBools ¶
MustBools returns the []bool value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustBytes ¶
MustBytes returns the []byte value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustDuration ¶
MustDuration returns the time.Duration value of a given key path or panics if it isn't set or set to default value 0.
func (*Koanf) MustFloat64 ¶
MustFloat64 returns the float64 value of a given key path or panics if it isn't set or set to default value 0.
func (*Koanf) MustFloat64Map ¶
MustFloat64Map returns the map[string]float64 value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustFloat64s ¶
MustFloat64s returns the []Float64 slice value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustInt ¶
MustInt returns the int value of a given key path or panics if it isn't set or set to default value of 0.
func (*Koanf) MustInt64 ¶
MustInt64 returns the int64 value of a given key path or panics if the value is not set or set to default value of 0.
func (*Koanf) MustInt64Map ¶
MustInt64Map returns the map[string]int64 value of a given key path or panics if it isn't set or set to default value.
func (*Koanf) MustInt64s ¶
MustInt64s returns the []int64 slice value of a given key path or panics if the value is not set or its default value.
func (*Koanf) MustIntMap ¶
MustIntMap returns the map[string]int value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustInts ¶
MustInts returns the []int slice value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustString ¶
MustString returns the string value of a given key path or panics if it isn't set or set to default value "".
func (*Koanf) MustStringMap ¶
MustStringMap returns the map[string]string value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustStrings ¶
MustStrings returns the []string slice value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustStringsMap ¶
MustStringsMap returns the map[string][]string value of a given key path or panics if the value is not set or set to default value.
func (*Koanf) MustTime ¶
MustTime attempts to parse the value of a given key path and return time.Time representation. If the value is numeric, it is treated as a UNIX timestamp and if it's string, a parse is attempted with the given layout. It panics if the parsed time is zero.
func (*Koanf) Print ¶
func (ko *Koanf) Print()
Print prints a key -> value string representation of the config map with keys sorted alphabetically.
func (*Koanf) Raw ¶
Raw returns a copy of the full raw conf map. Note that it uses maps.Copy to create a copy that uses json.Marshal which changes the numeric types to float64.
func (*Koanf) Slices ¶
Slices returns a list of Koanf instances constructed out of a []map[string]interface{} interface at the given path.
func (*Koanf) Sprint ¶
Sprint returns a key -> value string representation of the config map with keys sorted alphabetically.
func (*Koanf) String ¶
String returns the string value of a given key path or "" if the path does not exist or if the value is not a valid string.
func (*Koanf) StringMap ¶
StringMap returns the map[string]string value of a given key path or an empty map[string]string if the path does not exist or if the value is not a valid string map.
func (*Koanf) Strings ¶
Strings returns the []string slice value of a given key path or an empty []string slice if the path does not exist or if the value is not a valid string slice.
func (*Koanf) StringsMap ¶
StringsMap returns the map[string][]string value of a given key path or an empty map[string][]string if the path does not exist or if the value is not a valid strings map.
func (*Koanf) Time ¶
Time attempts to parse the value of a given key path and return time.Time representation. If the value is numeric, it is treated as a UNIX timestamp and if it's string, a parse is attempted with the given layout.
func (*Koanf) Unmarshal ¶
Unmarshal unmarshals a given key path into the given struct using the mapstructure lib. If no path is specified, the whole map is unmarshalled. `koanf` is the struct field tag used to match field names. To customize, use UnmarshalWithConf(). It uses the mitchellh/mapstructure package.
func (*Koanf) UnmarshalWithConf ¶
func (ko *Koanf) UnmarshalWithConf(path string, o interface{}, c UnmarshalConf) error
UnmarshalWithConf is like Unmarshal but takes configuration params in UnmarshalConf. See mitchellh/mapstructure's DecoderConfig for advanced customization of the unmarshal behaviour.
type Option ¶
type Option func(*options)
Option is a generic type used to modify the behavior of Koanf.Load.
func WithMergeFunc ¶
WithMergeFunc is an option to modify the merge behavior of Koanf.Load. If unset, the default merge function is used.
The merge function is expected to merge map src into dest (left to right).
type Parser ¶
type Parser interface { Unmarshal([]byte) (map[string]interface{}, error) Marshal(map[string]interface{}) ([]byte, error) }
Parser represents a configuration format parser.
type Provider ¶
type Provider interface { // ReadBytes returns the entire configuration as raw []bytes to be parsed. // with a Parser. ReadBytes() ([]byte, error) // Read returns the parsed configuration as a nested map[string]interface{}. // It is important to note that the string keys should not be flat delimited // keys like `parent.child.key`, but nested like `{parent: {child: {key: 1}}}`. Read() (map[string]interface{}, error) }
Provider represents a configuration provider. Providers can read configuration from a source (file, HTTP etc.)
type UnmarshalConf ¶
type UnmarshalConf struct { // Tag is the struct field tag to unmarshal. // `koanf` is used if left empty. Tag string // If this is set to true, instead of unmarshalling nested structures // based on the key path, keys are taken literally to unmarshal into // a flat struct. For example: // “` // type MyStuff struct { // Child1Name string `koanf:"parent1.child1.name"` // Child2Name string `koanf:"parent2.child2.name"` // Type string `koanf:"json"` // } // “` FlatPaths bool DecoderConfig *mapstructure.DecoderConfig }
UnmarshalConf represents configuration options used by Unmarshal() to unmarshal conf maps into arbitrary structs.