types.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package authority
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/pkg/errors"
  6. )
  7. // Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
  8. type Duration struct {
  9. time.Duration
  10. }
  11. // MarshalJSON parses a duration string and sets it to the duration.
  12. //
  13. // A duration string is a possibly signed sequence of decimal numbers, each with
  14. // optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
  15. // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  16. func (d *Duration) MarshalJSON() ([]byte, error) {
  17. return json.Marshal(d.Duration.String())
  18. }
  19. // UnmarshalJSON parses a duration string and sets it to the duration.
  20. //
  21. // A duration string is a possibly signed sequence of decimal numbers, each with
  22. // optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
  23. // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  24. func (d *Duration) UnmarshalJSON(data []byte) (err error) {
  25. var (
  26. s string
  27. _d time.Duration
  28. )
  29. if d == nil {
  30. return errors.New("duration cannot be nil")
  31. }
  32. if err = json.Unmarshal(data, &s); err != nil {
  33. return errors.Wrapf(err, "error unmarshalling %s", data)
  34. }
  35. if _d, err = time.ParseDuration(s); err != nil {
  36. return errors.Wrapf(err, "error parsing %s as duration", s)
  37. }
  38. d.Duration = _d
  39. return
  40. }
  41. // multiString represents a type that can be encoded/decoded in JSON as a single
  42. // string or an array of strings.
  43. type multiString []string
  44. // First returns the first element of a multiString. It will return an empty
  45. // string if the multistring is empty.
  46. func (s multiString) First() string {
  47. if len(s) > 0 {
  48. return s[0]
  49. }
  50. return ""
  51. }
  52. // HasEmpties returns `true` if any string in the array is empty.
  53. func (s multiString) HasEmpties() bool {
  54. if len(s) == 0 {
  55. return true
  56. }
  57. for _, ss := range s {
  58. if len(ss) == 0 {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. // MarshalJSON marshals the multistring as a string or a slice of strings . With
  65. // 0 elements it will return the empty string, with 1 element a regular string,
  66. // otherwise a slice of strings.
  67. func (s multiString) MarshalJSON() ([]byte, error) {
  68. switch len(s) {
  69. case 0:
  70. return []byte(`""`), nil
  71. case 1:
  72. return json.Marshal(s[0])
  73. default:
  74. return json.Marshal([]string(s))
  75. }
  76. }
  77. // UnmarshalJSON parses a string or a slice and sets it to the multiString.
  78. func (s *multiString) UnmarshalJSON(data []byte) error {
  79. if s == nil {
  80. return errors.New("multiString cannot be nil")
  81. }
  82. if len(data) == 0 {
  83. *s = nil
  84. return nil
  85. }
  86. // Parse string
  87. if data[0] == '"' {
  88. var str string
  89. if err := json.Unmarshal(data, &str); err != nil {
  90. return errors.Wrapf(err, "error unmarshalling %s", data)
  91. }
  92. *s = []string{str}
  93. return nil
  94. }
  95. // Parse array
  96. var ss []string
  97. if err := json.Unmarshal(data, &ss); err != nil {
  98. return errors.Wrapf(err, "error unmarshalling %s", data)
  99. }
  100. *s = ss
  101. return nil
  102. }