You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Package enviper is a helper/wrapper for viper with the same API.
It makes it possible to unmarshal config to struct considering environment variables.
Problem
Viper package doesn't consider environment variables while unmarshaling.
Please, see: 188 and 761
Solution
Just wrap viper instance and use the same Unmarshal method as you did before:
e:=enviper.New(viper.New())
e.Unmarshal(&config)
Example
package main
import (
"github.com/iamolegga/enviper""github.com/spf13/viper"
)
typebarrystruct {
Barint`mapstructure:"bar"`
}
typebazzystruct {
Bazbool
}
typequxxystruct {
Quxstring
}
typeconfigstruct {
FoostringBarrybarryBarriesmap[string]barryBazzybazzy`mapstructure:",squash"`Quxxy*quxxy
}
// For example this kind of structure can be unmarshaled with next yaml:// Foo: foo// Barry:// bar: 42// Baz: true// Barries: // key1:// Bar: 255// key2:// Bar: 256// Quxxy:// Qux: "lorem"//// And then it could be overriden by next env variables:// FOO=foo// BARRY_BAR=42// BAZ=true// BARRIES_KEY1_BAR=42// QUXXY_QUX=ipsum//// Or with prefix:// MYAPP_FOO=foo// MYAPP_BARRY_BAR=42// MYAPP_BAZ=true// MYAPP_BARRIES_KEY1_BAR=42// MYAPP_QUXXY_QUX=ipsumfuncmain() {
varcconfige:=enviper.New(viper.New())
e.SetEnvPrefix("MYAPP")
e.AddConfigPath("/my/config/path")
e.SetConfigName("config")
e.Unmarshal(&c)
}
Custom Tag Names
In case you want to use custom tag name (something different from mapstructure), you have to set it explicitly via WithTagName function.
The wrapper must know custom tag name in order to register all the env vars for viper so you can't just use DecoderConfigOption.