configuration

package
v2.0.0-...-59fec89 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: Apache-2.0, BSD-2-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownConfigFormat is returned if the format of the config file is unknown.
	ErrUnknownConfigFormat = errors.New("unknown config file format")
)

Functions

func BindParameters

func BindParameters(pointerToStruct interface{}, optionalNamespace ...string)

BindParameters is a utility function that allows to define and bind a set of parameters in a single step by using a struct as the registry and definition for the created configuration parameters. It parses the relevant information from the struct using reflection and optionally provided information in the tags of its fields.

The parameter names are determined by the names of the fields in the struct but they can be overridden by providing a name tag. The default value is determined by the value of the field in the struct but it can be overridden by providing a default tag. The usage information are determined by the usage tag of the field.

The method supports nested structs which get translates to parameter names in the following way: --level1.level2.level3.parameterName

The first level is determined by the package of struct but it can be overridden by providing an optional namespace parameter.

func UpdateBoundParameters

func UpdateBoundParameters(configuration *Configuration)

UpdateBoundParameters updates parameters that were bound using the BindParameters method with the current values in the configuration.

Types

type BoundParameter

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

BoundParameter stores the pointer and the type of values that were bound using the BindParameters function.

type Configuration

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

Configuration holds config parameters from several sources (file, env vars, flags).

func New

func New() *Configuration

New returns a new configuration.

func (*Configuration) All

func (c *Configuration) 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 (*Configuration) Bool

func (c *Configuration) 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 (*Configuration) BoolMap

func (c *Configuration) 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 (*Configuration) Bools

func (c *Configuration) 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 (*Configuration) Bytes

func (c *Configuration) 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 (*Configuration) Duration

func (c *Configuration) 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 (*Configuration) Exists

func (c *Configuration) Exists(path string) bool

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

func (*Configuration) Float64

func (c *Configuration) 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 (*Configuration) Float64Map

func (c *Configuration) 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 (*Configuration) Float64s

func (c *Configuration) 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 (*Configuration) Get

func (c *Configuration) 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 (*Configuration) Int

func (c *Configuration) 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 (*Configuration) Int64

func (c *Configuration) 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 (*Configuration) Int64Map

func (c *Configuration) 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 (*Configuration) Int64s

func (c *Configuration) 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 (*Configuration) IntMap

func (c *Configuration) 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 (*Configuration) Ints

func (c *Configuration) 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 (*Configuration) LoadEnvironmentVars

func (c *Configuration) LoadEnvironmentVars(prefix string) error

LoadEnvironmentVars loads parameters from env vars and merges them into the loaded config. The prefix is used to filter the env vars. Only existing keys will be overwritten, all other keys are ignored.

func (*Configuration) LoadFile

func (c *Configuration) LoadFile(filePath string) error

LoadFile loads parameters from a JSON or YAML file and merges them into the loaded config. Existing keys will be overwritten.

func (*Configuration) LoadFlagSet

func (c *Configuration) LoadFlagSet(flagSet *flag.FlagSet) error

LoadFlagSet loads parameters from a FlagSet (spf13/pflag lib) including default values and merges them into the loaded config. Existing keys will only be overwritten, if they were set via command line. If not given via command line, default values will only be used if they did not exist beforehand.

func (*Configuration) MapKeys

func (c *Configuration) 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 (*Configuration) Print

func (c *Configuration) Print(ignoreSettingsAtPrint ...[]string)

Print prints the actual configuration, ignoreSettingsAtPrint are not shown.

func (*Configuration) Set

func (c *Configuration) Set(path string, value interface{}, parser ...koanf.Parser) error

Set sets the value for the key (case-insensitive).

func (*Configuration) SetDefault

func (c *Configuration) SetDefault(path string, value interface{}, parser ...koanf.Parser) error

SetDefault sets the default value for the key (case-insensitive). Default is only applied if no value is provided via flag, file or env vars.

func (*Configuration) SetDefaultStruct

func (c *Configuration) SetDefaultStruct(path string, value interface{}, fieldTag string, parser ...koanf.Parser) error

SetDefaultStruct sets the default value for the key (case-insensitive). Default is only applied if no value is provided via flag, file or env vars.

func (*Configuration) SetStruct

func (c *Configuration) SetStruct(path string, value interface{}, fieldTag string, parser ...koanf.Parser) error

SetStruct sets the value of the struct for the key (case-insensitive).

func (*Configuration) StoreFile

func (c *Configuration) StoreFile(filePath string, ignoreSettingsAtStore ...[]string) error

StoreFile stores the current config to a JSON or YAML file. ignoreSettingsAtStore will not be stored to the file.

func (*Configuration) String

func (c *Configuration) 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 (*Configuration) StringMap

func (c *Configuration) 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 (*Configuration) Strings

func (c *Configuration) 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 (*Configuration) StringsMap

func (c *Configuration) 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 (*Configuration) Time

func (c *Configuration) 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 (*Configuration) Unmarshal

func (c *Configuration) 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 unmarshaled. `koanf` is the struct field tag used to match field names. To customize, use UnmarshalWithConf(). It uses the mitchellh/mapstructure package.

func (*Configuration) UnmarshalWithConf

func (c *Configuration) UnmarshalWithConf(path string, o interface{}, uc koanf.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 JSONLowerParser

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

JSONLowerParser implements a JSON parser. all config keys are lower cased.

func (*JSONLowerParser) Marshal

func (p *JSONLowerParser) Marshal(o map[string]interface{}) ([]byte, error)

Marshal marshals the given config map to JSON bytes.

func (*JSONLowerParser) Unmarshal

func (p *JSONLowerParser) Unmarshal(b []byte) (map[string]interface{}, error)

Unmarshal parses the given JSON bytes.

type YAMLLowerParser

type YAMLLowerParser struct{}

YAMLLowerParser implements a YAML parser. all config keys are lower cased.

func (*YAMLLowerParser) Marshal

func (p *YAMLLowerParser) Marshal(o map[string]interface{}) ([]byte, error)

Marshal marshals the given config map to YAML bytes.

func (*YAMLLowerParser) Unmarshal

func (p *YAMLLowerParser) Unmarshal(b []byte) (map[string]interface{}, error)

Unmarshal parses the given YAML bytes.

Jump to

Keyboard shortcuts

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