-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathduration.go
More file actions
55 lines (46 loc) · 1.21 KB
/
duration.go
File metadata and controls
55 lines (46 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package configtype
import (
"encoding/json"
"time"
"github.com/invopop/jsonschema"
)
// Duration is a wrapper around time.Duration that should be used in config
// when a duration type is required. We wrap the time.Duration type so that
// the spec can be extended in the future to support other types of durations
// (e.g. a duration that is specified in days).
type Duration struct {
duration time.Duration
}
func NewDuration(d time.Duration) Duration {
return Duration{
duration: d,
}
}
func (Duration) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Pattern: `^[-+]?([0-9]*(\.[0-9]*)?[a-z]+)+$`, // copied from time.ParseDuration
Title: "CloudQuery configtype.Duration",
}
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
duration, err := time.ParseDuration(s)
if err != nil {
return err
}
*d = Duration{duration: duration}
return nil
}
func (d *Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.duration.String())
}
func (d *Duration) Duration() time.Duration {
return d.duration
}
func (d Duration) Equal(other Duration) bool {
return d.duration == other.duration
}