From c231477d77b33b704b4304acadef35b2b124b9d7 Mon Sep 17 00:00:00 2001 From: Evan Li Date: Thu, 27 Jan 2022 18:23:37 +0800 Subject: [PATCH] feat: support object,list(object) variable types Closes: #233 Signed-off-by: Evan Li --- controllers/configuration/convert.go | 17 ++++---------- controllers/configuration/convert_test.go | 28 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/controllers/configuration/convert.go b/controllers/configuration/convert.go index 727cd967..6dc55a84 100644 --- a/controllers/configuration/convert.go +++ b/controllers/configuration/convert.go @@ -95,19 +95,12 @@ func Interface2String(v interface{}) (string, error) { value = fmt.Sprint(v) case bool: value = strconv.FormatBool(v) - case []interface{}: - var tmp string - for _, i := range v { - switch i.(type) { - case string: - tmp += fmt.Sprintf("\"%v\", ", i) - case int, bool: - tmp += fmt.Sprintf("%v, ", i) - } - } - value = fmt.Sprintf(`[%s]`, tmp) default: - return "", fmt.Errorf("could not convert %v to string", v) + valuejson, err := json.Marshal(v) + if err != nil { + return "", fmt.Errorf("cloud not convert %v to string", v) + } + value = string(valuejson) } return value, nil } diff --git a/controllers/configuration/convert_test.go b/controllers/configuration/convert_test.go index a154f614..60a423e4 100644 --- a/controllers/configuration/convert_test.go +++ b/controllers/configuration/convert_test.go @@ -1,10 +1,11 @@ package configuration import ( + "testing" + "gotest.tools/assert" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/yaml" - "testing" ) func TestRawExtension2Map(t *testing.T) { @@ -97,17 +98,38 @@ func TestInterface2String(t *testing.T) { err: nil, }, }, + "NumberType": { + variable: 1024.1, + want: want{ + result: "1024.1", + err: nil, + }, + }, "ListType1": { variable: []interface{}{"Will", "Catherine"}, want: want{ - result: "[\"Will\", \"Catherine\", ]", + result: `["Will","Catherine"]`, err: nil, }, }, "ListType2": { variable: []interface{}{123, 456}, want: want{ - result: "[123, 456, ]", + result: `[123,456]`, + err: nil, + }, + }, + "ObjectType": { + variable: struct{ Name string }{"Terraform"}, + want: want{ + result: `{"Name":"Terraform"}`, + err: nil, + }, + }, + "ListObjectType": { + variable: []struct{ Name string }{{"Terraform"}, {"OAM"}, {"Vela"}}, + want: want{ + result: `[{"Name":"Terraform"},{"Name":"OAM"},{"Name":"Vela"}]`, err: nil, }, },