knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to load config to the slice of structs?

renom opened this issue · comments

For example:

// Structure to unmarshal nested conf to.
type childStruct struct {
	Name       string            `koanf:"name"`
	Type       string            `koanf:"type"`
	Empty      map[string]string `koanf:"empty"`
	GrandChild []struct {
		Ids []int `koanf:"ids"`
		On  bool  `koanf:"on"`
	} `koanf:"grandchild1"`
}

How to populate the fields in GrandChild?

Unmarshalling the following JSON for example should work.

{
  "name": "xxx",
  "grandchild1": [{
     "ids": [1, 2, 3, 4],
      "on": true
  }, {
     "ids": [5, 6],
      "on": false
  }]
}

What about env variables?

Please refer to the examples folder. A slice of structs directly from env, that's not possible out of the box. You will have to do a lot of string manipulation and populate the struct with post-processing.

Thanks!