Unify time.Duration handling across framework and parseutil (#6935)

This removes a lot of duplicated code and adds time.Duration support to
parseutil, needed by the jwt auth method.
This commit is contained in:
Jeff Mitchell 2019-06-20 14:28:32 -04:00 committed by GitHub
parent 55e9f46ca3
commit 81ef0bb190
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 63 deletions

View file

@ -2,7 +2,6 @@ package framework
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -555,36 +554,11 @@ func (s *FieldSchema) DefaultOrZero() interface{} {
if s.Default != nil { if s.Default != nil {
switch s.Type { switch s.Type {
case TypeDurationSecond: case TypeDurationSecond:
var result int resultDur, err := parseutil.ParseDurationSecond(s.Default)
switch inp := s.Default.(type) { if err != nil {
case nil:
return s.Type.Zero()
case int:
result = inp
case int64:
result = int(inp)
case float32:
result = int(inp)
case float64:
result = int(inp)
case string:
dur, err := parseutil.ParseDurationSecond(inp)
if err != nil {
return s.Type.Zero()
}
result = int(dur.Seconds())
case json.Number:
valInt64, err := inp.Int64()
if err != nil {
return s.Type.Zero()
}
result = int(valInt64)
case time.Duration:
result = int(inp.Seconds())
default:
return s.Type.Zero() return s.Type.Zero()
} }
return result return int(resultDur.Seconds())
default: default:
return s.Default return s.Default

View file

@ -202,36 +202,12 @@ func (d *FieldData) getPrimitive(k string, schema *FieldSchema) (interface{}, bo
switch inp := raw.(type) { switch inp := raw.(type) {
case nil: case nil:
return nil, false, nil return nil, false, nil
case int: default:
result = inp
case int32:
result = int(inp)
case int64:
result = int(inp)
case uint:
result = int(inp)
case uint32:
result = int(inp)
case uint64:
result = int(inp)
case float32:
result = int(inp)
case float64:
result = int(inp)
case string:
dur, err := parseutil.ParseDurationSecond(inp) dur, err := parseutil.ParseDurationSecond(inp)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
result = int(dur.Seconds()) result = int(dur.Seconds())
case json.Number:
valInt64, err := inp.Int64()
if err != nil {
return nil, false, err
}
result = int(valInt64)
default:
return nil, false, fmt.Errorf("invalid input '%v'", raw)
} }
if result < 0 { if result < 0 {
return nil, false, fmt.Errorf("cannot provide negative value '%d'", result) return nil, false, fmt.Errorf("cannot provide negative value '%d'", result)

View file

@ -20,11 +20,12 @@ func ParseDurationSecond(in interface{}) (time.Duration, error) {
if ok { if ok {
in = jsonIn.String() in = jsonIn.String()
} }
switch in.(type) { switch inp := in.(type) {
case nil:
// return default of zero
case string: case string:
inp := in.(string)
if inp == "" { if inp == "" {
return time.Duration(0), nil return dur, nil
} }
var err error var err error
// Look for a suffix otherwise its a plain second value // Look for a suffix otherwise its a plain second value
@ -42,17 +43,23 @@ func ParseDurationSecond(in interface{}) (time.Duration, error) {
dur = time.Duration(secs) * time.Second dur = time.Duration(secs) * time.Second
} }
case int: case int:
dur = time.Duration(in.(int)) * time.Second dur = time.Duration(inp) * time.Second
case int32: case int32:
dur = time.Duration(in.(int32)) * time.Second dur = time.Duration(inp) * time.Second
case int64: case int64:
dur = time.Duration(in.(int64)) * time.Second dur = time.Duration(inp) * time.Second
case uint: case uint:
dur = time.Duration(in.(uint)) * time.Second dur = time.Duration(inp) * time.Second
case uint32: case uint32:
dur = time.Duration(in.(uint32)) * time.Second dur = time.Duration(inp) * time.Second
case uint64: case uint64:
dur = time.Duration(in.(uint64)) * time.Second dur = time.Duration(inp) * time.Second
case float32:
dur = time.Duration(inp) * time.Second
case float64:
dur = time.Duration(inp) * time.Second
case time.Duration:
dur = inp
default: default:
return 0, errors.New("could not parse duration from input") return 0, errors.New("could not parse duration from input")
} }