Support duration as nanosecond number when using a resolver

Duration is always marshaled as a nanosecond number when using JSON. Kong would only parse it as a string using ParseDuration. Now it can use the number version too.
This commit is contained in:
Yunchi Luo
2021-08-26 11:52:27 -04:00
committed by Alec Thomas
parent 5a9c9c7864
commit b5bcf3579b
2 changed files with 26 additions and 6 deletions
+14 -6
View File
@@ -308,15 +308,23 @@ func (boolMapper) IsBool() bool { return true }
func durationDecoder() MapperFunc {
return func(ctx *DecodeContext, target reflect.Value) error {
var value string
if err := ctx.Scan.PopValueInto("duration", &value); err != nil {
t, err := ctx.Scan.PopValue("duration")
if err != nil {
return err
}
r, err := time.ParseDuration(value)
if err != nil {
return errors.Errorf("expected duration but got %q: %s", value, err)
var d time.Duration
switch v := t.Value.(type) {
case string:
d, err = time.ParseDuration(v)
if err != nil {
return errors.Errorf("expected duration but got %q: %s", v, err)
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
d = reflect.ValueOf(v).Convert(reflect.TypeOf(time.Duration(0))).Interface().(time.Duration) // nolint: forcetypeassert
default:
return errors.Errorf("expected duration but got %q", v)
}
target.Set(reflect.ValueOf(r))
target.Set(reflect.ValueOf(d))
return nil
}
}
+12
View File
@@ -127,6 +127,18 @@ func TestDurationMapper(t *testing.T) {
require.Equal(t, time.Second*5, cli.Flag)
}
func TestDurationMapperJSONResolver(t *testing.T) {
var cli struct {
Flag time.Duration
}
resolver, err := kong.JSON(strings.NewReader(`{"flag": 5000000000}`))
require.NoError(t, err)
k := mustNew(t, &cli, kong.Resolvers(resolver))
_, err = k.Parse(nil)
require.NoError(t, err)
require.Equal(t, time.Second*5, cli.Flag)
}
func TestSplitEscaped(t *testing.T) {
require.Equal(t, []string{"a", "b"}, kong.SplitEscaped("a,b", ','))
require.Equal(t, []string{"a,b", "c"}, kong.SplitEscaped(`a\,b,c`, ','))