| 615 | } |
| 616 | |
| 617 | func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error { |
| 618 | dataVal := reflect.Indirect(reflect.ValueOf(data)) |
| 619 | dataKind := getKind(dataVal) |
| 620 | dataType := dataVal.Type() |
| 621 | |
| 622 | switch { |
| 623 | case dataKind == reflect.Int: |
| 624 | val.SetInt(dataVal.Int()) |
| 625 | case dataKind == reflect.Uint: |
| 626 | val.SetInt(int64(dataVal.Uint())) |
| 627 | case dataKind == reflect.Float32: |
| 628 | val.SetInt(int64(dataVal.Float())) |
| 629 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: |
| 630 | if dataVal.Bool() { |
| 631 | val.SetInt(1) |
| 632 | } else { |
| 633 | val.SetInt(0) |
| 634 | } |
| 635 | case dataKind == reflect.String && d.config.WeaklyTypedInput: |
| 636 | str := dataVal.String() |
| 637 | if str == "" { |
| 638 | str = "0" |
| 639 | } |
| 640 | |
| 641 | i, err := strconv.ParseInt(str, 0, val.Type().Bits()) |
| 642 | if err == nil { |
| 643 | val.SetInt(i) |
| 644 | } else { |
| 645 | return fmt.Errorf("cannot parse '%s' as int: %s", name, err) |
| 646 | } |
| 647 | case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number": |
| 648 | jn := data.(json.Number) |
| 649 | i, err := jn.Int64() |
| 650 | if err != nil { |
| 651 | return fmt.Errorf( |
| 652 | "error decoding json.Number into %s: %s", name, err) |
| 653 | } |
| 654 | val.SetInt(i) |
| 655 | default: |
| 656 | return fmt.Errorf( |
| 657 | "'%s' expected type '%s', got unconvertible type '%s', value: '%v'", |
| 658 | name, val.Type(), dataVal.Type(), data) |
| 659 | } |
| 660 | |
| 661 | return nil |
| 662 | } |
| 663 | |
| 664 | func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error { |
| 665 | dataVal := reflect.Indirect(reflect.ValueOf(data)) |