Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfg.Unpack panics for existing maps #167

Open
simitt opened this issue May 26, 2020 · 0 comments
Open

cfg.Unpack panics for existing maps #167

simitt opened this issue May 26, 2020 · 0 comments

Comments

@simitt
Copy link

simitt commented May 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants