github.com/andeya/ameda@v1.5.3/interface.go (about) 1 package ameda 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 // InterfaceToInterfacePtr converts interface to *interface. 9 func InterfaceToInterfacePtr(i interface{}) *interface{} { 10 return &i 11 } 12 13 // InterfaceToString converts interface to string. 14 func InterfaceToString(i interface{}) string { 15 return fmt.Sprintf("%v", i) 16 } 17 18 // InterfaceToStringPtr converts interface to *string. 19 func InterfaceToStringPtr(i interface{}) *string { 20 v := InterfaceToString(i) 21 return &v 22 } 23 24 // InterfaceToBool converts interface to bool. 25 // NOTE: 26 // 27 // 0 is false, other numbers are true 28 func InterfaceToBool(i interface{}, emptyAsFalse ...bool) (bool, error) { 29 switch v := i.(type) { 30 case bool: 31 return v, nil 32 case nil: 33 return false, nil 34 case float32: 35 return Float32ToBool(v), nil 36 case float64: 37 return Float64ToBool(v), nil 38 case int: 39 return IntToBool(v), nil 40 case int8: 41 return Int8ToBool(v), nil 42 case int16: 43 return Int16ToBool(v), nil 44 case int32: 45 return Int32ToBool(v), nil 46 case int64: 47 return Int64ToBool(v), nil 48 case uint: 49 return UintToBool(v), nil 50 case uint8: 51 return Uint8ToBool(v), nil 52 case uint16: 53 return Uint16ToBool(v), nil 54 case uint32: 55 return Uint32ToBool(v), nil 56 case uint64: 57 return Uint64ToBool(v), nil 58 case uintptr: 59 return v != 0, nil 60 case string: 61 return StringToBool(v, emptyAsFalse...) 62 default: 63 r := IndirectValue(reflect.ValueOf(i)) 64 switch r.Kind() { 65 case reflect.Bool: 66 return r.Bool(), nil 67 case reflect.Invalid: 68 return false, nil 69 case reflect.Float32, reflect.Float64: 70 return Float64ToBool(r.Float()), nil 71 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 72 return Int64ToBool(r.Int()), nil 73 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 74 return Uint64ToBool(r.Uint()), nil 75 case reflect.String: 76 return StringToBool(r.String(), emptyAsFalse...) 77 } 78 if isEmptyAsZero(emptyAsFalse) { 79 return !isZero(r), nil 80 } 81 return false, fmt.Errorf("cannot convert %#v of type %T to bool", i, i) 82 } 83 } 84 85 // InterfaceToBoolPtr converts interface to *bool. 86 // NOTE: 87 // 88 // 0 is false, other numbers are true 89 func InterfaceToBoolPtr(i interface{}, emptyAsFalse ...bool) (*bool, error) { 90 r, err := InterfaceToBool(i, emptyAsFalse...) 91 return &r, err 92 } 93 94 // InterfaceToFloat32 converts interface to float32. 95 func InterfaceToFloat32(i interface{}, emptyStringAsZero ...bool) (float32, error) { 96 switch v := i.(type) { 97 case bool: 98 return BoolToFloat32(v), nil 99 case nil: 100 return 0, nil 101 case int: 102 return IntToFloat32(v), nil 103 case int8: 104 return Int8ToFloat32(v), nil 105 case int16: 106 return Int16ToFloat32(v), nil 107 case int32: 108 return Int32ToFloat32(v), nil 109 case int64: 110 return Int64ToFloat32(v), nil 111 case uint: 112 return UintToFloat32(v), nil 113 case uint8: 114 return Uint8ToFloat32(v), nil 115 case uint16: 116 return Uint16ToFloat32(v), nil 117 case uint32: 118 return Uint32ToFloat32(v), nil 119 case uint64: 120 return Uint64ToFloat32(v), nil 121 case uintptr: 122 return UintToFloat32(uint(v)), nil 123 case string: 124 return StringToFloat32(v, emptyStringAsZero...) 125 default: 126 r := IndirectValue(reflect.ValueOf(i)) 127 switch r.Kind() { 128 case reflect.Bool: 129 return BoolToFloat32(r.Bool()), nil 130 case reflect.Invalid: 131 return 0, nil 132 case reflect.Float32: 133 return float32(r.Float()), nil 134 case reflect.Float64: 135 return Float64ToFloat32(r.Float()) 136 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 137 return Int64ToFloat32(r.Int()), nil 138 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 139 return Uint64ToFloat32(r.Uint()), nil 140 case reflect.String: 141 return StringToFloat32(r.String(), emptyStringAsZero...) 142 } 143 if isEmptyAsZero(emptyStringAsZero) { 144 return BoolToFloat32(!isZero(r)), nil 145 } 146 return 0, fmt.Errorf("cannot convert %#v of type %T to float32", i, i) 147 } 148 } 149 150 // InterfaceToFloat32Ptr converts interface to *float32. 151 func InterfaceToFloat32Ptr(i interface{}, emptyAsFalse ...bool) (*float32, error) { 152 r, err := InterfaceToFloat32(i, emptyAsFalse...) 153 return &r, err 154 } 155 156 // InterfaceToFloat64 converts interface to float64. 157 func InterfaceToFloat64(i interface{}, emptyStringAsZero ...bool) (float64, error) { 158 switch v := i.(type) { 159 case bool: 160 return BoolToFloat64(v), nil 161 case nil: 162 return 0, nil 163 case int: 164 return IntToFloat64(v), nil 165 case int8: 166 return Int8ToFloat64(v), nil 167 case int16: 168 return Int16ToFloat64(v), nil 169 case int32: 170 return Int32ToFloat64(v), nil 171 case int64: 172 return Int64ToFloat64(v), nil 173 case uint: 174 return UintToFloat64(v), nil 175 case uint8: 176 return Uint8ToFloat64(v), nil 177 case uint16: 178 return Uint16ToFloat64(v), nil 179 case uint32: 180 return Uint32ToFloat64(v), nil 181 case uint64: 182 return Uint64ToFloat64(v), nil 183 case uintptr: 184 return UintToFloat64(uint(v)), nil 185 case string: 186 return StringToFloat64(v, emptyStringAsZero...) 187 default: 188 r := IndirectValue(reflect.ValueOf(i)) 189 switch r.Kind() { 190 case reflect.Bool: 191 return BoolToFloat64(r.Bool()), nil 192 case reflect.Invalid: 193 return 0, nil 194 case reflect.Float32, reflect.Float64: 195 return r.Float(), nil 196 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 197 return Int64ToFloat64(r.Int()), nil 198 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 199 return Uint64ToFloat64(r.Uint()), nil 200 case reflect.String: 201 return StringToFloat64(r.String(), emptyStringAsZero...) 202 } 203 if isEmptyAsZero(emptyStringAsZero) { 204 return BoolToFloat64(!isZero(r)), nil 205 } 206 return 0, fmt.Errorf("cannot convert %#v of type %T to float64", i, i) 207 } 208 } 209 210 // InterfaceToFloat64Ptr converts interface to *float64. 211 func InterfaceToFloat64Ptr(i interface{}, emptyAsFalse ...bool) (*float64, error) { 212 r, err := InterfaceToFloat64(i, emptyAsFalse...) 213 return &r, err 214 } 215 216 // InterfaceToInt converts interface to int. 217 func InterfaceToInt(i interface{}, emptyStringAsZero ...bool) (int, error) { 218 switch v := i.(type) { 219 case bool: 220 return BoolToInt(v), nil 221 case nil: 222 return 0, nil 223 case int: 224 return v, nil 225 case int8: 226 return Int8ToInt(v), nil 227 case int16: 228 return Int16ToInt(v), nil 229 case int32: 230 return Int32ToInt(v), nil 231 case int64: 232 return Int64ToInt(v) 233 case uint: 234 return UintToInt(v) 235 case uint8: 236 return Uint8ToInt(v), nil 237 case uint16: 238 return Uint16ToInt(v), nil 239 case uint32: 240 return Uint32ToInt(v), nil 241 case uint64: 242 return Uint64ToInt(v), nil 243 case uintptr: 244 return UintToInt(uint(v)) 245 case string: 246 return StringToInt(v, emptyStringAsZero...) 247 default: 248 r := IndirectValue(reflect.ValueOf(i)) 249 switch r.Kind() { 250 case reflect.Bool: 251 return BoolToInt(r.Bool()), nil 252 case reflect.Invalid: 253 return 0, nil 254 case reflect.Float32, reflect.Float64: 255 return Float64ToInt(r.Float()) 256 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 257 return Int64ToInt(r.Int()) 258 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 259 return Uint64ToInt(r.Uint()), nil 260 case reflect.String: 261 return StringToInt(r.String(), emptyStringAsZero...) 262 } 263 if isEmptyAsZero(emptyStringAsZero) { 264 return BoolToInt(!isZero(r)), nil 265 } 266 return 0, fmt.Errorf("cannot convert %#v of type %T to int", i, i) 267 } 268 } 269 270 // InterfaceToIntPtr converts interface to *float64. 271 func InterfaceToIntPtr(i interface{}, emptyAsFalse ...bool) (*int, error) { 272 r, err := InterfaceToInt(i, emptyAsFalse...) 273 return &r, err 274 } 275 276 // InterfaceToInt8 converts interface to int8. 277 func InterfaceToInt8(i interface{}, emptyStringAsZero ...bool) (int8, error) { 278 switch v := i.(type) { 279 case bool: 280 return BoolToInt8(v), nil 281 case nil: 282 return 0, nil 283 case int: 284 return IntToInt8(v) 285 case int8: 286 return v, nil 287 case int16: 288 return Int16ToInt8(v) 289 case int32: 290 return Int32ToInt8(v) 291 case int64: 292 return Int64ToInt8(v) 293 case uint: 294 return UintToInt8(v) 295 case uint8: 296 return Uint8ToInt8(v) 297 case uint16: 298 return Uint16ToInt8(v) 299 case uint32: 300 return Uint32ToInt8(v) 301 case uint64: 302 return Uint64ToInt8(v) 303 case uintptr: 304 return UintToInt8(uint(v)) 305 case string: 306 return StringToInt8(v, emptyStringAsZero...) 307 default: 308 r := IndirectValue(reflect.ValueOf(i)) 309 switch r.Kind() { 310 case reflect.Bool: 311 return BoolToInt8(r.Bool()), nil 312 case reflect.Invalid: 313 return 0, nil 314 case reflect.Float32, reflect.Float64: 315 return Float64ToInt8(r.Float()) 316 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 317 return Int64ToInt8(r.Int()) 318 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 319 return Uint64ToInt8(r.Uint()) 320 case reflect.String: 321 return StringToInt8(r.String(), emptyStringAsZero...) 322 } 323 if isEmptyAsZero(emptyStringAsZero) { 324 return BoolToInt8(!isZero(r)), nil 325 } 326 return 0, fmt.Errorf("cannot convert %#v of type %T to int8", i, i) 327 } 328 } 329 330 // InterfaceToInt8Ptr converts interface to *int8. 331 func InterfaceToInt8Ptr(i interface{}, emptyAsFalse ...bool) (*int8, error) { 332 r, err := InterfaceToInt8(i, emptyAsFalse...) 333 return &r, err 334 } 335 336 // InterfaceToInt16 converts interface to int16. 337 func InterfaceToInt16(i interface{}, emptyStringAsZero ...bool) (int16, error) { 338 switch v := i.(type) { 339 case bool: 340 return BoolToInt16(v), nil 341 case nil: 342 return 0, nil 343 case int: 344 return IntToInt16(v) 345 case int8: 346 return Int8ToInt16(v), nil 347 case int16: 348 return v, nil 349 case int32: 350 return Int32ToInt16(v) 351 case int64: 352 return Int64ToInt16(v) 353 case uint: 354 return UintToInt16(v) 355 case uint8: 356 return Uint8ToInt16(v), nil 357 case uint16: 358 return Uint16ToInt16(v) 359 case uint32: 360 return Uint32ToInt16(v) 361 case uint64: 362 return Uint64ToInt16(v) 363 case uintptr: 364 return UintToInt16(uint(v)) 365 case string: 366 return StringToInt16(v, emptyStringAsZero...) 367 default: 368 r := IndirectValue(reflect.ValueOf(i)) 369 switch r.Kind() { 370 case reflect.Bool: 371 return BoolToInt16(r.Bool()), nil 372 case reflect.Invalid: 373 return 0, nil 374 case reflect.Float32, reflect.Float64: 375 return Float64ToInt16(r.Float()) 376 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 377 return Int64ToInt16(r.Int()) 378 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 379 return Uint64ToInt16(r.Uint()) 380 case reflect.String: 381 return StringToInt16(r.String(), emptyStringAsZero...) 382 } 383 if isEmptyAsZero(emptyStringAsZero) { 384 return BoolToInt16(!isZero(r)), nil 385 } 386 return 0, fmt.Errorf("cannot convert %#v of type %T to int16", i, i) 387 } 388 } 389 390 // InterfaceToInt16Ptr converts interface to *int16. 391 func InterfaceToInt16Ptr(i interface{}, emptyAsFalse ...bool) (*int16, error) { 392 r, err := InterfaceToInt16(i, emptyAsFalse...) 393 return &r, err 394 } 395 396 // InterfaceToInt32 converts interface to int32. 397 func InterfaceToInt32(i interface{}, emptyStringAsZero ...bool) (int32, error) { 398 switch v := i.(type) { 399 case bool: 400 return BoolToInt32(v), nil 401 case nil: 402 return 0, nil 403 case int: 404 return IntToInt32(v) 405 case int8: 406 return Int8ToInt32(v), nil 407 case int16: 408 return Int16ToInt32(v), nil 409 case int32: 410 return v, nil 411 case int64: 412 return Int64ToInt32(v) 413 case uint: 414 return UintToInt32(v) 415 case uint8: 416 return Uint8ToInt32(v), nil 417 case uint16: 418 return Uint16ToInt32(v), nil 419 case uint32: 420 return Uint32ToInt32(v) 421 case uint64: 422 return Uint64ToInt32(v) 423 case uintptr: 424 return UintToInt32(uint(v)) 425 case string: 426 return StringToInt32(v, emptyStringAsZero...) 427 default: 428 r := IndirectValue(reflect.ValueOf(i)) 429 switch r.Kind() { 430 case reflect.Bool: 431 return BoolToInt32(r.Bool()), nil 432 case reflect.Invalid: 433 return 0, nil 434 case reflect.Float32, reflect.Float64: 435 return Float64ToInt32(r.Float()) 436 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 437 return Int64ToInt32(r.Int()) 438 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 439 return Uint64ToInt32(r.Uint()) 440 case reflect.String: 441 return StringToInt32(r.String(), emptyStringAsZero...) 442 } 443 if isEmptyAsZero(emptyStringAsZero) { 444 return BoolToInt32(!isZero(r)), nil 445 } 446 return 0, fmt.Errorf("cannot convert %#v of type %T to int32", i, i) 447 } 448 } 449 450 // InterfaceToInt32Ptr converts interface to *int32. 451 func InterfaceToInt32Ptr(i interface{}, emptyAsFalse ...bool) (*int32, error) { 452 r, err := InterfaceToInt32(i, emptyAsFalse...) 453 return &r, err 454 } 455 456 // InterfaceToInt64 converts interface to int64. 457 func InterfaceToInt64(i interface{}, emptyStringAsZero ...bool) (int64, error) { 458 switch v := i.(type) { 459 case bool: 460 return BoolToInt64(v), nil 461 case nil: 462 return 0, nil 463 case int: 464 return IntToInt64(v), nil 465 case int8: 466 return Int8ToInt64(v), nil 467 case int16: 468 return Int16ToInt64(v), nil 469 case int32: 470 return Int32ToInt64(v), nil 471 case int64: 472 return v, nil 473 case uint: 474 return UintToInt64(v) 475 case uint8: 476 return Uint8ToInt64(v), nil 477 case uint16: 478 return Uint16ToInt64(v), nil 479 case uint32: 480 return Uint32ToInt64(v), nil 481 case uint64: 482 return Uint64ToInt64(v) 483 case uintptr: 484 return UintToInt64(uint(v)) 485 case string: 486 return StringToInt64(v, emptyStringAsZero...) 487 default: 488 r := IndirectValue(reflect.ValueOf(i)) 489 switch r.Kind() { 490 case reflect.Bool: 491 return BoolToInt64(r.Bool()), nil 492 case reflect.Invalid: 493 return 0, nil 494 case reflect.Float32, reflect.Float64: 495 return Float64ToInt64(r.Float()) 496 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 497 return r.Int(), nil 498 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 499 return Uint64ToInt64(r.Uint()) 500 case reflect.String: 501 return StringToInt64(r.String(), emptyStringAsZero...) 502 } 503 if isEmptyAsZero(emptyStringAsZero) { 504 return BoolToInt64(!isZero(r)), nil 505 } 506 return 0, fmt.Errorf("cannot convert %#v of type %T to int64", i, i) 507 } 508 } 509 510 // InterfaceToInt64Ptr converts interface to *int64. 511 func InterfaceToInt64Ptr(i interface{}, emptyAsFalse ...bool) (*int64, error) { 512 r, err := InterfaceToInt64(i, emptyAsFalse...) 513 return &r, err 514 } 515 516 // InterfaceToUint converts interface to uint. 517 func InterfaceToUint(i interface{}, emptyStringAsZero ...bool) (uint, error) { 518 switch v := i.(type) { 519 case bool: 520 return BoolToUint(v), nil 521 case nil: 522 return 0, nil 523 case int: 524 return IntToUint(v) 525 case int8: 526 return Int8ToUint(v) 527 case int16: 528 return Int16ToUint(v) 529 case int32: 530 return Int32ToUint(v) 531 case int64: 532 return Int64ToUint(v) 533 case uint: 534 return v, nil 535 case uint8: 536 return Uint8ToUint(v), nil 537 case uint16: 538 return Uint16ToUint(v), nil 539 case uint32: 540 return Uint32ToUint(v), nil 541 case uint64: 542 return Uint64ToUint(v) 543 case uintptr: 544 return uint(v), nil 545 case string: 546 return StringToUint(v, emptyStringAsZero...) 547 default: 548 r := IndirectValue(reflect.ValueOf(i)) 549 switch r.Kind() { 550 case reflect.Bool: 551 return BoolToUint(r.Bool()), nil 552 case reflect.Invalid: 553 return 0, nil 554 case reflect.Float32, reflect.Float64: 555 return Float64ToUint(r.Float()) 556 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 557 return Int64ToUint(r.Int()) 558 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 559 return Uint64ToUint(r.Uint()) 560 case reflect.String: 561 return StringToUint(r.String(), emptyStringAsZero...) 562 } 563 if isEmptyAsZero(emptyStringAsZero) { 564 return BoolToUint(!isZero(r)), nil 565 } 566 return 0, fmt.Errorf("cannot convert %#v of type %T to uint", i, i) 567 } 568 } 569 570 // InterfaceToUintPtr converts interface to *uint. 571 func InterfaceToUintPtr(i interface{}, emptyAsFalse ...bool) (*uint, error) { 572 r, err := InterfaceToUint(i, emptyAsFalse...) 573 return &r, err 574 } 575 576 // InterfaceToUint8 converts interface to uint8. 577 func InterfaceToUint8(i interface{}, emptyStringAsZero ...bool) (uint8, error) { 578 switch v := i.(type) { 579 case bool: 580 return BoolToUint8(v), nil 581 case nil: 582 return 0, nil 583 case int: 584 return IntToUint8(v) 585 case int8: 586 return Int8ToUint8(v) 587 case int16: 588 return Int16ToUint8(v) 589 case int32: 590 return Int32ToUint8(v) 591 case int64: 592 return Int64ToUint8(v) 593 case uint: 594 return UintToUint8(v) 595 case uint8: 596 return v, nil 597 case uint16: 598 return Uint16ToUint8(v) 599 case uint32: 600 return Uint32ToUint8(v) 601 case uint64: 602 return Uint64ToUint8(v) 603 case uintptr: 604 return UintToUint8(uint(v)) 605 case string: 606 return StringToUint8(v, emptyStringAsZero...) 607 default: 608 r := IndirectValue(reflect.ValueOf(i)) 609 switch r.Kind() { 610 case reflect.Bool: 611 return BoolToUint8(r.Bool()), nil 612 case reflect.Invalid: 613 return 0, nil 614 case reflect.Float32, reflect.Float64: 615 return Float64ToUint8(r.Float()) 616 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 617 return Int64ToUint8(r.Int()) 618 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 619 return Uint64ToUint8(r.Uint()) 620 case reflect.String: 621 return StringToUint8(r.String(), emptyStringAsZero...) 622 } 623 if isEmptyAsZero(emptyStringAsZero) { 624 return BoolToUint8(!isZero(r)), nil 625 } 626 return 0, fmt.Errorf("cannot convert %#v of type %T to uint8", i, i) 627 } 628 } 629 630 // InterfaceToUint8Ptr converts interface to *uint8. 631 func InterfaceToUint8Ptr(i interface{}, emptyAsFalse ...bool) (*uint8, error) { 632 r, err := InterfaceToUint8(i, emptyAsFalse...) 633 return &r, err 634 } 635 636 // InterfaceToUint16 converts interface to uint16. 637 func InterfaceToUint16(i interface{}, emptyStringAsZero ...bool) (uint16, error) { 638 switch v := i.(type) { 639 case bool: 640 return BoolToUint16(v), nil 641 case nil: 642 return 0, nil 643 case int: 644 return IntToUint16(v) 645 case int8: 646 return Int8ToUint16(v) 647 case int16: 648 return Int16ToUint16(v) 649 case int32: 650 return Int32ToUint16(v) 651 case int64: 652 return Int64ToUint16(v) 653 case uint: 654 return UintToUint16(v) 655 case uint8: 656 return Uint8ToUint16(v), nil 657 case uint16: 658 return v, nil 659 case uint32: 660 return Uint32ToUint16(v) 661 case uint64: 662 return Uint64ToUint16(v) 663 case uintptr: 664 return UintToUint16(uint(v)) 665 case string: 666 return StringToUint16(v, emptyStringAsZero...) 667 default: 668 r := IndirectValue(reflect.ValueOf(i)) 669 switch r.Kind() { 670 case reflect.Bool: 671 return BoolToUint16(r.Bool()), nil 672 case reflect.Invalid: 673 return 0, nil 674 case reflect.Float32, reflect.Float64: 675 return Float64ToUint16(r.Float()) 676 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 677 return Int64ToUint16(r.Int()) 678 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 679 return Uint64ToUint16(r.Uint()) 680 case reflect.String: 681 return StringToUint16(r.String(), emptyStringAsZero...) 682 } 683 if isEmptyAsZero(emptyStringAsZero) { 684 return BoolToUint16(!isZero(r)), nil 685 } 686 return 0, fmt.Errorf("cannot convert %#v of type %T to uint16", i, i) 687 } 688 } 689 690 // InterfaceToUint16Ptr converts interface to *uint16. 691 func InterfaceToUint16Ptr(i interface{}, emptyAsFalse ...bool) (*uint16, error) { 692 r, err := InterfaceToUint16(i, emptyAsFalse...) 693 return &r, err 694 } 695 696 // InterfaceToUint32 converts interface to uint32. 697 func InterfaceToUint32(i interface{}, emptyStringAsZero ...bool) (uint32, error) { 698 switch v := i.(type) { 699 case bool: 700 return BoolToUint32(v), nil 701 case nil: 702 return 0, nil 703 case int: 704 return IntToUint32(v) 705 case int8: 706 return Int8ToUint32(v) 707 case int16: 708 return Int16ToUint32(v) 709 case int32: 710 return Int32ToUint32(v) 711 case int64: 712 return Int64ToUint32(v) 713 case uint: 714 return UintToUint32(v) 715 case uint8: 716 return Uint8ToUint32(v), nil 717 case uint16: 718 return Uint16ToUint32(v), nil 719 case uint32: 720 return v, nil 721 case uint64: 722 return Uint64ToUint32(v) 723 case uintptr: 724 return UintToUint32(uint(v)) 725 case string: 726 return StringToUint32(v, emptyStringAsZero...) 727 default: 728 r := IndirectValue(reflect.ValueOf(i)) 729 switch r.Kind() { 730 case reflect.Bool: 731 return BoolToUint32(r.Bool()), nil 732 case reflect.Invalid: 733 return 0, nil 734 case reflect.Float32, reflect.Float64: 735 return Float64ToUint32(r.Float()) 736 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 737 return Int64ToUint32(r.Int()) 738 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 739 return Uint64ToUint32(r.Uint()) 740 case reflect.String: 741 return StringToUint32(r.String(), emptyStringAsZero...) 742 } 743 if isEmptyAsZero(emptyStringAsZero) { 744 return BoolToUint32(!isZero(r)), nil 745 } 746 return 0, fmt.Errorf("cannot convert %#v of type %T to uint32", i, i) 747 } 748 } 749 750 // InterfaceToUint32Ptr converts interface to *uint32. 751 func InterfaceToUint32Ptr(i interface{}, emptyAsFalse ...bool) (*uint32, error) { 752 r, err := InterfaceToUint32(i, emptyAsFalse...) 753 return &r, err 754 } 755 756 // InterfaceToUint64 converts interface to uint64. 757 func InterfaceToUint64(i interface{}, emptyStringAsZero ...bool) (uint64, error) { 758 switch v := i.(type) { 759 case bool: 760 return BoolToUint64(v), nil 761 case nil: 762 return 0, nil 763 case int: 764 return IntToUint64(v) 765 case int8: 766 return Int8ToUint64(v) 767 case int16: 768 return Int16ToUint64(v) 769 case int32: 770 return Int32ToUint64(v) 771 case int64: 772 return Int64ToUint64(v) 773 case uint: 774 return UintToUint64(v), nil 775 case uint8: 776 return Uint8ToUint64(v), nil 777 case uint16: 778 return Uint16ToUint64(v), nil 779 case uint32: 780 return Uint32ToUint64(v), nil 781 case uint64: 782 return v, nil 783 case uintptr: 784 return UintToUint64(uint(v)), nil 785 case string: 786 return StringToUint64(v, emptyStringAsZero...) 787 default: 788 r := IndirectValue(reflect.ValueOf(i)) 789 switch r.Kind() { 790 case reflect.Bool: 791 return BoolToUint64(r.Bool()), nil 792 case reflect.Invalid: 793 return 0, nil 794 case reflect.Float32, reflect.Float64: 795 return Float64ToUint64(r.Float()) 796 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 797 return Int64ToUint64(r.Int()) 798 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 799 return r.Uint(), nil 800 case reflect.String: 801 return StringToUint64(r.String(), emptyStringAsZero...) 802 } 803 if isEmptyAsZero(emptyStringAsZero) { 804 return BoolToUint64(!isZero(r)), nil 805 } 806 return 0, fmt.Errorf("cannot convert %#v of type %T to uint64", i, i) 807 } 808 } 809 810 // InterfaceToUint64Ptr converts interface to *uint64. 811 func InterfaceToUint64Ptr(i interface{}, emptyAsFalse ...bool) (*uint64, error) { 812 r, err := InterfaceToUint64(i, emptyAsFalse...) 813 return &r, err 814 }