mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
MIT License
7.93k stars 677 forks source link

mapstructure fails to unmarshal when the type is string slice #289

Open pantelis-karamolegkos opened 2 years ago

pantelis-karamolegkos commented 2 years ago

Although came across this when using viper, I believe this is a mapstructure issue

I am defining a slice flag as follows

fs.StringArray(name, sliceValue, help)

where sliceValue: = []string{"PUT", "POST"}

The corresponding Config struct field is the following

CORSAllowMethods []string `mapstructure:"CORS_ALLOW_METHODS" name:"cors-allow-methods" long:"cors-allow-methods" defaultValue:"PUT POST" help:"List of CORS methods that are allowed"`

After performing a v.Unmarshal(&conf) was expecting something like this:

CORSAllowMethods:[PUT POST]

The end result after v.Unmarshal(&conf) is

CORSAllowMethods:[[PUT POST]]

(a nested slice???!!!)