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

Function durationDecoder

mapper.go:328–349  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

326func (boolMapper) IsBool() bool { return true }
327
328func durationDecoder() MapperFunc {
329 return func(ctx *DecodeContext, target reflect.Value) error {
330 t, err := ctx.Scan.PopValue("duration")
331 if err != nil {
332 return err
333 }
334 var d time.Duration
335 switch v := t.Value.(type) {
336 case string:
337 d, err = time.ParseDuration(v)
338 if err != nil {
339 return fmt.Errorf("expected duration but got %q: %v", v, err)
340 }
341 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
342 d = reflect.ValueOf(v).Convert(reflect.TypeOf(time.Duration(0))).Interface().(time.Duration) //nolint: forcetypeassert
343 default:
344 return fmt.Errorf("expected duration but got %q", v)
345 }
346 target.Set(reflect.ValueOf(d))
347 return nil
348 }
349}
350
351func timeDecoder() MapperFunc {
352 return func(ctx *DecodeContext, target reflect.Value) error {

Callers 1

RegisterDefaultsMethod · 0.85

Calls 3

PopValueMethod · 0.80
ErrorfMethod · 0.80
SetMethod · 0.80

Tested by

no test coverage detected