Skip to content

Commit

Permalink
Fix a bug and typo.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbae1212 committed Feb 3, 2021
1 parent 39b8405 commit 7fd2505
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
26 changes: 20 additions & 6 deletions internal/conv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"fmt"
"reflect"
"strconv"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions internal/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7fd2505

Please sign in to comment.