github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_slice_int.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 // SliceInt is alias of Ints. 17 func SliceInt(any interface{}) []int { 18 return Ints(any) 19 } 20 21 // SliceInt32 is alias of Int32s. 22 func SliceInt32(any interface{}) []int32 { 23 return Int32s(any) 24 } 25 26 // SliceInt64 is alias of Int64s. 27 func SliceInt64(any interface{}) []int64 { 28 return Int64s(any) 29 } 30 31 // Ints converts `any` to []int. 32 func Ints(any interface{}) []int { 33 if any == nil { 34 return nil 35 } 36 var ( 37 array []int = nil 38 ) 39 switch value := any.(type) { 40 case []string: 41 array = make([]int, len(value)) 42 for k, v := range value { 43 array[k] = Int(v) 44 } 45 case []int: 46 array = value 47 case []int8: 48 array = make([]int, len(value)) 49 for k, v := range value { 50 array[k] = int(v) 51 } 52 case []int16: 53 array = make([]int, len(value)) 54 for k, v := range value { 55 array[k] = int(v) 56 } 57 case []int32: 58 array = make([]int, len(value)) 59 for k, v := range value { 60 array[k] = int(v) 61 } 62 case []int64: 63 array = make([]int, len(value)) 64 for k, v := range value { 65 array[k] = int(v) 66 } 67 case []uint: 68 array = make([]int, len(value)) 69 for k, v := range value { 70 array[k] = int(v) 71 } 72 case []uint8: 73 if json.Valid(value) { 74 _ = json.UnmarshalUseNumber(value, &array) 75 } else { 76 array = make([]int, len(value)) 77 for k, v := range value { 78 array[k] = int(v) 79 } 80 } 81 case []uint16: 82 array = make([]int, len(value)) 83 for k, v := range value { 84 array[k] = int(v) 85 } 86 case []uint32: 87 array = make([]int, len(value)) 88 for k, v := range value { 89 array[k] = int(v) 90 } 91 case []uint64: 92 array = make([]int, len(value)) 93 for k, v := range value { 94 array[k] = int(v) 95 } 96 case []bool: 97 array = make([]int, len(value)) 98 for k, v := range value { 99 if v { 100 array[k] = 1 101 } else { 102 array[k] = 0 103 } 104 } 105 case []float32: 106 array = make([]int, len(value)) 107 for k, v := range value { 108 array[k] = Int(v) 109 } 110 case []float64: 111 array = make([]int, len(value)) 112 for k, v := range value { 113 array[k] = Int(v) 114 } 115 case []interface{}: 116 array = make([]int, len(value)) 117 for k, v := range value { 118 array[k] = Int(v) 119 } 120 case [][]byte: 121 array = make([]int, len(value)) 122 for k, v := range value { 123 array[k] = Int(v) 124 } 125 } 126 if array != nil { 127 return array 128 } 129 if v, ok := any.(iInts); ok { 130 return v.Ints() 131 } 132 if v, ok := any.(iInterfaces); ok { 133 return Ints(v.Interfaces()) 134 } 135 // JSON format string value converting. 136 if checkJsonAndUnmarshalUseNumber(any, &array) { 137 return array 138 } 139 // Not a common type, it then uses reflection for conversion. 140 originValueAndKind := reflection.OriginValueAndKind(any) 141 switch originValueAndKind.OriginKind { 142 case reflect.Slice, reflect.Array: 143 var ( 144 length = originValueAndKind.OriginValue.Len() 145 slice = make([]int, length) 146 ) 147 for i := 0; i < length; i++ { 148 slice[i] = Int(originValueAndKind.OriginValue.Index(i).Interface()) 149 } 150 return slice 151 152 default: 153 if originValueAndKind.OriginValue.IsZero() { 154 return []int{} 155 } 156 return []int{Int(any)} 157 } 158 } 159 160 // Int32s converts `any` to []int32. 161 func Int32s(any interface{}) []int32 { 162 if any == nil { 163 return nil 164 } 165 var ( 166 array []int32 = nil 167 ) 168 switch value := any.(type) { 169 case []string: 170 array = make([]int32, len(value)) 171 for k, v := range value { 172 array[k] = Int32(v) 173 } 174 case []int: 175 array = make([]int32, len(value)) 176 for k, v := range value { 177 array[k] = int32(v) 178 } 179 case []int8: 180 array = make([]int32, len(value)) 181 for k, v := range value { 182 array[k] = int32(v) 183 } 184 case []int16: 185 array = make([]int32, len(value)) 186 for k, v := range value { 187 array[k] = int32(v) 188 } 189 case []int32: 190 array = value 191 case []int64: 192 array = make([]int32, len(value)) 193 for k, v := range value { 194 array[k] = int32(v) 195 } 196 case []uint: 197 array = make([]int32, len(value)) 198 for k, v := range value { 199 array[k] = int32(v) 200 } 201 case []uint8: 202 if json.Valid(value) { 203 _ = json.UnmarshalUseNumber(value, &array) 204 } else { 205 array = make([]int32, len(value)) 206 for k, v := range value { 207 array[k] = int32(v) 208 } 209 } 210 case []uint16: 211 array = make([]int32, len(value)) 212 for k, v := range value { 213 array[k] = int32(v) 214 } 215 case []uint32: 216 array = make([]int32, len(value)) 217 for k, v := range value { 218 array[k] = int32(v) 219 } 220 case []uint64: 221 array = make([]int32, len(value)) 222 for k, v := range value { 223 array[k] = int32(v) 224 } 225 case []bool: 226 array = make([]int32, len(value)) 227 for k, v := range value { 228 if v { 229 array[k] = 1 230 } else { 231 array[k] = 0 232 } 233 } 234 case []float32: 235 array = make([]int32, len(value)) 236 for k, v := range value { 237 array[k] = Int32(v) 238 } 239 case []float64: 240 array = make([]int32, len(value)) 241 for k, v := range value { 242 array[k] = Int32(v) 243 } 244 case []interface{}: 245 array = make([]int32, len(value)) 246 for k, v := range value { 247 array[k] = Int32(v) 248 } 249 case [][]byte: 250 array = make([]int32, len(value)) 251 for k, v := range value { 252 array[k] = Int32(v) 253 } 254 } 255 if array != nil { 256 return array 257 } 258 if v, ok := any.(iInts); ok { 259 return Int32s(v.Ints()) 260 } 261 if v, ok := any.(iInterfaces); ok { 262 return Int32s(v.Interfaces()) 263 } 264 // JSON format string value converting. 265 if checkJsonAndUnmarshalUseNumber(any, &array) { 266 return array 267 } 268 // Not a common type, it then uses reflection for conversion. 269 originValueAndKind := reflection.OriginValueAndKind(any) 270 switch originValueAndKind.OriginKind { 271 case reflect.Slice, reflect.Array: 272 var ( 273 length = originValueAndKind.OriginValue.Len() 274 slice = make([]int32, length) 275 ) 276 for i := 0; i < length; i++ { 277 slice[i] = Int32(originValueAndKind.OriginValue.Index(i).Interface()) 278 } 279 return slice 280 281 default: 282 if originValueAndKind.OriginValue.IsZero() { 283 return []int32{} 284 } 285 return []int32{Int32(any)} 286 } 287 } 288 289 // Int64s converts `any` to []int64. 290 func Int64s(any interface{}) []int64 { 291 if any == nil { 292 return nil 293 } 294 var ( 295 array []int64 = nil 296 ) 297 switch value := any.(type) { 298 case []string: 299 array = make([]int64, len(value)) 300 for k, v := range value { 301 array[k] = Int64(v) 302 } 303 case []int: 304 array = make([]int64, len(value)) 305 for k, v := range value { 306 array[k] = int64(v) 307 } 308 case []int8: 309 array = make([]int64, len(value)) 310 for k, v := range value { 311 array[k] = int64(v) 312 } 313 case []int16: 314 array = make([]int64, len(value)) 315 for k, v := range value { 316 array[k] = int64(v) 317 } 318 case []int32: 319 array = make([]int64, len(value)) 320 for k, v := range value { 321 array[k] = int64(v) 322 } 323 case []int64: 324 array = value 325 case []uint: 326 array = make([]int64, len(value)) 327 for k, v := range value { 328 array[k] = int64(v) 329 } 330 case []uint8: 331 if json.Valid(value) { 332 _ = json.UnmarshalUseNumber(value, &array) 333 } else { 334 array = make([]int64, len(value)) 335 for k, v := range value { 336 array[k] = int64(v) 337 } 338 } 339 case []uint16: 340 array = make([]int64, len(value)) 341 for k, v := range value { 342 array[k] = int64(v) 343 } 344 case []uint32: 345 array = make([]int64, len(value)) 346 for k, v := range value { 347 array[k] = int64(v) 348 } 349 case []uint64: 350 array = make([]int64, len(value)) 351 for k, v := range value { 352 array[k] = int64(v) 353 } 354 case []bool: 355 array = make([]int64, len(value)) 356 for k, v := range value { 357 if v { 358 array[k] = 1 359 } else { 360 array[k] = 0 361 } 362 } 363 case []float32: 364 array = make([]int64, len(value)) 365 for k, v := range value { 366 array[k] = Int64(v) 367 } 368 case []float64: 369 array = make([]int64, len(value)) 370 for k, v := range value { 371 array[k] = Int64(v) 372 } 373 case []interface{}: 374 array = make([]int64, len(value)) 375 for k, v := range value { 376 array[k] = Int64(v) 377 } 378 case [][]byte: 379 array = make([]int64, len(value)) 380 for k, v := range value { 381 array[k] = Int64(v) 382 } 383 } 384 if array != nil { 385 return array 386 } 387 if v, ok := any.(iInts); ok { 388 return Int64s(v.Ints()) 389 } 390 if v, ok := any.(iInterfaces); ok { 391 return Int64s(v.Interfaces()) 392 } 393 // JSON format string value converting. 394 if checkJsonAndUnmarshalUseNumber(any, &array) { 395 return array 396 } 397 // Not a common type, it then uses reflection for conversion. 398 originValueAndKind := reflection.OriginValueAndKind(any) 399 switch originValueAndKind.OriginKind { 400 case reflect.Slice, reflect.Array: 401 var ( 402 length = originValueAndKind.OriginValue.Len() 403 slice = make([]int64, length) 404 ) 405 for i := 0; i < length; i++ { 406 slice[i] = Int64(originValueAndKind.OriginValue.Index(i).Interface()) 407 } 408 return slice 409 410 default: 411 if originValueAndKind.OriginValue.IsZero() { 412 return []int64{} 413 } 414 return []int64{Int64(any)} 415 } 416 }