// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package gconv import ( "reflect" "github.com/gogf/gf/v2/internal/utils" ) // SliceAny is alias of Interfaces. func SliceAny(any interface{}) []interface{} { return Interfaces(any) } // Interfaces converts `any` to []interface{}. func Interfaces(any interface{}) []interface{} { if any == nil { return nil } if r, ok := any.([]interface{}); ok { return r } var ( array []interface{} = nil ) switch value := any.(type) { case []string: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []int: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []int8: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []int16: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []int32: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []int64: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []uint: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []uint8: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []uint16: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []uint32: for _, v := range value { array = append(array, v) } case []uint64: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []bool: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []float32: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } case []float64: array = make([]interface{}, len(value)) for k, v := range value { array[k] = v } } if array != nil { return array } if v, ok := any.(iInterfaces); ok { return v.Interfaces() } // JSON format string value converting. if checkJsonAndUnmarshalUseNumber(any, &array) { return array } // Not a common type, it then uses reflection for conversion. originValueAndKind := utils.OriginValueAndKind(any) switch originValueAndKind.OriginKind { case reflect.Slice, reflect.Array: var ( length = originValueAndKind.OriginValue.Len() slice = make([]interface{}, length) ) for i := 0; i < length; i++ { slice[i] = originValueAndKind.OriginValue.Index(i).Interface() } return slice default: if originValueAndKind.OriginValue.IsZero() { return []interface{}{} } return []interface{}{any} } }