conf

package
v0.0.0-...-495eea3 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDelimiter = "."
View Source
const DefaultTag = "conf"

Variables

View Source
var C = New()

C is the optional global configuration using "." as the delimeter.

Functions

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

func MatchSnakeCaseConfig(mapKey, fieldName string) bool

MatchSnakeCaseConfig can be used to match snake case config into Go struct fields that do not have tags.

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase converts to snake case.

func ZapLogLevelHookFunc

func ZapLogLevelHookFunc() mapstructure.DecodeHookFunc

ZapLogLevelHookFunc()

Types

type Conf

type Conf struct {
	*koanf.Koanf
	Opts
}

Conf is a wrapper around a koanf.Koanf to give it load and unmarshal functionality.

func New

func New() *Conf

New returns a new Conf with the default options.

func NewWithOpts

func NewWithOpts(opts Opts) *Conf

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

func (c *Conf) ParseBytes(b []byte, format string) error

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) ParseEnv

func (c *Conf) ParseEnv() error

WithEnv 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

func (c *Conf) ParseFile(configFile string) error

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) ParseMap

func (c *Conf) ParseMap(config map[string]interface{}) error

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

func (c *Conf) ParseProvider(p koanf.Provider, format string) error

ParseProvider is a helper that takes a koanf provider and format and parses configuration from it..

func (*Conf) ParseStruct

func (c *Conf) ParseStruct(in interface{}) error

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

func (c *Conf) ParseStructWithTag(in interface{}, tag string) error

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 WithTagName

func WithTagName(tagName string) DecodeOption

WithTag sets the decode field tag

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 Opts

type Opts struct {
	Delimiter string
	Tag       string
}

Opts allows overriding the default tag and delimiters.

type ParserFunc

type ParserFunc func(*Conf) error

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 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
	// 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 WithPath

func WithPath(path string) UnmarshalOption

WithPath sets the unmarshal path.

func WithTag

func WithTag(tag string) UnmarshalOption

WithTag sets the unmarshal tag.

Jump to

Keyboard shortcuts

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