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

Method Decode

mapper.go:298–325  ·  view source on GitHub ↗
(ctx *DecodeContext, target reflect.Value)

Source from the content-addressed store, hash-verified

296type boolMapper struct{}
297
298func (boolMapper) Decode(ctx *DecodeContext, target reflect.Value) error {
299 if ctx.Scan.Peek().Type == FlagValueToken {
300 token := ctx.Scan.Pop()
301 switch v := token.Value.(type) {
302 case string:
303 v = strings.ToLower(v)
304 switch v {
305 case "true", "1", "yes":
306 target.SetBool(true)
307
308 case "false", "0", "no":
309 target.SetBool(false)
310
311 default:
312 return fmt.Errorf("bool value must be true, 1, yes, false, 0 or no but got %q", v)
313 }
314
315 case bool:
316 target.SetBool(v)
317
318 default:
319 return fmt.Errorf("expected bool but got %q (%T)", token.Value, token.Value)
320 }
321 } else {
322 target.SetBool(true)
323 }
324 return nil
325}
326func (boolMapper) IsBool() bool { return true }
327
328func durationDecoder() MapperFunc {

Callers

nothing calls this directly

Calls 3

PeekMethod · 0.80
PopMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected