Skip to content
Rohan Verma edited this page Dec 10, 2019 · 2 revisions

Welcome to the koanf wiki!

Tips and Tricks

Mapstructure

Koanf uses the mapstructure package that's used for decoding the config from koanf to structs.

Default configuration

By default, koanf uses the following mapstructure decoder config:

mapstructure.DecoderConfig{
        ...
	WeaklyTypedInput: true,
        TagName = "koanf"
        ...
}

Weakly typed input

WeaklyTypedInput is set to be true and implies that koanf will do 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. 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 is weakly decoded. For example: "4" can become []int{4} the target type is an int slice.

Struct Tag

The TagName in DecoderConfig is set to koanf by default. If it is not set, the default fallback is mapstructure. Although, for fields without any tag, mapstructure fallsback to the field name. ref: #5

Embedded structs

For embedded/composite structs, you can use the ,squash tag to squash multiple embedded structs. You can refer to an example present in the mapstructure godoc, ref: #8