-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopt_duration.go
94 lines (82 loc) · 2.21 KB
/
opt_duration.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package configtypes
import (
"encoding/json"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
)
// OptDuration represents an optional time.Duration parameter. Any time.Duration value is allowed; if you
// want to allow only positive values (which is usually desirable), use OptDurationNonNegative.
//
// When setting this value from a string representation, it uses time.ParseDuration, so the allowed formats
// include "9ms" (milliseconds), "9s" (seconds), "9m" (minutes), or combinations such as "1m30s". Converting
// to a string uses similar rules, as implemented by Duration.String().
//
// When converting to or from JSON, an empty value is null, and all other values are strings.
//
// See the package documentation for the general contract for methods that have no specific documentation
// here.
type OptDuration struct {
hasValue bool
value time.Duration
}
func NewOptDuration(value time.Duration) OptDuration {
return OptDuration{hasValue: true, value: value}
}
func NewOptDurationFromString(s string) (OptDuration, error) {
if s == "" {
return OptDuration{}, nil
}
value, err := time.ParseDuration(s)
if err == nil {
return NewOptDuration(value), nil
}
return OptDuration{}, errDurationFormat()
}
func (o OptDuration) IsDefined() bool {
return o.hasValue
}
func (o OptDuration) GetOrElse(orElseValue time.Duration) time.Duration {
if !o.hasValue {
return orElseValue
}
return o.value
}
func (o OptDuration) String() string {
if !o.hasValue {
return ""
}
return o.value.String()
}
func (o OptDuration) MarshalText() ([]byte, error) {
return []byte(o.String()), nil
}
func (o *OptDuration) UnmarshalText(data []byte) error {
opt, err := NewOptDurationFromString(string(data))
if err == nil {
*o = opt
}
return err
}
func (o OptDuration) MarshalJSON() ([]byte, error) {
if o.hasValue {
return json.Marshal(o.String())
}
return json.Marshal(nil)
}
func (o *OptDuration) UnmarshalJSON(data []byte) error {
var v ldvalue.Value
var err error
if err = v.UnmarshalJSON(data); err != nil {
return err
}
switch {
case v.IsNull():
*o = OptDuration{}
return nil
case v.IsString():
*o, err = NewOptDurationFromString(v.StringValue())
return err
default:
return errDurationFormat()
}
}