0

I have a viper config that has some fixed fields, and some dynamic fields. It looks like this:

//always contains those keys
a: 1
b: "2"
c: true
//those keys might change:
params:
  x1: 111
  x2: "222"

I know I can unmarshal the params part into a map[string]interface{} and use the values at runtime, but I would like to unmarshal this config into the a viper config instead, ie the struct should look like this:

type Config struct {
   A int              `mapstructure:"a"`
   B string           `mapstructure:"b"`
   C bool             `mapstructure:"c"`
   Params viper.Viper `mapstructure:"params"`
}

but when trying to use the struct like that, the Params variable is unmarshaled into an empty config.

Can viper support such usage?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
lev
  • 3,986
  • 4
  • 33
  • 46
  • That's not how viper works. What are you actually trying to accomplish? – Adrian Oct 27 '20 at 13:26
  • I have many components, and I select at runtime what component to use. each component have different config values – lev Oct 28 '20 at 12:24

0 Answers0