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