github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_slice_str.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/wangyougui/gf. 6 7 package gconv 8 9 import ( 10 "reflect" 11 12 "github.com/wangyougui/gf/v2/internal/json" 13 "github.com/wangyougui/gf/v2/internal/reflection" 14 ) 15 16 // SliceStr is alias of Strings. 17 func SliceStr(any interface{}) []string { 18 return Strings(any) 19 } 20 21 // Strings converts `any` to []string. 22 func Strings(any interface{}) []string { 23 if any == nil { 24 return nil 25 } 26 var ( 27 array []string = nil 28 ) 29 switch value := any.(type) { 30 case []int: 31 array = make([]string, len(value)) 32 for k, v := range value { 33 array[k] = String(v) 34 } 35 case []int8: 36 array = make([]string, len(value)) 37 for k, v := range value { 38 array[k] = String(v) 39 } 40 case []int16: 41 array = make([]string, len(value)) 42 for k, v := range value { 43 array[k] = String(v) 44 } 45 case []int32: 46 array = make([]string, len(value)) 47 for k, v := range value { 48 array[k] = String(v) 49 } 50 case []int64: 51 array = make([]string, len(value)) 52 for k, v := range value { 53 array[k] = String(v) 54 } 55 case []uint: 56 array = make([]string, len(value)) 57 for k, v := range value { 58 array[k] = String(v) 59 } 60 case []uint8: 61 if json.Valid(value) { 62 _ = json.UnmarshalUseNumber(value, &array) 63 } else { 64 array = make([]string, len(value)) 65 for k, v := range value { 66 array[k] = String(v) 67 } 68 } 69 case []uint16: 70 array = make([]string, len(value)) 71 for k, v := range value { 72 array[k] = String(v) 73 } 74 case []uint32: 75 array = make([]string, len(value)) 76 for k, v := range value { 77 array[k] = String(v) 78 } 79 case []uint64: 80 array = make([]string, len(value)) 81 for k, v := range value { 82 array[k] = String(v) 83 } 84 case []bool: 85 array = make([]string, len(value)) 86 for k, v := range value { 87 array[k] = String(v) 88 } 89 case []float32: 90 array = make([]string, len(value)) 91 for k, v := range value { 92 array[k] = String(v) 93 } 94 case []float64: 95 array = make([]string, len(value)) 96 for k, v := range value { 97 array[k] = String(v) 98 } 99 case []interface{}: 100 array = make([]string, len(value)) 101 for k, v := range value { 102 array[k] = String(v) 103 } 104 case []string: 105 array = value 106 case [][]byte: 107 array = make([]string, len(value)) 108 for k, v := range value { 109 array[k] = String(v) 110 } 111 } 112 if array != nil { 113 return array 114 } 115 if v, ok := any.(iStrings); ok { 116 return v.Strings() 117 } 118 if v, ok := any.(iInterfaces); ok { 119 return Strings(v.Interfaces()) 120 } 121 // JSON format string value converting. 122 if checkJsonAndUnmarshalUseNumber(any, &array) { 123 return array 124 } 125 // Not a common type, it then uses reflection for conversion. 126 originValueAndKind := reflection.OriginValueAndKind(any) 127 switch originValueAndKind.OriginKind { 128 case reflect.Slice, reflect.Array: 129 var ( 130 length = originValueAndKind.OriginValue.Len() 131 slice = make([]string, length) 132 ) 133 for i := 0; i < length; i++ { 134 slice[i] = String(originValueAndKind.OriginValue.Index(i).Interface()) 135 } 136 return slice 137 138 default: 139 if originValueAndKind.OriginValue.IsZero() { 140 return []string{} 141 } 142 return []string{String(any)} 143 } 144 }