| 662 | } |
| 663 | |
| 664 | func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error { |
| 665 | dataVal := reflect.Indirect(reflect.ValueOf(data)) |
| 666 | dataKind := getKind(dataVal) |
| 667 | dataType := dataVal.Type() |
| 668 | |
| 669 | switch { |
| 670 | case dataKind == reflect.Int: |
| 671 | i := dataVal.Int() |
| 672 | if i < 0 && !d.config.WeaklyTypedInput { |
| 673 | return fmt.Errorf("cannot parse '%s', %d overflows uint", |
| 674 | name, i) |
| 675 | } |
| 676 | val.SetUint(uint64(i)) |
| 677 | case dataKind == reflect.Uint: |
| 678 | val.SetUint(dataVal.Uint()) |
| 679 | case dataKind == reflect.Float32: |
| 680 | f := dataVal.Float() |
| 681 | if f < 0 && !d.config.WeaklyTypedInput { |
| 682 | return fmt.Errorf("cannot parse '%s', %f overflows uint", |
| 683 | name, f) |
| 684 | } |
| 685 | val.SetUint(uint64(f)) |
| 686 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: |
| 687 | if dataVal.Bool() { |
| 688 | val.SetUint(1) |
| 689 | } else { |
| 690 | val.SetUint(0) |
| 691 | } |
| 692 | case dataKind == reflect.String && d.config.WeaklyTypedInput: |
| 693 | str := dataVal.String() |
| 694 | if str == "" { |
| 695 | str = "0" |
| 696 | } |
| 697 | |
| 698 | i, err := strconv.ParseUint(str, 0, val.Type().Bits()) |
| 699 | if err == nil { |
| 700 | val.SetUint(i) |
| 701 | } else { |
| 702 | return fmt.Errorf("cannot parse '%s' as uint: %s", name, err) |
| 703 | } |
| 704 | case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number": |
| 705 | jn := data.(json.Number) |
| 706 | i, err := strconv.ParseUint(string(jn), 0, 64) |
| 707 | if err != nil { |
| 708 | return fmt.Errorf( |
| 709 | "error decoding json.Number into %s: %s", name, err) |
| 710 | } |
| 711 | val.SetUint(i) |
| 712 | default: |
| 713 | return fmt.Errorf( |
| 714 | "'%s' expected type '%s', got unconvertible type '%s', value: '%v'", |
| 715 | name, val.Type(), dataVal.Type(), data) |
| 716 | } |
| 717 | |
| 718 | return nil |
| 719 | } |
| 720 | |
| 721 | func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error { |