-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.go
200 lines (161 loc) · 2.88 KB
/
type.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
package genorm
import (
"database/sql"
"database/sql/driver"
"errors"
"time"
)
type ExprType interface {
driver.Valuer
}
type ColumnFieldExprType interface {
sql.Scanner
driver.Valuer
}
type ColumnFieldExprTypePointer[T ExprType] interface {
ColumnFieldExprType
*T
}
type ExprPrimitive interface {
bool |
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64 |
string | time.Time
}
type WrappedPrimitive[T ExprPrimitive] struct {
valid bool
val T
}
func Wrap[T ExprPrimitive](val T) WrappedPrimitive[T] {
return WrappedPrimitive[T]{
valid: true,
val: val,
}
}
func (wp *WrappedPrimitive[T]) Scan(src any) error {
var dest any = wp.val
switch dest.(type) {
case bool:
nb := sql.NullBool{}
err := nb.Scan(src)
if err != nil {
return err
}
wp.valid = nb.Valid
dest = nb.Bool
case int8:
ni := sql.NullInt16{}
err := ni.Scan(src)
if err != nil {
return err
}
wp.valid = ni.Valid
dest = int8(ni.Int16)
case int16:
ns := sql.NullInt16{}
err := ns.Scan(src)
if err != nil {
return err
}
wp.valid = ns.Valid
dest = ns.Int16
case int32:
ns := sql.NullInt32{}
err := ns.Scan(src)
if err != nil {
return err
}
wp.valid = ns.Valid
dest = ns.Int32
case int, int64:
ni := sql.NullInt64{}
err := ni.Scan(src)
if err != nil {
return err
}
wp.valid = ni.Valid
dest = ni.Int64
case byte: // uint8
nb := sql.NullByte{}
err := nb.Scan(src)
if err != nil {
return err
}
wp.valid = nb.Valid
dest = nb.Byte
case uint16:
ns := sql.NullInt32{}
err := ns.Scan(src)
if err != nil {
return err
}
wp.valid = ns.Valid
dest = uint16(ns.Int32)
case uint32:
ns := sql.NullInt64{}
err := ns.Scan(src)
if err != nil {
return err
}
wp.valid = ns.Valid
dest = uint32(ns.Int64)
case uint64:
ni := sql.NullInt64{}
err := ni.Scan(src)
if err != nil {
return err
}
wp.valid = ni.Valid
dest = uint64(ni.Int64)
case float32:
nf := sql.NullFloat64{}
err := nf.Scan(src)
if err != nil {
return err
}
wp.valid = nf.Valid
dest = float32(nf.Float64)
case float64:
nf := sql.NullFloat64{}
err := nf.Scan(src)
if err != nil {
return err
}
wp.valid = nf.Valid
dest = nf.Float64
case string:
ns := sql.NullString{}
err := ns.Scan(src)
if err != nil {
return err
}
wp.valid = ns.Valid
dest = ns.String
case time.Time:
nt := sql.NullTime{}
err := nt.Scan(src)
if err != nil {
return err
}
wp.valid = nt.Valid
dest = nt.Time
default:
return errors.New("unsupported type")
}
var ok bool
wp.val, ok = dest.(T)
if !ok {
return errors.New("failed to convert")
}
return nil
}
func (wp WrappedPrimitive[_]) Value() (driver.Value, error) {
if !wp.valid {
return nil, ErrNullValue
}
return wp.val, nil
}
func (wp WrappedPrimitive[T]) Val() (T, bool) {
return wp.val, wp.valid
}