2017-03-07 16:21:22 +00:00
|
|
|
package parseutil
|
2016-07-11 17:46:23 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-01 13:32:05 +00:00
|
|
|
"encoding/json"
|
2017-02-27 19:42:00 +00:00
|
|
|
"errors"
|
2018-04-17 22:52:09 +00:00
|
|
|
"fmt"
|
2016-07-11 17:46:23 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-03-07 16:21:22 +00:00
|
|
|
|
2018-04-17 22:52:09 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
sockaddr "github.com/hashicorp/go-sockaddr"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2017-03-07 16:21:22 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2016-07-11 17:46:23 +00:00
|
|
|
)
|
|
|
|
|
2017-02-27 19:42:00 +00:00
|
|
|
func ParseDurationSecond(in interface{}) (time.Duration, error) {
|
2016-07-11 18:19:35 +00:00
|
|
|
var dur time.Duration
|
2017-03-01 13:32:05 +00:00
|
|
|
jsonIn, ok := in.(json.Number)
|
|
|
|
if ok {
|
|
|
|
in = jsonIn.String()
|
|
|
|
}
|
2019-06-20 18:28:32 +00:00
|
|
|
switch inp := in.(type) {
|
|
|
|
case nil:
|
|
|
|
// return default of zero
|
2017-02-27 19:42:00 +00:00
|
|
|
case string:
|
2017-08-31 19:46:13 +00:00
|
|
|
if inp == "" {
|
2019-06-20 18:28:32 +00:00
|
|
|
return dur, nil
|
2017-08-31 19:46:13 +00:00
|
|
|
}
|
2017-02-27 19:42:00 +00:00
|
|
|
var err error
|
|
|
|
// Look for a suffix otherwise its a plain second value
|
2018-07-24 21:50:49 +00:00
|
|
|
if strings.HasSuffix(inp, "s") || strings.HasSuffix(inp, "m") || strings.HasSuffix(inp, "h") || strings.HasSuffix(inp, "ms") {
|
2017-02-27 19:42:00 +00:00
|
|
|
dur, err = time.ParseDuration(inp)
|
|
|
|
if err != nil {
|
|
|
|
return dur, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Plain integer
|
|
|
|
secs, err := strconv.ParseInt(inp, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return dur, err
|
|
|
|
}
|
|
|
|
dur = time.Duration(secs) * time.Second
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|
2017-02-27 19:42:00 +00:00
|
|
|
case int:
|
2019-06-20 18:28:32 +00:00
|
|
|
dur = time.Duration(inp) * time.Second
|
2017-02-27 19:42:00 +00:00
|
|
|
case int32:
|
2019-06-20 18:28:32 +00:00
|
|
|
dur = time.Duration(inp) * time.Second
|
2017-02-27 19:42:00 +00:00
|
|
|
case int64:
|
2019-06-20 18:28:32 +00:00
|
|
|
dur = time.Duration(inp) * time.Second
|
2017-02-27 19:42:00 +00:00
|
|
|
case uint:
|
2019-06-20 18:28:32 +00:00
|
|
|
dur = time.Duration(inp) * time.Second
|
2017-02-27 19:42:00 +00:00
|
|
|
case uint32:
|
2019-06-20 18:28:32 +00:00
|
|
|
dur = time.Duration(inp) * time.Second
|
2017-02-27 19:42:00 +00:00
|
|
|
case uint64:
|
2019-06-20 18:28:32 +00:00
|
|
|
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
|
2017-02-27 19:42:00 +00:00
|
|
|
default:
|
|
|
|
return 0, errors.New("could not parse duration from input")
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:35 +00:00
|
|
|
return dur, nil
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|
2017-03-07 16:21:22 +00:00
|
|
|
|
2020-09-09 20:53:51 +00:00
|
|
|
func ParseAbsoluteTime(in interface{}) (time.Time, error) {
|
|
|
|
var t time.Time
|
|
|
|
switch inp := in.(type) {
|
|
|
|
case nil:
|
|
|
|
// return default of zero
|
|
|
|
return t, nil
|
|
|
|
case string:
|
|
|
|
// Allow RFC3339 with nanoseconds, or without,
|
|
|
|
// or an epoch time as an integer.
|
|
|
|
var err error
|
|
|
|
t, err = time.Parse(time.RFC3339Nano, inp)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t, err = time.Parse(time.RFC3339, inp)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
epochTime, err := strconv.ParseInt(inp, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
t = time.Unix(epochTime, 0)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return t, errors.New("could not parse string as date and time")
|
|
|
|
case json.Number:
|
|
|
|
epochTime, err := inp.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
t = time.Unix(epochTime, 0)
|
|
|
|
case int:
|
|
|
|
t = time.Unix(int64(inp), 0)
|
|
|
|
case int32:
|
|
|
|
t = time.Unix(int64(inp), 0)
|
|
|
|
case int64:
|
|
|
|
t = time.Unix(inp, 0)
|
|
|
|
case uint:
|
|
|
|
t = time.Unix(int64(inp), 0)
|
|
|
|
case uint32:
|
|
|
|
t = time.Unix(int64(inp), 0)
|
|
|
|
case uint64:
|
|
|
|
t = time.Unix(int64(inp), 0)
|
|
|
|
default:
|
|
|
|
return t, errors.New("could not parse time from input type")
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2017-10-07 22:41:07 +00:00
|
|
|
func ParseInt(in interface{}) (int64, error) {
|
|
|
|
var ret int64
|
|
|
|
jsonIn, ok := in.(json.Number)
|
|
|
|
if ok {
|
|
|
|
in = jsonIn.String()
|
|
|
|
}
|
|
|
|
switch in.(type) {
|
|
|
|
case string:
|
|
|
|
inp := in.(string)
|
|
|
|
if inp == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
left, err := strconv.ParseInt(inp, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
ret = left
|
|
|
|
case int:
|
|
|
|
ret = int64(in.(int))
|
|
|
|
case int32:
|
|
|
|
ret = int64(in.(int32))
|
|
|
|
case int64:
|
|
|
|
ret = in.(int64)
|
|
|
|
case uint:
|
|
|
|
ret = int64(in.(uint))
|
|
|
|
case uint32:
|
|
|
|
ret = int64(in.(uint32))
|
|
|
|
case uint64:
|
|
|
|
ret = int64(in.(uint64))
|
|
|
|
default:
|
|
|
|
return 0, errors.New("could not parse value from input")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2017-03-07 16:21:22 +00:00
|
|
|
func ParseBool(in interface{}) (bool, error) {
|
|
|
|
var result bool
|
|
|
|
if err := mapstructure.WeakDecode(in, &result); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2018-02-16 22:19:34 +00:00
|
|
|
|
2020-07-27 20:28:52 +00:00
|
|
|
func ParseString(in interface{}) (string, error) {
|
|
|
|
var result string
|
|
|
|
if err := mapstructure.WeakDecode(in, &result); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:19:34 +00:00
|
|
|
func ParseCommaStringSlice(in interface{}) ([]string, error) {
|
2018-12-14 14:12:26 +00:00
|
|
|
rawString, ok := in.(string)
|
|
|
|
if ok && rawString == "" {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
2018-02-16 22:19:34 +00:00
|
|
|
var result []string
|
|
|
|
config := &mapstructure.DecoderConfig{
|
|
|
|
Result: &result,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
DecodeHook: mapstructure.StringToSliceHookFunc(","),
|
|
|
|
}
|
|
|
|
decoder, err := mapstructure.NewDecoder(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := decoder.Decode(in); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return strutil.TrimStrings(result), nil
|
|
|
|
}
|
2018-04-17 22:52:09 +00:00
|
|
|
|
|
|
|
func ParseAddrs(addrs interface{}) ([]*sockaddr.SockAddrMarshaler, error) {
|
|
|
|
out := make([]*sockaddr.SockAddrMarshaler, 0)
|
|
|
|
stringAddrs := make([]string, 0)
|
|
|
|
|
|
|
|
switch addrs.(type) {
|
|
|
|
case string:
|
|
|
|
stringAddrs = strutil.ParseArbitraryStringSlice(addrs.(string), ",")
|
|
|
|
if len(stringAddrs) == 0 {
|
|
|
|
return nil, fmt.Errorf("unable to parse addresses from %v", addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
case []string:
|
|
|
|
stringAddrs = addrs.([]string)
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
for _, v := range addrs.([]interface{}) {
|
|
|
|
stringAddr, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing %v as string", v)
|
|
|
|
}
|
|
|
|
stringAddrs = append(stringAddrs, stringAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown address input type %T", addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range stringAddrs {
|
|
|
|
sa, err := sockaddr.NewSockAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf(fmt.Sprintf("error parsing address %q: {{err}}", addr), err)
|
|
|
|
}
|
|
|
|
out = append(out, &sockaddr.SockAddrMarshaler{
|
|
|
|
SockAddr: sa,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|