From 91c0ed863a7d514b65db79ae4eb85822f0f0f508 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Wed, 8 Jun 2022 19:28:28 +0100 Subject: [bugfix] #621: add weak type handing to mapstructure decode (#625) * Drone sig (#623) * accept weakly typed input on mapstructure decode i.e. .UnmarshalMap() Signed-off-by: kim * add envparsing script to test for panics during environment variable parsing Signed-off-by: kim * add envparsing.sh script to drone commands Signed-off-by: kim * update drone signature Co-authored-by: kim * compare expected with output * update expected output of envparsing * update expected output to correct value * use viper's unmarshal function instead There were problems with marshalling string slices from viper into the st.config struct with the other function. Now, we can use viper's unmarshal function and pass in the custom decoder config that we need as a hook. This ensures that we marshal string slices from viper into our config struct correctly. Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com> Co-authored-by: tsmethurst --- internal/config/state.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'internal/config/state.go') diff --git a/internal/config/state.go b/internal/config/state.go index 70972a835..1364fb84e 100644 --- a/internal/config/state.go +++ b/internal/config/state.go @@ -22,6 +22,7 @@ import ( "strings" "sync" + "github.com/mitchellh/mapstructure" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -129,8 +130,10 @@ func (st *ConfigState) reloadToViper() { // reloadFromViper will reload Configuration{} values from viper. func (st *ConfigState) reloadFromViper() { - err := st.config.UnmarshalMap(st.viper.AllSettings()) - if err != nil { + if err := st.viper.Unmarshal(&st.config, func(c *mapstructure.DecoderConfig) { + c.TagName = "name" + c.ZeroFields = true // empty the config struct before we marshal values into it + }); err != nil { panic(err) } } -- cgit v1.2.3