Skip to content

Commit

Permalink
internal: Update the return value for typed value null
Browse files Browse the repository at this point in the history
This commit updates the parser for --set options
to return empty struct as return value when null
is specified. This is needed to set an empty object
with the CLI overrides for a null typed value.

Fixes: open-policy-agent#3846

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
  • Loading branch information
ashutosh-narkar committed Oct 6, 2021
1 parent 02a3bf0 commit 6360d7c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
45 changes: 45 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,48 @@ func TestLoadConfigWithParamOverrideNoConfigFile(t *testing.T) {
t.Errorf("config does not match expected:\n\nExpected: %+v\nActual: %+v", expected, config)
}
}

func TestLoadConfigWithParamOverrideNoConfigFileWithEmptyObject(t *testing.T) {
configOverrides := []string{
"services.acmecorp.url=https://example.com/control-plane-api/v1",
"services.acmecorp.headers=null",
"services.acmecorp.credentials.s3_signing.environment_credentials=null",
"decision_logs.plugin=my_plugin",
"plugins.my_plugin=null",
}

configBytes, err := Load("", configOverrides, nil)
if err != nil {
t.Errorf("unexpected error loading config: " + err.Error())
}

config := map[string]interface{}{}
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
t.Errorf("unexpected error unmarshalling config")
}

expected := map[string]interface{}{
"services": map[string]interface{}{
"acmecorp": map[string]interface{}{
"url": "https://example.com/control-plane-api/v1",
"headers": map[string]interface{}{},
"credentials": map[string]interface{}{
"s3_signing": map[string]interface{}{
"environment_credentials": map[string]interface{}{},
},
},
},
},
"decision_logs": map[string]interface{}{
"plugin": "my_plugin",
},
"plugins": map[string]interface{}{
"my_plugin": map[string]interface{}{},
},
}

if !reflect.DeepEqual(config, expected) {
t.Errorf("config does not match expected:\n\nExpected: %+v\nActual: %+v", expected, config)
}
}
2 changes: 1 addition & 1 deletion internal/strvals/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func typedVal(v []rune, st bool) interface{} {
}

if strings.EqualFold(val, "null") {
return nil
return struct{}{}
}

if strings.EqualFold(val, "0") {
Expand Down
4 changes: 2 additions & 2 deletions internal/strvals/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestParseSet(t *testing.T) {
}{
{
"name1=null,f=false,t=true",
map[string]interface{}{"name1": nil, "f": false, "t": true},
map[string]interface{}{"name1": map[string]interface{}{}, "f": false, "t": true},
false,
},
{
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestParseSet(t *testing.T) {
},
{
str: "is_null=null",
expect: map[string]interface{}{"is_null": nil},
expect: map[string]interface{}{"is_null": map[string]interface{}{}},
err: false,
},
{
Expand Down

0 comments on commit 6360d7c

Please sign in to comment.