MCPcopy
hub / github.com/mitchellh/mapstructure / decodeFloat

Method decodeFloat

mapstructure.go:752–797  ·  view source on GitHub ↗
(name string, data interface{}, val reflect.Value)

Source from the content-addressed store, hash-verified

750}
751
752func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
753 dataVal := reflect.Indirect(reflect.ValueOf(data))
754 dataKind := getKind(dataVal)
755 dataType := dataVal.Type()
756
757 switch {
758 case dataKind == reflect.Int:
759 val.SetFloat(float64(dataVal.Int()))
760 case dataKind == reflect.Uint:
761 val.SetFloat(float64(dataVal.Uint()))
762 case dataKind == reflect.Float32:
763 val.SetFloat(dataVal.Float())
764 case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
765 if dataVal.Bool() {
766 val.SetFloat(1)
767 } else {
768 val.SetFloat(0)
769 }
770 case dataKind == reflect.String && d.config.WeaklyTypedInput:
771 str := dataVal.String()
772 if str == "" {
773 str = "0"
774 }
775
776 f, err := strconv.ParseFloat(str, val.Type().Bits())
777 if err == nil {
778 val.SetFloat(f)
779 } else {
780 return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
781 }
782 case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
783 jn := data.(json.Number)
784 i, err := jn.Float64()
785 if err != nil {
786 return fmt.Errorf(
787 "error decoding json.Number into %s: %s", name, err)
788 }
789 val.SetFloat(i)
790 default:
791 return fmt.Errorf(
792 "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
793 name, val.Type(), dataVal.Type(), data)
794 }
795
796 return nil
797}
798
799func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
800 valType := val.Type()

Callers 1

decodeMethod · 0.95

Calls 1

getKindFunction · 0.85

Tested by

no test coverage detected