From 16f70d7335f9906c6bfeb22369ec6bc4fdde0d24 Mon Sep 17 00:00:00 2001 From: wuzhongjian Date: Mon, 27 Feb 2023 17:45:15 +0800 Subject: [PATCH] Fix: the array type cannot be converted to interface type Signed-off-by: wuzhongjian --- pkg/apiserver/domain/service/util.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/apiserver/domain/service/util.go b/pkg/apiserver/domain/service/util.go index 3af1c879f..65f2dc416 100644 --- a/pkg/apiserver/domain/service/util.go +++ b/pkg/apiserver/domain/service/util.go @@ -17,7 +17,7 @@ limitations under the License. package service import ( - "fmt" + "reflect" ) // guaranteePolicyExist check the slice whether contain the target policy, if not put it in. @@ -57,16 +57,19 @@ func extractPolicyListAndProperty(property map[string]interface{}) ([]string, ma if policies == nil { return nil, property, nil } - list, ok := policies.([]interface{}) - if !ok { - return nil, nil, fmt.Errorf("the policies incorrect") - } - if len(list) == 0 { - return nil, property, nil - } - res := []string{} - for _, i := range list { - res = append(res, i.(string)) + // In mongodb, the storage type of policies is array, + // but the array type cannot be converted to interface type, + // so we can get the policies by reflection. + var res []string + kind := reflect.TypeOf(policies).Kind() + switch kind { + case reflect.Slice, reflect.Array: + s := reflect.ValueOf(policies) + for i := 0; i < s.Len(); i++ { + res = append(res, s.Index(i).Interface().(string)) + } + default: + // other type not supported } return res, property, nil }