Fix: the array type cannot be converted to interface type

Signed-off-by: wuzhongjian <wuzhongjian_yewu@cmss.chinamobile.com>
This commit is contained in:
wuzhongjian
2023-02-27 17:45:15 +08:00
parent af10f11225
commit 16f70d7335
+14 -11
View File
@@ -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
}