MCPcopy Create free account
hub / github.com/alecthomas/kong / floatDecoder

Function floatDecoder

mapper.go:432–460  ·  view source on GitHub ↗
(bits int)

Source from the content-addressed store, hash-verified

430}
431
432func floatDecoder(bits int) MapperFunc {
433 return func(ctx *DecodeContext, target reflect.Value) error {
434 t, err := ctx.Scan.PopValue("float")
435 if err != nil {
436 return err
437 }
438 switch v := t.Value.(type) {
439 case string:
440 n, err := strconv.ParseFloat(v, bits)
441 if err != nil {
442 return fmt.Errorf("expected a float but got %q (%T)", t, t.Value)
443 }
444 target.SetFloat(n)
445
446 case float32:
447 target.SetFloat(float64(v))
448
449 case float64:
450 target.SetFloat(v)
451
452 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
453 target.SetFloat(reflect.ValueOf(v).Convert(reflect.TypeOf(float64(0))).Float())
454
455 default:
456 return fmt.Errorf("expected a float but got %q (%T)", t, t.Value)
457 }
458 return nil
459 }
460}
461
462func mapDecoder(r *Registry) MapperFunc {
463 return func(ctx *DecodeContext, target reflect.Value) error {

Callers 1

RegisterDefaultsMethod · 0.85

Calls 2

PopValueMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected