Documentation
¶
Index ¶
- Constants
- Variables
- func DecimalHookFunc() mapstructure.DecodeHookFunc
- func Decode(source, dest interface{}, opts ...DecodeOption) error
- func DefaultDecodeHooks(extras ...mapstructure.DecodeHookFunc) []mapstructure.DecodeHookFunc
- func DefaultDecoderConfig(opts ...DecodeOption) *mapstructure.DecoderConfig
- func MatchSnakeCaseConfig(mapKey, fieldName string) bool
- func SLogLevelHookFunc() mapstructure.DecodeHookFunc
- func ToSnakeCase(str string) string
- func UnmarshalEnvStruct(path string, dest interface{}, tag string) error
- type Conf
- func (c *Conf) Parse(parsers ...ParserFunc) error
- func (c *Conf) ParseBytes(b []byte, format string) error
- func (c *Conf) ParseEnvPrefix(prefix string) error
- func (c *Conf) ParseFile(configFile string) error
- func (c *Conf) ParseFlagSet(in *pflag.FlagSet) error
- func (c *Conf) ParseMap(config map[string]interface{}) error
- func (c *Conf) ParseProvider(p koanf.Provider, format string) error
- func (c *Conf) ParseStruct(in interface{}) error
- func (c *Conf) ParseStructWithTag(in interface{}, tag string) error
- func (c *Conf) Unmarshal(dest interface{}, unmarshalConfig UnmarshalConf) error
- func (c *Conf) UnmarshalWithOpts(dest interface{}, opts ...UnmarshalOption) error
- type DecodeOption
- func WithDecodeHook(hooks ...mapstructure.DecodeHookFunc) DecodeOption
- func WithErrUnused(b bool) DecodeOption
- func WithMatchName(f func(mapKey, fieldName string) bool) DecodeOption
- func WithSquash(b bool) DecodeOption
- func WithTagName(tagName string) DecodeOption
- func WithWeaklyTypedInput(b bool) DecodeOption
- func WithZeroFields(b bool) DecodeOption
- type Opts
- type ParserFunc
- func WithBytes(b []byte, format string) ParserFunc
- func WithEnv() ParserFunc
- func WithEnvPrefix(prefix string) ParserFunc
- func WithFile(configFile string) ParserFunc
- func WithMap(config map[string]interface{}) ParserFunc
- func WithStruct(in interface{}) ParserFunc
- func WithStructWithTag(in interface{}, tag string) ParserFunc
- type UnmarshalConf
- type UnmarshalOption
Constants ¶
const DefaultDelimiter = "."
const DefaultTag = "conf"
Variables ¶
var C = New()
C is the optional global configuration using "." as the delimeter.
Functions ¶
func DecimalHookFunc ¶
func DecimalHookFunc() mapstructure.DecodeHookFunc
DecimalHookFunc returns a DecodeHookFunc that converts strings or numbers to decimal.Decimal
func Decode ¶
func Decode(source, dest interface{}, opts ...DecodeOption) error
Decode is a helper for github.com/mitchellh/mapstructure to compose a DecoderConfig decode into a struct.
func DefaultDecodeHooks ¶
func DefaultDecodeHooks(extras ...mapstructure.DecodeHookFunc) []mapstructure.DecodeHookFunc
DefaultDecodeHooks are the default decoding hooks used to unmarshal into a struct. This includes hooks for parsing string to time.Duration, time.Time(RFC3339 format), net.IP and net.IPNet. You can use this function to grab the defaults plus add your own with the extras option.
func DefaultDecoderConfig ¶
func DefaultDecoderConfig(opts ...DecodeOption) *mapstructure.DecoderConfig
DefaultDecoderConfig returns the default decoder config which should decode everything that is typically used. It designed for this module and configuration parsing. You can also pass additional options to further configure it.
func MatchSnakeCaseConfig ¶
MatchSnakeCaseConfig can be used to match snake case config into Go struct fields that do not have tags.
func SLogLevelHookFunc ¶
func SLogLevelHookFunc() mapstructure.DecodeHookFunc
SLogLevelHookFunc decodes string config into a slog.Level.
func UnmarshalEnvStruct ¶
Allow using without a config just for environment variables into a struct.
Types ¶
type Conf ¶
Conf is a wrapper around a koanf.Koanf to give it load and unmarshal functionality.
func NewWithOpts ¶
NewWithOpts returns a new Conf instance with custom options.
func (*Conf) Parse ¶
func (c *Conf) Parse(parsers ...ParserFunc) error
Parse is the all purpose wrapper to parse configuration from a multitude of places. Config sources are provided via ParserFuncs.
func (*Conf) ParseBytes ¶
ParseBytes loads configuration from bytes. It supports yaml, json and toml. The format must be supplied. If buf is empty is it ignored.
func (*Conf) ParseEnvPrefix ¶
ParseEnvPrefix parses configuration values from environment variables. It is only useful for overriding values that are already present in the configuration. By default it looks for an environment variable that is all caps with periods replaced by underscores to override. database.user_password would be overridden by an environment variable called DATABASE_USER_PASSWORD.
func (*Conf) ParseFile ¶
ParseFile loads configuration from a file. It supports yaml, json and toml. The type is inferred from configFile extension. If configFile is an empty string the file is ignored.
func (*Conf) ParseFlagSet ¶
ParseFlagSet will set configuration values from existing command line flags. It uses flag names separated with dots just like the config options.
func (*Conf) ParseMap ¶
ParseMap parses configuration from a map[string]interface{} and is handy for passing in defaults. If config is nil, it is ignored.
func (*Conf) ParseProvider ¶
ParseProvider is a helper that takes a koanf provider and format and parses configuration from it..
func (*Conf) ParseStruct ¶
ParseStruct loads configuration from a struct. If it's nil, it's ignored. It will follow any tags configured on the struct.
func (*Conf) ParseStructWithTag ¶
ParseStruct loads configuration from a struct. If it's nil, it's ignored. This allows you to set what tag to look at on the struct.
func (*Conf) Unmarshal ¶
func (c *Conf) Unmarshal(dest interface{}, unmarshalConfig UnmarshalConf) error
Unmarshal configuration to dest (it should be a pointer). See help for UnmarshalConf for more information.
func (*Conf) UnmarshalWithOpts ¶
func (c *Conf) UnmarshalWithOpts(dest interface{}, opts ...UnmarshalOption) error
UnmarshalWithOpts unmarshals config into a data struct using config options.
type DecodeOption ¶
type DecodeOption func(dc *mapstructure.DecoderConfig)
DecodeOption is a helper function to configure a Decoder
func WithDecodeHook ¶
func WithDecodeHook(hooks ...mapstructure.DecodeHookFunc) DecodeOption
WithDecodeHook sets the decode hooks for this decoder
func WithErrUnused ¶
func WithErrUnused(b bool) DecodeOption
If ErrorUnused is true, then it is an error for there to exist keys in the original map that were unused in the decoding process (extra keys).
func WithMatchName ¶
func WithMatchName(f func(mapKey, fieldName string) bool) DecodeOption
WithMatchName allows you to set a function to match the tag/field name with the map key.
func WithSquash ¶
func WithSquash(b bool) DecodeOption
Squash will squash embedded structs. A squash tag may also be added to an individual struct field using a tag. For example:
type Parent struct { Child `mapstructure:",squash"` }
func WithWeaklyTypedInput ¶
func WithWeaklyTypedInput(b bool) DecodeOption
If WeaklyTypedInput is true, the decoder will make the following "weak" conversions:
- bools to string (true = "1", false = "0")
- numbers to string (base 10)
- bools to int/uint (true = 1, false = 0)
- strings to int/uint (base implied by prefix)
- int to bool (true if value != 0)
- string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Anything else is an error)
- empty array = empty map and vice versa
- negative numbers to overflowed uint values (base 10)
- slice of maps to a merged map
- single values are converted to slices if required. Each element is weakly decoded. For example: "4" can become []int{4} if the target type is an int slice.
func WithZeroFields ¶
func WithZeroFields(b bool) DecodeOption
ZeroFields, if set to true, will zero fields before writing them. For example, a map will be emptied before decoded values are put in it. If this is false, a map will be merged.
type ParserFunc ¶
ParserFunc is a option function for loading different types of config.
func WithBytes ¶
func WithBytes(b []byte, format string) ParserFunc
WithBytes parses a configuration from byte slice. See ParseBytes for information on formats supported.
func WithEnv ¶
func WithEnv() ParserFunc
WithEnv parses configuration from environment variable. See ParseEnv for more information. Environment variables can only override existing configuration values.
func WithEnvPrefix ¶
func WithEnvPrefix(prefix string) ParserFunc
WithEnvPrefix parses configuration from environment variables given the environment variable prefix.. See ParseEnvPrefix for more information. Environment variables can only override existing configuration values.
func WithFile ¶
func WithFile(configFile string) ParserFunc
WithFile parses a configuration from a file. See ParseFile for information on file types supported.
func WithMap ¶
func WithMap(config map[string]interface{}) ParserFunc
WithMap is a ParserFunc to leverage a map to load configuration. See ParseMap for more information.
func WithStruct ¶
func WithStruct(in interface{}) ParserFunc
WithStruct parses a configuration from a struct. See ParseStruct for information on arguments.
func WithStructWithTag ¶
func WithStructWithTag(in interface{}, tag string) ParserFunc
WithStructWithTag parses a configuration from a struct. See ParseStructWithTag for information on arguments.
type UnmarshalConf ¶
type UnmarshalConf struct { // The path in the configuration to start unmarshaling from. // Leave empty to unmarshal the root config structure. Path string // FlatPath interprets keys with delimeters literally instead of recursively unmarshaling structs. FlatPaths bool // If unmarshaling to a struct it will set defaults in the struct first using // github.com/creasty/defaults tags before unmarshaling the config into the struct. StructDefaults bool // Normally if a configuration field is not already set in the config it will not be used // from the environment. Setting this to true will also scan for environment variables // associated to the struct fields/tags even if it is not already set in the config. StructEnvironment bool // If you wish to look for an environment variable with a prefix, you can set it here. StructEnvironmentPrefix string // Decoder config is the github.com/mitchellh/mapstructure.DecoderConfig used to umarshal // configuration into data structures. DecoderConfig *mapstructure.DecoderConfig }
UnmarshalConf is used to configure the unmarshaler
type UnmarshalOption ¶
type UnmarshalOption func(c *UnmarshalConf)
UnmarshalOption are used to configure the Unmarshal behavior
func WithDecoderOpts ¶
func WithDecoderOpts(opts ...DecodeOption) UnmarshalOption
DecoderConfig is the decoder config used to decode into the struct.
func WithFlatPaths ¶
func WithFlatPaths(b bool) UnmarshalOption
WithFlatPath set unmarshaling to use flat path. See UnmarshalConf.
func WithStructDefaults ¶
func WithStructDefaults(b bool) UnmarshalOption
WithStructDefaults sets the struct defaults. See UnmarshalConf.
func WithStructEnvironment ¶
func WithStructEnvironment(b bool) UnmarshalOption
WithStructEnvironment sets the struct via environment. See UnmarshalConf.