-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
97 lines (83 loc) · 1.56 KB
/
types.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
package dalc
import (
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"time"
)
type NullBytes struct {
Bytes []byte
Valid bool // Valid is true if Time is not NULL
}
func (n *NullBytes) Scan(value interface{}) (err error) {
if value == nil {
n.Bytes, n.Valid = nil, false
return nil
}
n.Valid = true
switch value.(type) {
case []byte:
n.Bytes = value.([]byte)
case string:
n.Bytes = []byte(value.(string))
default:
err = fmt.Errorf("dalc scan mysql time type failed, %s is not []uint8 and string", reflect.TypeOf(value).Name())
}
return
}
// Value implements the driver Valuer interface.
func (n NullBytes) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Bytes, nil
}
type NullJson NullBytes
func (n *NullJson) Unmarshal(v interface{}) (err error) {
if !n.Valid {
return
}
err = json.Unmarshal(n.Bytes, v)
return
}
func (n *NullJson) Marshal(v interface{}) (err error) {
if reflect.ValueOf(v).IsNil() {
n.Valid = false
return
}
p, err0 := json.Marshal(v)
if err0 != nil {
n.Valid = false
err = err0
return
}
n.Valid = true
n.Bytes = p
return
}
func NowTime() *NullTime {
return &NullTime{time.Time{}, true}
}
type NullTime struct {
Time time.Time
Valid bool
}
func (n *NullTime) Scan(value interface{}) error {
if value == nil {
n.Time, n.Valid = time.Time{}, false
return nil
}
switch value.(type) {
case time.Time:
n.Time = value.(time.Time)
n.Valid = true
}
return nil
}
func (n NullTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time, nil
}