-
Notifications
You must be signed in to change notification settings - Fork 1
/
duration.go
47 lines (37 loc) · 901 Bytes
/
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
package sqle
import (
"database/sql/driver"
"errors"
"time"
)
type Duration time.Duration
func (d Duration) Duration() time.Duration { // skipcq: GO-W1029
return time.Duration(d)
}
// Value implements the driver.Valuer interface,
// and turns the Duration into a VARCHAR field for MySQL storage.
func (d Duration) Value() (driver.Value, error) { // skipcq: GO-W1029
return time.Duration(d).String(), nil
}
// Scan implements the sql.Scanner interface,
// and turns the VARCHAR field incoming from MySQL into a Duration
func (d *Duration) Scan(src interface{}) error { // skipcq: GO-W1029
if src == nil {
return nil
}
var val string
switch v := src.(type) {
case []byte:
val = string(v)
case string:
val = v
default:
return errors.New("bad duration type assertion")
}
td, err := time.ParseDuration(val)
if err != nil {
return err
}
*d = Duration(td)
return nil
}