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
We use a config file, ENV vars (automatic, without binding). Then we are loading credentials from Google Secret Manager, which is saved with viper.Set(). Then we want to unmarshal it into a structure, which is passed further.
But the problem with Unmarshal is, it just gets the top-level key, and does not merge it.
Example:
I have this in code viper.SetEnvPrefix("JKL"); viper.AutomaticEnv() and also env variable JKL_DB_HOST=localhost
and this is output from viper.Debug()
viper.Get("db.timeout") // return 15viper.Get("db.host") // return localhostviper.Get("db.password") // return pass// HowevertypeDbConfigstruct {
HoststringUsernamestringPasswordstringTimeoutint
}
dbcfg:=&DbConfig{}
viper.UnmarshalKey("db", &dbcfg)
// dbcfg will have set only password and username
The reason is, that viper.Get("db") is returning the first match, which is in override, and do not merge with the rest.
Is there some way, which would just go through all fields in a struct and for each would call some callback, where I can put viper.Get()? Maybe some another library, because mapstruct obviously does not support it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
We use a config file, ENV vars (automatic, without binding). Then we are loading credentials from Google Secret Manager, which is saved with
viper.Set()
. Then we want to unmarshal it into a structure, which is passed further.But the problem with Unmarshal is, it just gets the top-level key, and does not merge it.
Example:
I have this in code
viper.SetEnvPrefix("JKL"); viper.AutomaticEnv()
and also env variableJKL_DB_HOST=localhost
and this is output from
viper.Debug()
When I do
The reason is, that
viper.Get("db")
is returning the first match, which is in override, and do not merge with the rest.Is there some way, which would just go through all fields in a struct and for each would call some callback, where I can put
viper.Get()
? Maybe some another library, because mapstruct obviously does not support it.Beta Was this translation helpful? Give feedback.
All reactions