types.go 2.6 KB

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