github.com/influxdata/telegraf@v1.30.3/internal/type_conversions.go (about) 1 package internal 2 3 import ( 4 "fmt" 5 "math" 6 "strconv" 7 "strings" 8 ) 9 10 var ErrOutOfRange = strconv.ErrRange 11 12 func ToFloat64(value interface{}) (float64, error) { 13 switch v := value.(type) { 14 case string: 15 return strconv.ParseFloat(v, 64) 16 case []byte: 17 return strconv.ParseFloat(string(v), 64) 18 case fmt.Stringer: 19 return strconv.ParseFloat(v.String(), 64) 20 case bool: 21 if v { 22 return 1, nil 23 } 24 return 0, nil 25 case int: 26 return float64(v), nil 27 case int8: 28 return float64(v), nil 29 case int16: 30 return float64(v), nil 31 case int32: 32 return float64(v), nil 33 case int64: 34 return float64(v), nil 35 case uint: 36 return float64(v), nil 37 case uint8: 38 return float64(v), nil 39 case uint16: 40 return float64(v), nil 41 case uint32: 42 return float64(v), nil 43 case uint64: 44 return float64(v), nil 45 case float32: 46 return float64(v), nil 47 case float64: 48 return v, nil 49 case nil: 50 return 0, nil 51 } 52 return 0, fmt.Errorf("type \"%T\" unsupported", value) 53 } 54 55 func ToFloat32(value interface{}) (float32, error) { 56 switch v := value.(type) { 57 case string: 58 x, err := strconv.ParseFloat(v, 32) 59 return float32(x), err 60 case []byte: 61 x, err := strconv.ParseFloat(string(v), 32) 62 return float32(x), err 63 case fmt.Stringer: 64 x, err := strconv.ParseFloat(v.String(), 32) 65 return float32(x), err 66 case bool: 67 if v { 68 return 1, nil 69 } 70 return 0, nil 71 case int: 72 return float32(v), nil 73 case int8: 74 return float32(v), nil 75 case int16: 76 return float32(v), nil 77 case int32: 78 return float32(v), nil 79 case int64: 80 return float32(v), nil 81 case uint: 82 return float32(v), nil 83 case uint8: 84 return float32(v), nil 85 case uint16: 86 return float32(v), nil 87 case uint32: 88 return float32(v), nil 89 case uint64: 90 return float32(v), nil 91 case float32: 92 return v, nil 93 case float64: 94 if v < -math.MaxFloat32 || v > math.MaxFloat32 { 95 return float32(v), ErrOutOfRange 96 } 97 return float32(v), nil 98 case nil: 99 return 0, nil 100 } 101 return 0, fmt.Errorf("type \"%T\" unsupported", value) 102 } 103 104 func ToUint64(value interface{}) (uint64, error) { 105 switch v := value.(type) { 106 case string: 107 if strings.HasPrefix(v, "0x") { 108 return strconv.ParseUint(strings.TrimPrefix(v, "0x"), 16, 64) 109 } 110 return strconv.ParseUint(v, 10, 64) 111 case []byte: 112 return strconv.ParseUint(string(v), 10, 64) 113 case fmt.Stringer: 114 return strconv.ParseUint(v.String(), 10, 64) 115 case bool: 116 if v { 117 return 1, nil 118 } 119 return 0, nil 120 case int: 121 if v < 0 { 122 return uint64(v), ErrOutOfRange 123 } 124 return uint64(v), nil 125 case int8: 126 if v < 0 { 127 return uint64(v), ErrOutOfRange 128 } 129 return uint64(v), nil 130 case int16: 131 if v < 0 { 132 return uint64(v), ErrOutOfRange 133 } 134 return uint64(v), nil 135 case int32: 136 if v < 0 { 137 return uint64(v), ErrOutOfRange 138 } 139 return uint64(v), nil 140 case int64: 141 if v < 0 { 142 return uint64(v), ErrOutOfRange 143 } 144 return uint64(v), nil 145 case uint: 146 return uint64(v), nil 147 case uint8: 148 return uint64(v), nil 149 case uint16: 150 return uint64(v), nil 151 case uint32: 152 return uint64(v), nil 153 case uint64: 154 return v, nil 155 case float32: 156 if v < 0 || v > math.MaxUint64 { 157 return uint64(v), ErrOutOfRange 158 } 159 return uint64(v), nil 160 case float64: 161 if v < 0 || v > math.MaxUint64 { 162 return uint64(v), ErrOutOfRange 163 } 164 return uint64(v), nil 165 case nil: 166 return 0, nil 167 } 168 return 0, fmt.Errorf("type \"%T\" unsupported", value) 169 } 170 171 func ToUint32(value interface{}) (uint32, error) { 172 switch v := value.(type) { 173 case string: 174 if strings.HasPrefix(v, "0x") { 175 x, err := strconv.ParseUint(strings.TrimPrefix(v, "0x"), 16, 32) 176 return uint32(x), err 177 } 178 x, err := strconv.ParseUint(v, 10, 32) 179 return uint32(x), err 180 case []byte: 181 x, err := strconv.ParseUint(string(v), 10, 32) 182 return uint32(x), err 183 case fmt.Stringer: 184 x, err := strconv.ParseUint(v.String(), 10, 32) 185 return uint32(x), err 186 case bool: 187 if v { 188 return 1, nil 189 } 190 return 0, nil 191 case int: 192 if v < 0 || uint64(v) > math.MaxUint32 { 193 return uint32(v), ErrOutOfRange 194 } 195 return uint32(v), nil 196 case int8: 197 if v < 0 { 198 return uint32(v), ErrOutOfRange 199 } 200 return uint32(v), nil 201 case int16: 202 if v < 0 { 203 return uint32(v), ErrOutOfRange 204 } 205 return uint32(v), nil 206 case int32: 207 if v < 0 { 208 return uint32(v), ErrOutOfRange 209 } 210 return uint32(v), nil 211 case int64: 212 if v < 0 || v > math.MaxUint32 { 213 return uint32(v), ErrOutOfRange 214 } 215 return uint32(v), nil 216 case uint: 217 return uint32(v), nil 218 case uint8: 219 return uint32(v), nil 220 case uint16: 221 return uint32(v), nil 222 case uint32: 223 return v, nil 224 case uint64: 225 if v > math.MaxUint32 { 226 return uint32(v), ErrOutOfRange 227 } 228 return uint32(v), nil 229 case float32: 230 if v < 0 || v > math.MaxUint32 { 231 return uint32(v), ErrOutOfRange 232 } 233 return uint32(v), nil 234 case float64: 235 if v < 0 || v > math.MaxUint32 { 236 return uint32(v), ErrOutOfRange 237 } 238 return uint32(v), nil 239 case nil: 240 return 0, nil 241 } 242 return 0, fmt.Errorf("type \"%T\" unsupported", value) 243 } 244 245 func ToUint16(value interface{}) (uint16, error) { 246 switch v := value.(type) { 247 case string: 248 if strings.HasPrefix(v, "0x") { 249 x, err := strconv.ParseUint(strings.TrimPrefix(v, "0x"), 16, 16) 250 return uint16(x), err 251 } 252 x, err := strconv.ParseUint(v, 10, 32) 253 return uint16(x), err 254 case []byte: 255 x, err := strconv.ParseUint(string(v), 10, 32) 256 return uint16(x), err 257 case fmt.Stringer: 258 x, err := strconv.ParseUint(v.String(), 10, 32) 259 return uint16(x), err 260 case bool: 261 if v { 262 return 1, nil 263 } 264 return 0, nil 265 case int: 266 if v < 0 || v > math.MaxUint16 { 267 return uint16(v), ErrOutOfRange 268 } 269 return uint16(v), nil 270 case int8: 271 if v < 0 { 272 return uint16(v), ErrOutOfRange 273 } 274 return uint16(v), nil 275 case int16: 276 if v < 0 { 277 return uint16(v), ErrOutOfRange 278 } 279 return uint16(v), nil 280 case int32: 281 if v < 0 || v > math.MaxUint16 { 282 return uint16(v), ErrOutOfRange 283 } 284 return uint16(v), nil 285 case int64: 286 if v < 0 || v > math.MaxUint16 { 287 return uint16(v), ErrOutOfRange 288 } 289 return uint16(v), nil 290 case uint: 291 return uint16(v), nil 292 case uint8: 293 return uint16(v), nil 294 case uint16: 295 return v, nil 296 case uint32: 297 if v > math.MaxUint16 { 298 return uint16(v), ErrOutOfRange 299 } 300 return uint16(v), nil 301 case uint64: 302 if v > math.MaxUint16 { 303 return uint16(v), ErrOutOfRange 304 } 305 return uint16(v), nil 306 case float32: 307 if v < 0 || v > math.MaxUint16 { 308 return uint16(v), ErrOutOfRange 309 } 310 return uint16(v), nil 311 case float64: 312 if v < 0 || v > math.MaxUint16 { 313 return uint16(v), ErrOutOfRange 314 } 315 return uint16(v), nil 316 case nil: 317 return 0, nil 318 } 319 return 0, fmt.Errorf("type \"%T\" unsupported", value) 320 } 321 322 func ToUint8(value interface{}) (uint8, error) { 323 switch v := value.(type) { 324 case string: 325 if strings.HasPrefix(v, "0x") { 326 x, err := strconv.ParseUint(strings.TrimPrefix(v, "0x"), 16, 8) 327 return uint8(x), err 328 } 329 x, err := strconv.ParseUint(v, 10, 32) 330 return uint8(x), err 331 case []byte: 332 x, err := strconv.ParseUint(string(v), 10, 32) 333 return uint8(x), err 334 case fmt.Stringer: 335 x, err := strconv.ParseUint(v.String(), 10, 32) 336 return uint8(x), err 337 case bool: 338 if v { 339 return 1, nil 340 } 341 return 0, nil 342 case int: 343 if v < 0 || v > math.MaxUint8 { 344 return uint8(v), ErrOutOfRange 345 } 346 return uint8(v), nil 347 case int8: 348 if v < 0 { 349 return uint8(v), ErrOutOfRange 350 } 351 return uint8(v), nil 352 case int16: 353 if v < 0 || v > math.MaxUint8 { 354 return uint8(v), ErrOutOfRange 355 } 356 return uint8(v), nil 357 case int32: 358 if v < 0 || v > math.MaxUint8 { 359 return uint8(v), ErrOutOfRange 360 } 361 return uint8(v), nil 362 case int64: 363 if v < 0 || v > math.MaxUint8 { 364 return uint8(v), ErrOutOfRange 365 } 366 return uint8(v), nil 367 case uint: 368 return uint8(v), nil 369 case uint8: 370 return v, nil 371 case uint16: 372 if v > math.MaxUint8 { 373 return uint8(v), ErrOutOfRange 374 } 375 return uint8(v), nil 376 case uint32: 377 if v > math.MaxUint8 { 378 return uint8(v), ErrOutOfRange 379 } 380 return uint8(v), nil 381 case uint64: 382 if v > math.MaxUint8 { 383 return uint8(v), ErrOutOfRange 384 } 385 return uint8(v), nil 386 case float32: 387 if v < 0 || v > math.MaxUint8 { 388 return uint8(v), ErrOutOfRange 389 } 390 return uint8(v), nil 391 case float64: 392 if v < 0 || v > math.MaxUint8 { 393 return uint8(v), ErrOutOfRange 394 } 395 return uint8(v), nil 396 case nil: 397 return 0, nil 398 } 399 return 0, fmt.Errorf("type \"%T\" unsupported", value) 400 } 401 402 func ToInt64(value interface{}) (int64, error) { 403 switch v := value.(type) { 404 case string: 405 if strings.HasPrefix(v, "0x") { 406 return strconv.ParseInt(strings.TrimPrefix(v, "0x"), 16, 64) 407 } 408 return strconv.ParseInt(v, 10, 64) 409 case []byte: 410 return strconv.ParseInt(string(v), 10, 64) 411 case fmt.Stringer: 412 return strconv.ParseInt(v.String(), 10, 64) 413 case bool: 414 if v { 415 return 1, nil 416 } 417 return 0, nil 418 case int: 419 return int64(v), nil 420 case int8: 421 return int64(v), nil 422 case int16: 423 return int64(v), nil 424 case int32: 425 return int64(v), nil 426 case int64: 427 return v, nil 428 case uint: 429 if uint64(v) > math.MaxInt64 { 430 return int64(v), ErrOutOfRange 431 } 432 return int64(v), nil 433 case uint8: 434 return int64(v), nil 435 case uint16: 436 return int64(v), nil 437 case uint32: 438 return int64(v), nil 439 case uint64: 440 if v > math.MaxInt64 { 441 return int64(v), ErrOutOfRange 442 } 443 return int64(v), nil 444 case float32: 445 if v < math.MinInt64 || v > math.MaxInt64 { 446 return int64(v), ErrOutOfRange 447 } 448 return int64(v), nil 449 case float64: 450 if v < math.MinInt64 || v > math.MaxInt64 { 451 return int64(v), ErrOutOfRange 452 } 453 return int64(v), nil 454 case nil: 455 return 0, nil 456 } 457 return 0, fmt.Errorf("type \"%T\" unsupported", value) 458 } 459 460 func ToInt32(value interface{}) (int32, error) { 461 switch v := value.(type) { 462 case string: 463 if strings.HasPrefix(v, "0x") { 464 x, err := strconv.ParseInt(strings.TrimPrefix(v, "0x"), 16, 32) 465 return int32(x), err 466 } 467 x, err := strconv.ParseInt(v, 10, 32) 468 return int32(x), err 469 case []byte: 470 x, err := strconv.ParseInt(string(v), 10, 32) 471 return int32(x), err 472 case fmt.Stringer: 473 x, err := strconv.ParseInt(v.String(), 10, 32) 474 return int32(x), err 475 case bool: 476 if v { 477 return 1, nil 478 } 479 return 0, nil 480 case int: 481 if int64(v) < math.MinInt32 || int64(v) > math.MaxInt32 { 482 return int32(v), ErrOutOfRange 483 } 484 return int32(v), nil 485 case int8: 486 return int32(v), nil 487 case int16: 488 return int32(v), nil 489 case int32: 490 return v, nil 491 case int64: 492 if v < math.MinInt32 || v > math.MaxInt32 { 493 return int32(v), ErrOutOfRange 494 } 495 return int32(v), nil 496 case uint: 497 if v > math.MaxInt32 { 498 return int32(v), ErrOutOfRange 499 } 500 return int32(v), nil 501 case uint8: 502 return int32(v), nil 503 case uint16: 504 return int32(v), nil 505 case uint32: 506 if v > math.MaxInt32 { 507 return int32(v), ErrOutOfRange 508 } 509 return int32(v), nil 510 case uint64: 511 if v > math.MaxInt32 { 512 return int32(v), ErrOutOfRange 513 } 514 return int32(v), nil 515 case float32: 516 if v < math.MinInt32 || v > math.MaxInt32 { 517 return int32(v), ErrOutOfRange 518 } 519 return int32(v), nil 520 case float64: 521 if v < math.MinInt32 || v > math.MaxInt32 { 522 return int32(v), ErrOutOfRange 523 } 524 return int32(v), nil 525 case nil: 526 return 0, nil 527 } 528 return 0, fmt.Errorf("type \"%T\" unsupported", value) 529 } 530 531 func ToInt16(value interface{}) (int16, error) { 532 switch v := value.(type) { 533 case string: 534 if strings.HasPrefix(v, "0x") { 535 x, err := strconv.ParseInt(strings.TrimPrefix(v, "0x"), 16, 16) 536 return int16(x), err 537 } 538 x, err := strconv.ParseInt(v, 10, 32) 539 return int16(x), err 540 case []byte: 541 x, err := strconv.ParseInt(string(v), 10, 32) 542 return int16(x), err 543 case fmt.Stringer: 544 x, err := strconv.ParseInt(v.String(), 10, 32) 545 return int16(x), err 546 case bool: 547 if v { 548 return 1, nil 549 } 550 return 0, nil 551 case int: 552 return int16(v), nil 553 case int8: 554 return int16(v), nil 555 case int16: 556 return v, nil 557 case int32: 558 if v < math.MinInt16 || v > math.MaxInt16 { 559 return int16(v), ErrOutOfRange 560 } 561 return int16(v), nil 562 case int64: 563 if v < math.MinInt16 || v > math.MaxInt16 { 564 return int16(v), ErrOutOfRange 565 } 566 return int16(v), nil 567 case uint: 568 if v > math.MaxInt16 { 569 return int16(v), ErrOutOfRange 570 } 571 return int16(v), nil 572 case uint8: 573 return int16(v), nil 574 case uint16: 575 if v > math.MaxInt16 { 576 return int16(v), ErrOutOfRange 577 } 578 return int16(v), nil 579 case uint32: 580 if v > math.MaxInt16 { 581 return int16(v), ErrOutOfRange 582 } 583 return int16(v), nil 584 case uint64: 585 if v > math.MaxInt16 { 586 return int16(v), ErrOutOfRange 587 } 588 return int16(v), nil 589 case float32: 590 if v < math.MinInt16 || v > math.MaxInt16 { 591 return int16(v), ErrOutOfRange 592 } 593 return int16(v), nil 594 case float64: 595 if v < math.MinInt16 || v > math.MaxInt16 { 596 return int16(v), ErrOutOfRange 597 } 598 return int16(v), nil 599 case nil: 600 return 0, nil 601 } 602 return 0, fmt.Errorf("type \"%T\" unsupported", value) 603 } 604 605 func ToInt8(value interface{}) (int8, error) { 606 switch v := value.(type) { 607 case string: 608 if strings.HasPrefix(v, "0x") { 609 x, err := strconv.ParseInt(strings.TrimPrefix(v, "0x"), 16, 8) 610 return int8(x), err 611 } 612 x, err := strconv.ParseInt(v, 10, 32) 613 return int8(x), err 614 case []byte: 615 x, err := strconv.ParseInt(string(v), 10, 32) 616 return int8(x), err 617 case fmt.Stringer: 618 x, err := strconv.ParseInt(v.String(), 10, 32) 619 return int8(x), err 620 case bool: 621 if v { 622 return 1, nil 623 } 624 return 0, nil 625 case int: 626 return int8(v), nil 627 case int8: 628 return v, nil 629 case int16: 630 if v < math.MinInt8 || v > math.MaxInt8 { 631 return int8(v), ErrOutOfRange 632 } 633 return int8(v), nil 634 case int32: 635 if v < math.MinInt8 || v > math.MaxInt8 { 636 return int8(v), ErrOutOfRange 637 } 638 return int8(v), nil 639 case int64: 640 if v < math.MinInt8 || v > math.MaxInt8 { 641 return int8(v), ErrOutOfRange 642 } 643 return int8(v), nil 644 case uint: 645 if v > math.MaxInt8 { 646 return int8(v), ErrOutOfRange 647 } 648 return int8(v), nil 649 case uint8: 650 if v > math.MaxInt8 { 651 return int8(v), ErrOutOfRange 652 } 653 return int8(v), nil 654 case uint16: 655 if v > math.MaxInt8 { 656 return int8(v), ErrOutOfRange 657 } 658 return int8(v), nil 659 case uint32: 660 if v > math.MaxInt8 { 661 return int8(v), ErrOutOfRange 662 } 663 return int8(v), nil 664 case uint64: 665 if v > math.MaxInt8 { 666 return int8(v), ErrOutOfRange 667 } 668 return int8(v), nil 669 case float32: 670 if v < math.MinInt8 || v > math.MaxInt8 { 671 return int8(v), ErrOutOfRange 672 } 673 return int8(v), nil 674 case float64: 675 if v < math.MinInt8 || v > math.MaxInt8 { 676 return int8(v), ErrOutOfRange 677 } 678 return int8(v), nil 679 case nil: 680 return 0, nil 681 } 682 return 0, fmt.Errorf("type \"%T\" unsupported", value) 683 } 684 685 func ToString(value interface{}) (string, error) { 686 switch v := value.(type) { 687 case string: 688 return v, nil 689 case []byte: 690 return string(v), nil 691 case int: 692 return strconv.FormatInt(int64(v), 10), nil 693 case int8: 694 return strconv.FormatInt(int64(v), 10), nil 695 case int16: 696 return strconv.FormatInt(int64(v), 10), nil 697 case int32: 698 return strconv.FormatInt(int64(v), 10), nil 699 case int64: 700 return strconv.FormatInt(v, 10), nil 701 case uint: 702 return strconv.FormatUint(uint64(v), 10), nil 703 case uint8: 704 return strconv.FormatUint(uint64(v), 10), nil 705 case uint16: 706 return strconv.FormatUint(uint64(v), 10), nil 707 case uint32: 708 return strconv.FormatUint(uint64(v), 10), nil 709 case uint64: 710 return strconv.FormatUint(v, 10), nil 711 case float32: 712 return strconv.FormatFloat(float64(v), 'f', -1, 32), nil 713 case float64: 714 return strconv.FormatFloat(v, 'f', -1, 64), nil 715 case bool: 716 return strconv.FormatBool(v), nil 717 case fmt.Stringer: 718 return v.String(), nil 719 case nil: 720 return "", nil 721 } 722 return "", fmt.Errorf("type \"%T\" unsupported", value) 723 } 724 725 func ToBool(value interface{}) (bool, error) { 726 switch v := value.(type) { 727 case string: 728 return strconv.ParseBool(v) 729 case []byte: 730 return strconv.ParseBool(string(v)) 731 case fmt.Stringer: 732 return strconv.ParseBool(v.String()) 733 case int: 734 return v > 0, nil 735 case int8: 736 return v > 0, nil 737 case int16: 738 return v > 0, nil 739 case int32: 740 return v > 0, nil 741 case int64: 742 return v > 0, nil 743 case uint: 744 return v > 0, nil 745 case uint8: 746 return v > 0, nil 747 case uint16: 748 return v > 0, nil 749 case uint32: 750 return v > 0, nil 751 case uint64: 752 return v > 0, nil 753 case float32: 754 return v > 0, nil 755 case float64: 756 return v > 0, nil 757 case bool: 758 return v, nil 759 case nil: 760 return false, nil 761 } 762 return false, fmt.Errorf("type \"%T\" unsupported", value) 763 }