koanf: github.com/knadh/koanf Index | Files | Directories

package koanf

import "github.com/knadh/koanf"

Index

Package Files

getters.go interfaces.go koanf.go

type KeyMap Uses

type KeyMap map[string][]string

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 Uses

type Koanf struct {
    // contains filtered or unexported fields
}

Koanf is the configuration apparatus.

func New Uses

func New(delim string) *Koanf

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 (*Koanf) All Uses

func (ko *Koanf) All() map[string]interface{}

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 Uses

func (ko *Koanf) Bool(path string) 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 Uses

func (ko *Koanf) BoolMap(path string) map[string]bool

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 Uses

func (ko *Koanf) Bools(path string) []bool

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 Uses

func (ko *Koanf) Bytes(path string) []byte

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) Copy Uses

func (ko *Koanf) Copy() *Koanf

Copy returns a copy of the Koanf instance.

func (*Koanf) Cut Uses

func (ko *Koanf) Cut(path string) *Koanf

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) Duration Uses

func (ko *Koanf) Duration(path string) time.Duration

Duration returns the time.Duration value of a given key path assuming that the key contains a valid numeric value.

func (*Koanf) Exists Uses

func (ko *Koanf) Exists(path string) bool

Exists returns true if the given key path exists in the conf map.

func (*Koanf) Float64 Uses

func (ko *Koanf) Float64(path string) 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 Uses

func (ko *Koanf) Float64Map(path string) map[string]float64

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 Uses

func (ko *Koanf) Float64s(path string) []float64

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 Uses

func (ko *Koanf) Get(path string) interface{}

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 Uses

func (ko *Koanf) Int(path string) 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 Uses

func (ko *Koanf) Int64(path string) 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 Uses

func (ko *Koanf) Int64Map(path string) map[string]int64

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 Uses

func (ko *Koanf) Int64s(path string) []int64

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 Uses

func (ko *Koanf) IntMap(path string) map[string]int

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 Uses

func (ko *Koanf) Ints(path string) []int

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 Uses

func (ko *Koanf) KeyMap() 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 Uses

func (ko *Koanf) Keys() []string

Keys returns the slice of all flattened keys in the loaded configuration sorted alphabetically.

func (*Koanf) Load Uses

func (ko *Koanf) Load(p Provider, pa Parser) error

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.

func (*Koanf) MapKeys Uses

func (ko *Koanf) MapKeys(path string) []string

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 Uses

func (ko *Koanf) Marshal(p Parser) ([]byte, error)

Marshal takes a Parser implementation and marshals the config map into bytes, for example, to TOML or JSON bytes.

func (*Koanf) Merge Uses

func (ko *Koanf) Merge(in *Koanf)

Merge merges the config map of a given Koanf instance into the current instance.

func (*Koanf) MustBoolMap Uses

func (ko *Koanf) MustBoolMap(path string) map[string]bool

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 Uses

func (ko *Koanf) MustBools(path string) []bool

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 Uses

func (ko *Koanf) MustBytes(path string) []byte

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 Uses

func (ko *Koanf) MustDuration(path string) time.Duration

MustDuration returns the time.Duration value of a given key path or panics if its not set or set to default value 0.

func (*Koanf) MustFloat64 Uses

func (ko *Koanf) MustFloat64(path string) float64

MustFloat64 returns the float64 value of a given key path or panics or panics if its not set or set to default value 0.

func (*Koanf) MustFloat64Map Uses

func (ko *Koanf) MustFloat64Map(path string) map[string]float64

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 Uses

func (ko *Koanf) MustFloat64s(path string) []float64

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 Uses

func (ko *Koanf) MustInt(path string) int

MustInt returns the int value of a given key path or panics or panics if its not set or set to default value of 0.

func (*Koanf) MustInt64 Uses

func (ko *Koanf) MustInt64(path string) int64

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 Uses

func (ko *Koanf) MustInt64Map(path string) map[string]int64

MustInt64Map returns the map[string]int64 value of a given key path or panics if its not set or set to default value.

func (*Koanf) MustInt64s Uses

