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:
@@ -308,15 +308,23 @@ func (boolMapper) IsBool() bool { return true }
|
|||||||
|
|
||||||
func durationDecoder() MapperFunc {
|
func durationDecoder() MapperFunc {
|
||||||
return func(ctx *DecodeContext, target reflect.Value) error {
|
return func(ctx *DecodeContext, target reflect.Value) error {
|
||||||
var value string
|
t, err := ctx.Scan.PopValue("duration")
|
||||||
if err := ctx.Scan.PopValueInto("duration", &value); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r, err := time.ParseDuration(value)
|
var d time.Duration
|
||||||
if err != nil {
|
switch v := t.Value.(type) {
|
||||||
return errors.Errorf("expected duration but got %q: %s", value, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,6 +127,18 @@ func TestDurationMapper(t *testing.T) {
|
|||||||
require.Equal(t, time.Second*5, cli.Flag)
|
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) {
|
func TestSplitEscaped(t *testing.T) {
|
||||||
require.Equal(t, []string{"a", "b"}, kong.SplitEscaped("a,b", ','))
|
require.Equal(t, []string{"a", "b"}, kong.SplitEscaped("a,b", ','))
|
||||||
require.Equal(t, []string{"a,b", "c"}, kong.SplitEscaped(`a\,b,c`, ','))
|
require.Equal(t, []string{"a,b", "c"}, kong.SplitEscaped(`a\,b,c`, ','))
|
||||||
|
|||||||
Reference in New Issue
Block a user