types.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. type multiString []string
  34. // FIXME: remove me, avoids deadcode warning
  35. var _ = multiString{}
  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. // Empties checks that none of the string is empty.
  45. func (s multiString) Empties() 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(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 len(data) == 0 {
  72. *s = nil
  73. return nil
  74. }
  75. if data[0] == '"' {
  76. var str string
  77. if err := json.Unmarshal(data, &str); err != nil {
  78. return errors.Wrapf(err, "error unmarshalling %s", data)
  79. }
  80. *s = []string{str}
  81. return nil
  82. }
  83. if err := json.Unmarshal(data, s); err != nil {
  84. return errors.Wrapf(err, "error unmarshalling %s", data)
  85. }
  86. return nil
  87. }