From 6360d7c97b80ec94df2a0048c8cccaf3d395045d Mon Sep 17 00:00:00 2001 From: Ashutosh Narkar Date: Tue, 5 Oct 2021 12:09:52 -0700 Subject: [PATCH] internal: Update the return value for typed value null 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: #3846 Signed-off-by: Ashutosh Narkar --- internal/config/config_test.go | 45 +++++++++++++++++++++++++++++++++ internal/strvals/parser.go | 2 +- internal/strvals/parser_test.go | 4 +-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 4e8764449a..b5a4018312 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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) + } +} diff --git a/internal/strvals/parser.go b/internal/strvals/parser.go index 6bd4c31b46..b5afafcca0 100644 --- a/internal/strvals/parser.go +++ b/internal/strvals/parser.go @@ -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") { diff --git a/internal/strvals/parser_test.go b/internal/strvals/parser_test.go index 5d77aed18d..fa51a3ba63 100644 --- a/internal/strvals/parser_test.go +++ b/internal/strvals/parser_test.go @@ -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, }, { @@ -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, }, {