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

Method decodeBool

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

Source from the content-addressed store, hash-verified

719}
720
721func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
722 dataVal := reflect.Indirect(reflect.ValueOf(data))
723 dataKind := getKind(dataVal)
724
725 switch {
726 case dataKind == reflect.Bool:
727 val.SetBool(dataVal.Bool())
728 case dataKind == reflect.Int && d.config.WeaklyTypedInput:
729 val.SetBool(dataVal.Int() != 0)
730 case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
731 val.SetBool(dataVal.Uint() != 0)
732 case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
733 val.SetBool(dataVal.Float() != 0)
734 case dataKind == reflect.String && d.config.WeaklyTypedInput:
735 b, err := strconv.ParseBool(dataVal.String())
736 if err == nil {
737 val.SetBool(b)
738 } else if dataVal.String() == "" {
739 val.SetBool(false)
740 } else {
741 return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
742 }
743 default:
744 return fmt.Errorf(
745 "'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
746 name, val.Type(), dataVal.Type(), data)
747 }
748
749 return nil
750}
751
752func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
753 dataVal := reflect.Indirect(reflect.ValueOf(data))

Callers 1

decodeMethod · 0.95

Calls 1

getKindFunction · 0.85

Tested by

no test coverage detected