MCPcopy
hub / github.com/alecthomas/kong / intDecoder

Function intDecoder

mapper.go:370–397  ·  view source on GitHub ↗
(bits int)

Source from the content-addressed store, hash-verified

368}
369
370func intDecoder(bits int) MapperFunc { //nolint: dupl
371 return func(ctx *DecodeContext, target reflect.Value) error {
372 t, err := ctx.Scan.PopValue("int")
373 if err != nil {
374 return err
375 }
376 var sv string
377 switch v := t.Value.(type) {
378 case string:
379 sv = v
380
381 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
382 sv = fmt.Sprintf("%v", v)
383
384 case float32, float64:
385 sv = fmt.Sprintf("%0.f", v)
386
387 default:
388 return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
389 }
390 n, err := strconv.ParseInt(sv, 0, bits)
391 if err != nil {
392 return fmt.Errorf("expected a valid %d bit int but got %q", bits, sv)
393 }
394 target.SetInt(n)
395 return nil
396 }
397}
398
399func uintDecoder(bits int) MapperFunc { //nolint: dupl
400 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

Used in the wild real call sites across dependent graphs

searching dependent graphs…