-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration.go
183 lines (161 loc) · 3.43 KB
/
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package extratypes
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
)
// Duration is wrapper for time.Duration with additional methods
type Duration struct {
time.Duration
Nil bool
}
// Value that the database usage will see
func (d Duration) Value() (driver.Value, error) {
return int64(d.Duration), nil
}
// Scan the result from a query and assign it to the struct
func (d *Duration) Scan(v interface{}) error {
if v == nil {
d.Duration = -1
d.Nil = true
return nil
}
val := reflect.ValueOf(v)
kind := val.Kind()
switch kind {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
d.Duration = time.Duration(val.Int())
if d.Duration == -1 {
d.Nil = true
}
case reflect.String:
var err error
str := val.String()
if str == "" {
d.Duration = -1
d.Nil = true
return nil
}
d.Duration, err = time.ParseDuration(str)
if err != nil {
return err
}
case reflect.Float32, reflect.Float64:
f := val.Float()
if f == 0 || f < 0 {
d.Duration = -1
d.Nil = true
return nil
}
d.Duration = time.Duration(f)
case reflect.Invalid:
d.Duration = -1
d.Nil = true
return nil
default:
return fmt.Errorf("Invalid type of %T", val)
}
return nil
}
// MarshalJSON takes a duration and marshal it as a string
func (d Duration) MarshalJSON() ([]byte, error) {
if d.Nil {
return json.Marshal(nil)
}
return json.Marshal(d.String())
}
// MarshalText takes duration and marshal it as a string
func (d *Duration) MarshalText() ([]byte, error) {
if d.Nil {
return []byte(""), nil
}
return []byte(d.Duration.String()), nil
}
// UnmarshalJSON takes a slice of bytes and convert it to Duration
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
val := reflect.ValueOf(v)
kind := val.Kind()
switch kind {
case reflect.Float32, reflect.Float64:
d.Duration = time.Duration(val.Float())
return nil
case reflect.String:
var err error
d.Duration, err = time.ParseDuration(val.String())
if err != nil {
return err
}
return nil
case reflect.Map:
iface := val.Interface()
m, success := iface.(map[string]interface{})
if !success {
return errors.New("Invalid map type")
}
l := len(m)
if l == 0 {
return errors.New("no content found")
}
if l > 1 {
return fmt.Errorf("Length %d is too big", l)
}
var err error
for _, value := range m {
val2 := reflect.ValueOf(value)
kind2 := val2.Kind()
switch kind2 {
case reflect.Float32, reflect.Float64:
d.Duration = time.Duration(val2.Float())
return nil
case reflect.String:
d.Duration, err = time.ParseDuration(val2.String())
if err != nil {
return err
}
return nil
case reflect.Invalid:
d.Duration = -1
d.Nil = true
return nil
default:
return fmt.Errorf("Invalid kind of value: %s", kind2)
}
}
case reflect.Invalid:
d.Duration = -1
d.Nil = true
return nil
default:
return fmt.Errorf("Invalid duration type: %T, %+v", v, v)
}
return errors.New("Unknown error")
}
// UnmarshalText unmar
func (d *Duration) UnmarshalText(b []byte) error {
if len(b) == 0 {
d.Duration = -1
d.Nil = true
return nil
}
var err error
d.Duration, err = time.ParseDuration(string(b))
if err != nil {
d.Duration = -1
d.Nil = true
return err
}
return nil
}
func (d Duration) String() string {
if d.Nil {
return "nil"
}
return d.Duration.String()
}