2019-01-05 01:51:32 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-01-18 18:39:12 +00:00
|
|
|
// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
2019-01-21 05:33:14 +00:00
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
2019-01-05 01:51:32 +00:00
|
|
|
|
2019-01-18 20:08:18 +00:00
|
|
|
// MarshalJSON parses a duration string and sets it to the duration.
|
2019-01-05 01:51:32 +00:00
|
|
|
//
|
|
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
|
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
2019-01-21 05:33:14 +00:00
|
|
|
func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.Duration.String())
|
2019-01-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:08:18 +00:00
|
|
|
// UnmarshalJSON parses a duration string and sets it to the duration.
|
2019-01-05 01:51:32 +00:00
|
|
|
//
|
2019-01-18 20:08:18 +00:00
|
|
|
// A duration string is a possibly signed sequence of decimal numbers, each with
|
2019-01-05 01:51:32 +00:00
|
|
|
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
|
|
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
2019-01-21 05:33:14 +00:00
|
|
|
func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
2019-01-18 20:08:18 +00:00
|
|
|
var (
|
|
|
|
s string
|
|
|
|
_d time.Duration
|
|
|
|
)
|
|
|
|
if d == nil {
|
|
|
|
return errors.New("duration cannot be nil")
|
|
|
|
}
|
2019-01-05 01:51:32 +00:00
|
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
|
|
}
|
2019-01-18 20:08:18 +00:00
|
|
|
if _d, err = time.ParseDuration(s); err != nil {
|
2019-01-05 01:51:32 +00:00
|
|
|
return errors.Wrapf(err, "error parsing %s as duration", s)
|
|
|
|
}
|
2019-01-21 05:33:14 +00:00
|
|
|
d.Duration = _d
|
2019-01-05 01:51:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:30:28 +00:00
|
|
|
// multiString represents a type that can be encoded/decoded in JSON as a single
|
|
|
|
// string or an array of strings.
|
2019-01-05 01:51:32 +00:00
|
|
|
type multiString []string
|
|
|
|
|
|
|
|
// First returns the first element of a multiString. It will return an empty
|
|
|
|
// string if the multistring is empty.
|
|
|
|
func (s multiString) First() string {
|
|
|
|
if len(s) > 0 {
|
|
|
|
return s[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:59:31 +00:00
|
|
|
// HasEmpties returns `true` if any string in the array is empty.
|
|
|
|
func (s multiString) HasEmpties() bool {
|
2019-01-05 01:51:32 +00:00
|
|
|
if len(s) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, ss := range s {
|
|
|
|
if len(ss) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals the multistring as a string or a slice of strings . With
|
|
|
|
// 0 elements it will return the empty string, with 1 element a regular string,
|
|
|
|
// otherwise a slice of strings.
|
|
|
|
func (s multiString) MarshalJSON() ([]byte, error) {
|
|
|
|
switch len(s) {
|
|
|
|
case 0:
|
2019-01-07 23:30:28 +00:00
|
|
|
return []byte(`""`), nil
|
2019-01-05 01:51:32 +00:00
|
|
|
case 1:
|
|
|
|
return json.Marshal(s[0])
|
|
|
|
default:
|
2019-01-07 23:30:28 +00:00
|
|
|
return json.Marshal([]string(s))
|
2019-01-05 01:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON parses a string or a slice and sets it to the multiString.
|
|
|
|
func (s *multiString) UnmarshalJSON(data []byte) error {
|
2019-01-07 23:30:28 +00:00
|
|
|
if s == nil {
|
|
|
|
return errors.New("multiString cannot be nil")
|
|
|
|
}
|
2019-01-05 01:51:32 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
*s = nil
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-07 23:30:28 +00:00
|
|
|
// Parse string
|
2019-01-05 01:51:32 +00:00
|
|
|
if data[0] == '"' {
|
|
|
|
var str string
|
|
|
|
if err := json.Unmarshal(data, &str); err != nil {
|
|
|
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
|
|
}
|
|
|
|
*s = []string{str}
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-07 23:30:28 +00:00
|
|
|
// Parse array
|
|
|
|
var ss []string
|
|
|
|
if err := json.Unmarshal(data, &ss); err != nil {
|
2019-01-05 01:51:32 +00:00
|
|
|
return errors.Wrapf(err, "error unmarshalling %s", data)
|
|
|
|
}
|
2019-01-07 23:30:28 +00:00
|
|
|
*s = ss
|
2019-01-05 01:51:32 +00:00
|
|
|
return nil
|
|
|
|
}
|