Skip to content

cfg.Unpack panics for existing maps #167

@simitt

Description

@simitt

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)
}

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions