diff --git a/internal/conv.go b/internal/conv.go index e96752b..f2abf4e 100644 --- a/internal/conv.go +++ b/internal/conv.go @@ -1,6 +1,7 @@ package internal import ( + "fmt" "reflect" "strconv" ) @@ -32,7 +33,7 @@ func InterfaceToString(i interface{}) (string, error) { } // InterfaceToMap converts interface{} type to map[string]string -func InterfaceTotMap(m interface{}) (map[string]string, error) { +func InterfaceToMap(m interface{}) (map[string]string, error) { result := make(map[string]string) if m == nil { return result, nil @@ -47,13 +48,26 @@ func InterfaceTotMap(m interface{}) (map[string]string, error) { if err != nil { return nil, err } - value, err := InterfaceToString(rv.MapIndex(k).Interface()) - if err != nil { - return nil, err + vvof := reflect.ValueOf(rv.MapIndex(k).Interface()) + switch vvof.Type().Kind() { + case reflect.Map: + innerMap, err := InterfaceToMap(rv.MapIndex(k).Interface()) + if err != nil { + return nil, err + } + for kk, vv := range innerMap { + result[name+"."+kk] = vv + } + case reflect.Slice: + result[name] = fmt.Sprintf("%v", rv.MapIndex(k).Interface()) + default: + vv, err := InterfaceToString(rv.MapIndex(k).Interface()) + if err != nil { + return nil, err + } + result[name] = vv } - result[name] = value } - default: return nil, ErrInvalidParams } diff --git a/internal/conv_test.go b/internal/conv_test.go index 6381bfd..6e17d2c 100644 --- a/internal/conv_test.go +++ b/internal/conv_test.go @@ -31,7 +31,7 @@ func TestInterfaceToString(t *testing.T) { } } -func TestInterfaceTotMap(t *testing.T) { +func TestInterfaceToMap(t *testing.T) { assert := assert.New(t) tests := map[string]struct { @@ -54,15 +54,21 @@ func TestInterfaceTotMap(t *testing.T) { input: map[string]int{"aa": 11, "cc": 22}, output: map[string]string{"aa": "11", "cc": "22"}, }, "map[int]string": { - input: map[int]string{ 11:"aa", 22: "cc"}, output: map[string]string{"11": "aa", "22": "cc"}, + input: map[int]string{11: "aa", 22: "cc"}, output: map[string]string{"11": "aa", "22": "cc"}, }, - "map[interface]interface": { + "map[interface]interface-1": { input: map[interface{}]interface{}{"aa": 11, "cc": 22}, output: map[string]string{"aa": "11", "cc": "22"}, }, + "map[interface]interface-2": { + input: map[interface{}]interface{}{"aa": map[string]int{"bb": 11}, "cc": map[interface{}]interface{}{ + "dd": 22, + }}, + output: map[string]string{"aa.bb": "11", "cc.dd": "22"}, + }, } for _, t := range tests { - output, err := InterfaceTotMap(t.input) + output, err := InterfaceToMap(t.input) assert.Equal(t.isErr, err != nil) if err == nil { for k, v := range output { diff --git a/internal/kubeconfig.go b/internal/kubeconfig.go index d6e5b55..b580962 100644 --- a/internal/kubeconfig.go +++ b/internal/kubeconfig.go @@ -110,11 +110,11 @@ func (k *KubeConfig) GetContexts() ([]*KubeContext, error) { for e1, v1 := range ctx.(map[interface{}]interface{}) { k1, err := InterfaceToString(e1) if err != nil { - continue + return nil, err } switch k1 { case "cluster": - m, err := InterfaceTotMap(v1) + m, err := InterfaceToMap(v1) if err != nil { return nil, err } @@ -153,7 +153,7 @@ func (k *KubeConfig) GetContexts() ([]*KubeContext, error) { } switch k1 { case "context": - m, err := InterfaceTotMap(v1) + m, err := InterfaceToMap(v1) if err != nil { return nil, err }