1 package pgtype
2
3 import (
4 "database/sql/driver"
5 "encoding/binary"
6 "encoding/json"
7 "fmt"
8 "math"
9 "strconv"
10
11 "github.com/jackc/pgio"
12 )
13
14 type Int2 struct {
15 Int int16
16 Status Status
17 }
18
19 func (dst *Int2) Set(src interface{}) error {
20 if src == nil {
21 *dst = Int2{Status: Null}
22 return nil
23 }
24
25 if value, ok := src.(interface{ Get() interface{} }); ok {
26 value2 := value.Get()
27 if value2 != value {
28 return dst.Set(value2)
29 }
30 }
31
32 switch value := src.(type) {
33 case int8:
34 *dst = Int2{Int: int16(value), Status: Present}
35 case uint8:
36 *dst = Int2{Int: int16(value), Status: Present}
37 case int16:
38 *dst = Int2{Int: int16(value), Status: Present}
39 case uint16:
40 if value > math.MaxInt16 {
41 return fmt.Errorf("%d is greater than maximum value for Int2", value)
42 }
43 *dst = Int2{Int: int16(value), Status: Present}
44 case int32:
45 if value < math.MinInt16 {
46 return fmt.Errorf("%d is greater than maximum value for Int2", value)
47 }
48 if value > math.MaxInt16 {
49 return fmt.Errorf("%d is greater than maximum value for Int2", value)
50 }
51 *dst = Int2{Int: int16(value), Status: Present}
52 case uint32:
53 if value > math.MaxInt16 {
54 return fmt.Errorf("%d is greater than maximum value for Int2", value)
55 }
56 *dst = Int2{Int: int16(value), Status: Present}
57 case int64:
58 if value < math.MinInt16 {
59 return fmt.Errorf("%d is greater than maximum value for Int2", value)
60 }
61 if value > math.MaxInt16 {
62 return fmt.Errorf("%d is greater than maximum value for Int2", value)
63 }
64 *dst = Int2{Int: int16(value), Status: Present}
65 case uint64:
66 if value > math.MaxInt16 {
67 return fmt.Errorf("%d is greater than maximum value for Int2", value)
68 }
69 *dst = Int2{Int: int16(value), Status: Present}
70 case int:
71 if value < math.MinInt16 {
72 return fmt.Errorf("%d is greater than maximum value for Int2", value)
73 }
74 if value > math.MaxInt16 {
75 return fmt.Errorf("%d is greater than maximum value for Int2", value)
76 }
77 *dst = Int2{Int: int16(value), Status: Present}
78 case uint:
79 if value > math.MaxInt16 {
80 return fmt.Errorf("%d is greater than maximum value for Int2", value)
81 }
82 *dst = Int2{Int: int16(value), Status: Present}
83 case string:
84 num, err := strconv.ParseInt(value, 10, 16)
85 if err != nil {
86 return err
87 }
88 *dst = Int2{Int: int16(num), Status: Present}
89 case float32:
90 if value > math.MaxInt16 {
91 return fmt.Errorf("%f is greater than maximum value for Int2", value)
92 }
93 *dst = Int2{Int: int16(value), Status: Present}
94 case float64:
95 if value > math.MaxInt16 {
96 return fmt.Errorf("%f is greater than maximum value for Int2", value)
97 }
98 *dst = Int2{Int: int16(value), Status: Present}
99 case *int8:
100 if value == nil {
101 *dst = Int2{Status: Null}
102 } else {
103 return dst.Set(*value)
104 }
105 case *uint8:
106 if value == nil {
107 *dst = Int2{Status: Null}
108 } else {
109 return dst.Set(*value)
110 }
111 case *int16:
112 if value == nil {
113 *dst = Int2{Status: Null}
114 } else {
115 return dst.Set(*value)
116 }
117 case *uint16:
118 if value == nil {
119 *dst = Int2{Status: Null}
120 } else {
121 return dst.Set(*value)
122 }
123 case *int32:
124 if value == nil {
125 *dst = Int2{Status: Null}
126 } else {
127 return dst.Set(*value)
128 }
129 case *uint32:
130 if value == nil {
131 *dst = Int2{Status: Null}
132 } else {
133 return dst.Set(*value)
134 }
135 case *int64:
136 if value == nil {
137 *dst = Int2{Status: Null}
138 } else {
139 return dst.Set(*value)
140 }
141 case *uint64:
142 if value == nil {
143 *dst = Int2{Status: Null}
144 } else {
145 return dst.Set(*value)
146 }
147 case *int:
148 if value == nil {
149 *dst = Int2{Status: Null}
150 } else {
151 return dst.Set(*value)
152 }
153 case *uint:
154 if value == nil {
155 *dst = Int2{Status: Null}
156 } else {
157 return dst.Set(*value)
158 }
159 case *string:
160 if value == nil {
161 *dst = Int2{Status: Null}
162 } else {
163 return dst.Set(*value)
164 }
165 case *float32:
166 if value == nil {
167 *dst = Int2{Status: Null}
168 } else {
169 return dst.Set(*value)
170 }
171 case *float64:
172 if value == nil {
173 *dst = Int2{Status: Null}
174 } else {
175 return dst.Set(*value)
176 }
177 default:
178 if originalSrc, ok := underlyingNumberType(src); ok {
179 return dst.Set(originalSrc)
180 }
181 return fmt.Errorf("cannot convert %v to Int2", value)
182 }
183
184 return nil
185 }
186
187 func (dst Int2) Get() interface{} {
188 switch dst.Status {
189 case Present:
190 return dst.Int
191 case Null:
192 return nil
193 default:
194 return dst.Status
195 }
196 }
197
198 func (src *Int2) AssignTo(dst interface{}) error {
199 return int64AssignTo(int64(src.Int), src.Status, dst)
200 }
201
202 func (dst *Int2) DecodeText(ci *ConnInfo, src []byte) error {
203 if src == nil {
204 *dst = Int2{Status: Null}
205 return nil
206 }
207
208 n, err := strconv.ParseInt(string(src), 10, 16)
209 if err != nil {
210 return err
211 }
212
213 *dst = Int2{Int: int16(n), Status: Present}
214 return nil
215 }
216
217 func (dst *Int2) DecodeBinary(ci *ConnInfo, src []byte) error {
218 if src == nil {
219 *dst = Int2{Status: Null}
220 return nil
221 }
222
223 if len(src) != 2 {
224 return fmt.Errorf("invalid length for int2: %v", len(src))
225 }
226
227 n := int16(binary.BigEndian.Uint16(src))
228 *dst = Int2{Int: n, Status: Present}
229 return nil
230 }
231
232 func (src Int2) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
233 switch src.Status {
234 case Null:
235 return nil, nil
236 case Undefined:
237 return nil, errUndefined
238 }
239
240 return append(buf, strconv.FormatInt(int64(src.Int), 10)...), nil
241 }
242
243 func (src Int2) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
244 switch src.Status {
245 case Null:
246 return nil, nil
247 case Undefined:
248 return nil, errUndefined
249 }
250
251 return pgio.AppendInt16(buf, src.Int), nil
252 }
253
254
255 func (dst *Int2) Scan(src interface{}) error {
256 if src == nil {
257 *dst = Int2{Status: Null}
258 return nil
259 }
260
261 switch src := src.(type) {
262 case int64:
263 if src < math.MinInt16 {
264 return fmt.Errorf("%d is greater than maximum value for Int2", src)
265 }
266 if src > math.MaxInt16 {
267 return fmt.Errorf("%d is greater than maximum value for Int2", src)
268 }
269 *dst = Int2{Int: int16(src), Status: Present}
270 return nil
271 case string:
272 return dst.DecodeText(nil, []byte(src))
273 case []byte:
274 srcCopy := make([]byte, len(src))
275 copy(srcCopy, src)
276 return dst.DecodeText(nil, srcCopy)
277 }
278
279 return fmt.Errorf("cannot scan %T", src)
280 }
281
282
283 func (src Int2) Value() (driver.Value, error) {
284 switch src.Status {
285 case Present:
286 return int64(src.Int), nil
287 case Null:
288 return nil, nil
289 default:
290 return nil, errUndefined
291 }
292 }
293
294 func (src Int2) MarshalJSON() ([]byte, error) {
295 switch src.Status {
296 case Present:
297 return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
298 case Null:
299 return []byte("null"), nil
300 case Undefined:
301 return nil, errUndefined
302 }
303
304 return nil, errBadStatus
305 }
306
307 func (dst *Int2) UnmarshalJSON(b []byte) error {
308 var n *int16
309 err := json.Unmarshal(b, &n)
310 if err != nil {
311 return err
312 }
313
314 if n == nil {
315 *dst = Int2{Status: Null}
316 } else {
317 *dst = Int2{Int: *n, Status: Present}
318 }
319
320 return nil
321 }
322
View as plain text