Repository: sacloud/usacloud (go)

Issue:
フィルタのスライス対応
from #619 

`pkg/vdev`に登録するフィルタ(mapconv用)において、元の値がスライスだった場合に対応できていない。

例: AutoBackupの`Weekdays`: `[]string` => `[]types.EBackupSpanWeekday`

Pull request description:
Support a slice/array by ConverterFilter
closes #620 

フィルタをslice/arrayに対応させる。


Patch:
diff --git a/pkg/vdef/definitions.go b/pkg/vdef/definitions.go
index 7ac5ee24..4136d8a0 100644
--- a/pkg/vdef/definitions.go
+++ b/pkg/vdef/definitions.go
@@ -140,7 +140,22 @@ func registerCLITagOptions() {
 }
 
 func convertFuncToValue(defName string, defs []*definition) mapconv.FilterFunc {
-	return func(v interface{}) (interface{}, error) {
+	var fn func(interface{}) (interface{}, error)
+	fn = func(v interface{}) (interface{}, error) {
+		// スライスの場合は再帰処理
+		vt := reflect.ValueOf(v)
+		if vt.Kind() == reflect.Slice || vt.Kind() == reflect.Array {
+			var results []interface{}
+			for i := 0; i < vt.Len(); i++ {
+				res, err := fn(vt.Index(i).Interface())
+				if err != nil {
+					return nil, err
+				}
+				results = append(results, res)
+			}
+			return results, nil
+		}
+
 		var result interface{}
 		for _, def := range defs {
 			if reflect.DeepEqual(v, def.key) {
@@ -153,10 +168,26 @@ func convertFuncToValue(defName string, defs []*definition) mapconv.FilterFunc {
 		}
 		return result, nil
 	}
+	return fn
 }
 
 func convertFuncToKey(defName string, defs []*definition) mapconv.FilterFunc {
-	return func(v interface{}) (interface{}, error) {
+	var fn func(interface{}) (interface{}, error)
+	fn = func(v interface{}) (interface{}, error) {
+		// スライスの場合は再帰処理
+		vt := reflect.ValueOf(v)
+		if vt.Kind() == reflect.Slice || vt.Kind() == reflect.Array {
+			var results []interface{}
+			for i := 0; i < vt.Len(); i++ {
+				res, err := fn(vt.Index(i).Interface())
+				if err != nil {
+					return nil, err
+				}
+				results = append(results, res)
+			}
+			return results, nil
+		}
+
 		var result interface{}
 		for _, def := range defs {
 			if reflect.DeepEqual(v, def.value) {
@@ -169,6 +200,7 @@ func convertFuncToKey(defName string, defs []*definition) mapconv.FilterFunc {
 		}
 		return result, nil
 	}
+	return fn
 }
 
 func templateFuncToValue(defs []*definition) func(interface{}) interface{} {


Test patch:
diff --git a/pkg/vdef/definitions_test.go b/pkg/vdef/definitions_test.go
index 3db0c760..0c86bc9f 100644
--- a/pkg/vdef/definitions_test.go
+++ b/pkg/vdef/definitions_test.go
@@ -72,6 +72,10 @@ func TestConverterFuncGenerator(t *testing.T) {
 				in:  int64(1),
 				err: "key 1 not found in test",
 			},
+			{
+				in:     []interface{}{"string", 1},
+				expect: []interface{}{"stringValue", "intValue"},
+			},
 			{
 				in:     "foobar",
 				expect: nil,
@@ -115,6 +119,10 @@ func TestConverterFuncGenerator(t *testing.T) {
 				expect: "intKey",
 				err:    `value 1 not found in test`,
 			},
+			{
+				in:     []interface{}{"stringValue", "intValue"},
+				expect: []interface{}{"string", 1},
+			},
 			{
 				in:  "foobar",
 				err: `value foobar not found in test`,