github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_slice_any.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gconv 8 9 import ( 10 "reflect" 11 12 "github.com/gogf/gf/v2/internal/json" 13 "github.com/gogf/gf/v2/internal/reflection" 14 "github.com/gogf/gf/v2/util/gconv/internal/localinterface" 15 ) 16 17 // SliceAny is alias of Interfaces. 18 func SliceAny(any interface{}) []interface{} { 19 return Interfaces(any) 20 } 21 22 // Interfaces converts `any` to []interface{}. 23 func Interfaces(any interface{}) []interface{} { 24 if any == nil { 25 return nil 26 } 27 var array []interface{} 28 switch value := any.(type) { 29 case []interface{}: 30 array = value 31 case []string: 32 array = make([]interface{}, len(value)) 33 for k, v := range value { 34 array[k] = v 35 } 36 case []int: 37 array = make([]interface{}, len(value)) 38 for k, v := range value { 39 array[k] = v 40 } 41 case []int8: 42 array = make([]interface{}, len(value)) 43 for k, v := range value { 44 array[k] = v 45 } 46 case []int16: 47 array = make([]interface{}, len(value)) 48 for k, v := range value { 49 array[k] = v 50 } 51 case []int32: 52 array = make([]interface{}, len(value)) 53 for k, v := range value { 54 array[k] = v 55 } 56 case []int64: 57 array = make([]interface{}, len(value)) 58 for k, v := range value { 59 array[k] = v 60 } 61 case []uint: 62 array = make([]interface{}, len(value)) 63 for k, v := range value { 64 array[k] = v 65 } 66 case []uint8: 67 if json.Valid(value) { 68 _ = json.UnmarshalUseNumber(value, &array) 69 } else { 70 array = make([]interface{}, len(value)) 71 for k, v := range value { 72 array[k] = v 73 } 74 } 75 case []uint16: 76 array = make([]interface{}, len(value)) 77 for k, v := range value { 78 array[k] = v 79 } 80 case []uint32: 81 for _, v := range value { 82 array = append(array, v) 83 } 84 case []uint64: 85 array = make([]interface{}, len(value)) 86 for k, v := range value { 87 array[k] = v 88 } 89 case []bool: 90 array = make([]interface{}, len(value)) 91 for k, v := range value { 92 array[k] = v 93 } 94 case []float32: 95 array = make([]interface{}, len(value)) 96 for k, v := range value { 97 array[k] = v 98 } 99 case []float64: 100 array = make([]interface{}, len(value)) 101 for k, v := range value { 102 array[k] = v 103 } 104 } 105 if array != nil { 106 return array 107 } 108 if v, ok := any.(localinterface.IInterfaces); ok { 109 return v.Interfaces() 110 } 111 // JSON format string value converting. 112 if checkJsonAndUnmarshalUseNumber(any, &array) { 113 return array 114 } 115 // Not a common type, it then uses reflection for conversion. 116 originValueAndKind := reflection.OriginValueAndKind(any) 117 switch originValueAndKind.OriginKind { 118 case reflect.Slice, reflect.Array: 119 var ( 120 length = originValueAndKind.OriginValue.Len() 121 slice = make([]interface{}, length) 122 ) 123 for i := 0; i < length; i++ { 124 slice[i] = originValueAndKind.OriginValue.Index(i).Interface() 125 } 126 return slice 127 128 default: 129 return []interface{}{any} 130 } 131 }