func (ko *Koanf) MustInt64s(path string) []int64

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 Uses

func (ko *Koanf) MustIntMap(path string) map[string]int

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 Uses

func (ko *Koanf) MustInts(path string) []int

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 Uses

func (ko *Koanf) MustString(path string) string

MustString returns the string value of a given key path or panics if its not set or set to default value "".

func (*Koanf) MustStringMap Uses

func (ko *Koanf) MustStringMap(path string) map[string]string

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 Uses

func (ko *Koanf) MustStrings(path string) []string

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 Uses

func (ko *Koanf) MustStringsMap(path string) map[string][]string

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 Uses

func (ko *Koanf) MustTime(path, layout string) time.Time

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 Uses

func (ko *Koanf) Print()

Print prints a key -> value string representation of the config map with keys sorted alphabetically.

func (*Koanf) Raw Uses

func (ko *Koanf) Raw() map[string]interface{}

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) Sprint Uses

func (ko *Koanf) Sprint() string

Sprint returns a key -> value string representation of the config map with keys sorted alphabetically.

func (*Koanf) String Uses

func (ko *Koanf) String(path string) 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 Uses

func (ko *Koanf) StringMap(path string) map[string]string

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 Uses

func (ko *Koanf) Strings(path string) []string

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 Uses

func (ko *Koanf) StringsMap(path string) map[string][]string

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 Uses

func (ko *Koanf) Time(path, layout string) time.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 Uses

func (ko *Koanf) Unmarshal(path string, o interface{}) error

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 Uses

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 Parser Uses

type Parser interface {
    Unmarshal([]byte) (map[string]interface{}, error)
    Marshal(map[string]interface{}) ([]byte, error)
}

Parser represents a configuration format parser.

type Provider Uses

type Provider interface {
    // Read returns the entire configuration as raw []bytes to be parsed.
    // with a Parser.
    ReadBytes() ([]byte, error)

    // Read returns the prased 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)

    // Watch watches the source for changes, for instance, changes to a file,
    // and invokes a callback with an `event` interface, which a provider
    // is free to substitute with its own type, including nil.
    Watch(func(event interface{}, err error)) error
}

Provider represents a configuration provider. Providers can read configuration from a source (file, HTTP etc.)

type UnmarshalConf Uses

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.

Directories

PathSynopsis
mapsPackage maps provides reusable functions for manipulating nested map[string]interface{} maps are common unmarshal products from various serializers such as json, yaml etc.
parsers/hclPackage hcl implements a koanf.Parser that parses Hashicorp HCL bytes as conf maps.
parsers/jsonPackage json implements a koanf.Parser that parses JSON bytes as conf maps.
parsers/tomlPackage toml implements a koanf.Parser that parses TOML bytes as conf maps.
parsers/yamlPackage yaml implements a koanf.Parser that parses YAML bytes as conf maps.
providers/basicflagPackage basicflag implements a koanf.Provider that reads commandline parameters as conf maps using the Go's flag package.
providers/confmapPackage confmap implements a koanf.Provider that takes nested and flat map[string]interface{} config maps and provides them to koanf.
providers/envPackage env implements a koanf.Provider that reads environment variables as conf maps.
providers/filePackage file implements a koanf.Provider that reads raw bytes from files on disk to be used with a koanf.Parser to parse into conf maps.
providers/posflagPackage posflag implements a koanf.Provider that reads commandline parameters as conf maps using spf13/pflag, a POSIX compliant alternative to Go's stdlib flag package.
providers/rawbytesPackage rawbytes implements a koanf.Provider that takes a []byte slice and provides it to koanf to be parsed by a koanf.Parser.
providers/s3Package s3 implements a koanf.Provider that takes a []byte slice and provides it to koanf to be parsed by a koanf.Parser.
providers/structsPackage structs implements a koanf.Provider that takes a struct and tag and returns a nested config map (using fatih/structs) to provide it to koanf.

Package koanf imports 9 packages (graph) and is imported by 10 packages. Updated 2020-04-21. Refresh now. Tools for package owners.