forked from c9s/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
128 lines (114 loc) · 2.69 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
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
package gatsby
import (
"database/sql/driver"
"fmt"
"strconv"
)
// NullInt32 represents an int32 that may be null.
// NullInt32 implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullInt32 struct {
Int32 int32
Valid bool // Valid is true if Int32 is not NULL
}
// Scan implements the Scanner interface.
func (n *NullInt32) Scan(value interface{}) error {
if value == nil {
n.Int32, n.Valid = 0, false
return nil
}
n.Valid = true
if val, ok := value.(int32); ok {
n.Int32 = val
return nil
}
var s = asString(value)
val, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return fmt.Errorf("converting string %q to a int32: %v", s, err)
}
n.Int32 = int32(val)
return nil
}
// Value implements the driver Valuer interface.
func (n NullInt32) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Int32, nil
}
// NullInt16 represents an int64 that may be null.
// NullInt16 implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullInt16 struct {
Int16 int16
Valid bool // Valid is true if Int16 is not NULL
}
// Scan implements the Scanner interface.
func (n *NullInt16) Scan(value interface{}) error {
if value == nil {
n.Int16, n.Valid = 0, false
return nil
}
n.Valid = true
if val, ok := value.(int16); ok {
n.Int16 = val
return nil
}
var s = asString(value)
val, err := strconv.ParseInt(s, 10, 16)
if err != nil {
return fmt.Errorf("converting string %q to a int16: %v", s, err)
}
n.Int16 = int16(val)
return nil
}
// Value implements the driver Valuer interface.
func (n NullInt16) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Int16, nil
}
// NullInt8 represents an int64 that may be null.
// NullInt8 implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullInt8 struct {
Int8 int8
Valid bool // Valid is true if Int8 is not NULL
}
// Scan implements the Scanner interface.
func (n *NullInt8) Scan(value interface{}) error {
if value == nil {
n.Int8, n.Valid = 0, false
return nil
}
n.Valid = true
if val, ok := value.(int8); ok {
n.Int8 = val
return nil
}
var s = asString(value)
val, err := strconv.ParseInt(s, 10, 8)
if err != nil {
return fmt.Errorf("converting string %q to a int8: %v", s, err)
}
n.Int8 = int8(val)
return nil
}
// Value implements the driver Valuer interface.
func (n NullInt8) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Int8, nil
}
func asString(src interface{}) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
return fmt.Sprintf("%v", src)
}