Summary
cfg.Unpack panics under certain circumstances for structs that contain attributes of type map[string]interface{}. When unpacking a configuration that does not contain values for the attribute of type map into a a variable that already has data set for the map attribute, the cfg.Unpack panics with following error:
panic: reflect: call of reflect.Value.MapKeys on interface Value [recovered]
panic: reflect: call of reflect.Value.MapKeys on interface Value
How to reproduce
type Deployment struct {
Configurations []map[string]interface{} `config:"configurations"`
}
func TestUnpack(t *testing.T) {
var deployment Deployment
// unpack empty config into empty struct
cfg, _ := json.NewConfig([]byte(`{}`))
require.NoError(t, cfg.Unpack(&deployment))
fmt.Println(deployment)
// unpack config into empty struct
cfg, _ = json.NewConfig([]byte(`{"configurations":[{"name":"foo","body":{"env":{"v":1}}}]}`))
require.NoError(t, cfg.Unpack(&deployment))
fmt.Println(deployment)
// unpack config into struct with existing data
cfg, _ = json.NewConfig([]byte(`{"configurations":[{"name":"bar","body":{"env":{"v":1}}}]}`))
require.NoError(t, cfg.Unpack(&deployment))
fmt.Println(deployment)
// unpack empty config into struct with existing data
// panics
cfg, _ = json.NewConfig([]byte(`{}`))
require.NoError(t, cfg.Unpack(&deployment))
fmt.Println(deployment)
}
Summary
cfg.Unpackpanics under certain circumstances for structs that contain attributes of typemap[string]interface{}. When unpacking a configuration that does not contain values for the attribute of typemapinto a a variable that already has data set for themapattribute, thecfg.Unpackpanics with following error:How to reproduce