Skip to content

Commit

Permalink
feat: support object,list(object) variable types
Browse files Browse the repository at this point in the history
Closes: #233
Signed-off-by: Evan Li <[email protected]>
  • Loading branch information
evanli18 committed Jan 27, 2022
1 parent 01e3ac0 commit c231477
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
17 changes: 5 additions & 12 deletions controllers/configuration/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 25 additions & 3 deletions controllers/configuration/convert_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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,
},
},
Expand Down

0 comments on commit c231477

Please sign in to comment.