-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgt_null_interval.go
336 lines (281 loc) · 8.77 KB
/
gt_null_interval.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package gt
import (
"database/sql/driver"
"time"
)
/*
Shortcut: parses successfully or panics. Should be used only in root scope. When
error handling is relevant, use `.Parse`.
*/
func ParseNullInterval(src string) (val NullInterval) {
try(val.Parse(src))
return
}
// Simplified interval constructor without a time constituent.
func DateNullInterval(years int, months int, days int) NullInterval {
return NullInterval{Years: years, Months: months, Days: days}
}
// Simplified interval constructor without a date constituent.
func TimeNullInterval(hours, mins, secs int) NullInterval {
return NullInterval{Hours: hours, Minutes: mins, Seconds: secs}
}
// Simplified interval constructor.
func NullIntervalFrom(years int, months int, days, hours, mins, secs int) NullInterval {
return NullInterval{years, months, days, hours, mins, secs}
}
// Uses `.SetDuration` and returns the resulting interval.
func DurationNullInterval(src time.Duration) (val NullInterval) {
val.SetDuration(src)
return
}
/*
Variant of `gt.Interval` where zero value is considered empty in text, and null
in JSON and SQL.
*/
type NullInterval Interval
var (
_ = Encodable(NullInterval{})
_ = Decodable((*NullInterval)(nil))
)
// Implement `gt.Zeroable`. Equivalent to `reflect.ValueOf(self).IsZero()`.
func (self NullInterval) IsZero() bool { return Interval(self).IsZero() }
// Implement `gt.Nullable`. True if zero.
func (self NullInterval) IsNull() bool { return self.IsZero() }
/*
Implement `gt.Getter`. If zero, returns `nil`, otherwise uses `.String` to
return a string representation.
*/
func (self NullInterval) Get() any {
if self.IsNull() {
return nil
}
return Interval(self).Get()
}
// Implement `gt.Setter`, using `.Scan`. Panics on error.
func (self *NullInterval) Set(src any) { try(self.Scan(src)) }
// Implement `gt.Zeroer`, zeroing the receiver.
func (self *NullInterval) Zero() { (*Interval)(self).Zero() }
/*
Implement `fmt.Stringer`. If zero, returns an empty string. Otherwise returns a
text representation in the standard machine-readable ISO 8601 format.
*/
func (self NullInterval) String() string {
if self.IsNull() {
return ``
}
return Interval(self).String()
}
/*
Implement `gt.Parser`. If the input is empty, zeroes the receiver. Otherwise
requires a valid machine-readable ISO 8601 representation.
*/
func (self *NullInterval) Parse(src string) error {
if len(src) == 0 {
self.Zero()
return nil
}
return (*Interval)(self).Parse(src)
}
// Implement `gt.AppenderTo`, using the same representation as `.String`.
func (self NullInterval) AppendTo(buf []byte) []byte {
if self.IsNull() {
return buf
}
return Interval(self).AppendTo(buf)
}
/*
Implement `encoding.TextMarhaler`. If zero, returns nil. Otherwise returns the
same representation as `.String`.
*/
func (self NullInterval) MarshalText() ([]byte, error) {
if self.IsNull() {
return nil, nil
}
return Interval(self).MarshalText()
}
// Implement `encoding.TextUnmarshaler`, using the same algorithm as `.Parse`.
func (self *NullInterval) UnmarshalText(src []byte) error {
return self.Parse(bytesString(src))
}
/*
Implement `json.Marshaler`. If zero, returns bytes representing `null`.
Otherwise returns bytes representing a JSON string with the same text as in
`.String`.
*/
func (self NullInterval) MarshalJSON() ([]byte, error) {
if self.IsNull() {
return bytesNull, nil
}
return Interval(self).MarshalJSON()
}
/*
Implement `json.Unmarshaler`. If the input is empty or represents JSON `null`,
zeroes the receiver. Otherwise parses a JSON string, using the same algorithm
as `.Parse`.
*/
func (self *NullInterval) UnmarshalJSON(src []byte) error {
if isJsonEmpty(src) {
self.Zero()
return nil
}
if isJsonStr(src) {
return self.UnmarshalText(cutJsonStr(src))
}
return errJsonString(src, self)
}
// Implement `driver.Valuer`, using `.Get`.
func (self NullInterval) Value() (driver.Value, error) {
return self.Get(), nil
}
/*
Implement `sql.Scanner`, converting an arbitrary input to `gt.NullInterval` and
modifying the receiver. Acceptable inputs:
* `nil` -> use `.Zero`
* `string` -> use `.Parse`
* `[]byte` -> use `.UnmarshalText`
* `time.Duration` -> use `.SetDuration`
* `*time.Duration` -> use `.Zero` or `.SetDuration`
* `gt.Interval` -> assign
* `*gt.Interval` -> use `.Zero` or assign
* `gt.NullInterval` -> assign
* `gt.Getter` -> scan underlying value
*/
func (self *NullInterval) Scan(src any) error {
switch src := src.(type) {
case nil:
self.Zero()
return nil
case string:
return self.Parse(src)
case []byte:
return self.UnmarshalText(src)
case time.Duration:
self.SetDuration(src)
return nil
case *time.Duration:
if src == nil {
self.Zero()
} else {
self.SetDuration(*src)
}
return nil
case Interval:
*self = NullInterval(src)
return nil
case *Interval:
if src == nil {
self.Zero()
} else {
*self = NullInterval(*src)
}
return nil
case NullInterval:
*self = src
return nil
default:
val, ok := get(src)
if ok {
return self.Scan(val)
}
return errScanType(self, src)
}
}
// Same as `(*gt.Interval).SetDuration`.
func (self *NullInterval) SetDuration(val time.Duration) {
(*Interval)(self).SetDuration(val)
}
// Same as `gt.Interval.Date`.
func (self NullInterval) Date() (years int, months int, days int) {
return Interval(self).Date()
}
// Same as `gt.Interval.OnlyDate`.
func (self NullInterval) OnlyDate() NullInterval {
return NullInterval(Interval(self).OnlyDate())
}
// Same as `gt.Interval.OnlyTime`.
func (self NullInterval) OnlyTime() NullInterval {
return NullInterval(Interval(self).OnlyTime())
}
// Same as `gt.Interval.HasDate`.
func (self NullInterval) HasDate() bool {
return Interval(self).HasDate()
}
// Same as `gt.Interval.HasTime`.
func (self NullInterval) HasTime() bool {
return Interval(self).HasTime()
}
// Same as `gt.Interval.Duration`.
func (self NullInterval) Duration() time.Duration {
return Interval(self).Duration()
}
// Returns a version of this interval with `.Years = val`.
func (self NullInterval) WithYears(val int) NullInterval {
return NullInterval(Interval(self).WithYears(val))
}
// Returns a version of this interval with `.Months = val`.
func (self NullInterval) WithMonths(val int) NullInterval {
return NullInterval(Interval(self).WithMonths(val))
}
// Returns a version of this interval with `.Days = val`.
func (self NullInterval) WithDays(val int) NullInterval {
return NullInterval(Interval(self).WithDays(val))
}
// Returns a version of this interval with `.Hours = val`.
func (self NullInterval) WithHours(val int) NullInterval {
return NullInterval(Interval(self).WithHours(val))
}
// Returns a version of this interval with `.Minutes = val`.
func (self NullInterval) WithMinutes(val int) NullInterval {
return NullInterval(Interval(self).WithMinutes(val))
}
// Returns a version of this interval with `.Seconds = val`.
func (self NullInterval) WithSeconds(val int) NullInterval {
return NullInterval(Interval(self).WithSeconds(val))
}
// Returns a version of this interval with `.Years += val`.
func (self NullInterval) AddYears(val int) NullInterval {
return NullInterval(Interval(self).AddYears(val))
}
// Returns a version of this interval with `.Months += val`.
func (self NullInterval) AddMonths(val int) NullInterval {
return NullInterval(Interval(self).AddMonths(val))
}
// Returns a version of this interval with `.Days += val`.
func (self NullInterval) AddDays(val int) NullInterval {
return NullInterval(Interval(self).AddDays(val))
}
// Returns a version of this interval with `.Hours += val`.
func (self NullInterval) AddHours(val int) NullInterval {
return NullInterval(Interval(self).AddHours(val))
}
// Returns a version of this interval with `.Minutes += val`.
func (self NullInterval) AddMinutes(val int) NullInterval {
return NullInterval(Interval(self).AddMinutes(val))
}
// Returns a version of this interval with `.Seconds += val`.
func (self NullInterval) AddSeconds(val int) NullInterval {
return NullInterval(Interval(self).AddSeconds(val))
}
/*
Adds every field of one interval to every field of another interval, returning
the sum. Does NOT convert different time units, such as seconds to minutes or
vice versa.
*/
func (self NullInterval) Add(val NullInterval) NullInterval {
return NullInterval(Interval(self).Add(Interval(val)))
}
/*
Subtracts every field of one interval from every corresponding field of another
interval, returning the difference. Does NOT convert different time units, such
as seconds to minutes or vice versa.
*/
func (self NullInterval) Sub(val NullInterval) NullInterval {
return NullInterval(Interval(self).Sub(Interval(val)))
}
/*
Returns a version of this interval with every field inverted: positive fields
become negative, and negative fields become positive.
*/
func (self NullInterval) Neg() NullInterval {
return NullInterval(Interval(self).Neg())
}