...
1 package pgtype
2
3 import (
4 "fmt"
5 "math"
6 "strconv"
7 )
8
9
10
11
12
13
14
15
16
17
18
19
20 type QChar struct {
21 Int int8
22 Status Status
23 }
24
25 func (dst *QChar) Set(src interface{}) error {
26 if src == nil {
27 *dst = QChar{Status: Null}
28 return nil
29 }
30
31 if value, ok := src.(interface{ Get() interface{} }); ok {
32 value2 := value.Get()
33 if value2 != value {
34 return dst.Set(value2)
35 }
36 }
37
38 switch value := src.(type) {
39 case int8:
40 *dst = QChar{Int: value, Status: Present}
41 case uint8:
42 if value > math.MaxInt8 {
43 return fmt.Errorf("%d is greater than maximum value for QChar", value)
44 }
45 *dst = QChar{Int: int8(value), Status: Present}
46 case int16:
47 if value < math.MinInt8 {
48 return fmt.Errorf("%d is greater than maximum value for QChar", value)
49 }
50 if value > math.MaxInt8 {
51 return fmt.Errorf("%d is greater than maximum value for QChar", value)
52 }
53 *dst = QChar{Int: int8(value), Status: Present}
54 case uint16:
55 if value > math.MaxInt8 {
56 return fmt.Errorf("%d is greater than maximum value for QChar", value)
57 }
58 *dst = QChar{Int: int8(value), Status: Present}
59 case int32:
60 if value < math.MinInt8 {
61 return fmt.Errorf("%d is greater than maximum value for QChar", value)
62 }
63 if value > math.MaxInt8 {
64 return fmt.Errorf("%d is greater than maximum value for QChar", value)
65 }
66 *dst = QChar{Int: int8(value), Status: Present}
67 case uint32:
68 if value > math.MaxInt8 {
69 return fmt.Errorf("%d is greater than maximum value for QChar", value)
70 }
71 *dst = QChar{Int: int8(value), Status: Present}
72 case int64:
73 if value < math.MinInt8 {
74 return fmt.Errorf("%d is greater than maximum value for QChar", value)
75 }
76 if value > math.MaxInt8 {
77 return fmt.Errorf("%d is greater than maximum value for QChar", value)
78 }
79 *dst = QChar{Int: int8(value), Status: Present}
80 case uint64:
81 if value > math.MaxInt8 {
82 return fmt.Errorf("%d is greater than maximum value for QChar", value)
83 }
84 *dst = QChar{Int: int8(value), Status: Present}
85 case int:
86 if value < math.MinInt8 {
87 return fmt.Errorf("%d is greater than maximum value for QChar", value)
88 }
89 if value > math.MaxInt8 {
90 return fmt.Errorf("%d is greater than maximum value for QChar", value)
91 }
92 *dst = QChar{Int: int8(value), Status: Present}
93 case uint:
94 if value > math.MaxInt8 {
95 return fmt.Errorf("%d is greater than maximum value for QChar", value)
96 }
97 *dst = QChar{Int: int8(value), Status: Present}
98 case string:
99 num, err := strconv.ParseInt(value, 10, 8)
100 if err != nil {
101 return err
102 }
103 *dst = QChar{Int: int8(num), Status: Present}
104 default:
105 if originalSrc, ok := underlyingNumberType(src); ok {
106 return dst.Set(originalSrc)
107 }
108 return fmt.Errorf("cannot convert %v to QChar", value)
109 }
110
111 return nil
112 }
113
114 func (dst QChar) Get() interface{} {
115 switch dst.Status {
116 case Present:
117 return dst.Int
118 case Null:
119 return nil
120 default:
121 return dst.Status
122 }
123 }
124
125 func (src *QChar) AssignTo(dst interface{}) error {
126 return int64AssignTo(int64(src.Int), src.Status, dst)
127 }
128
129 func (dst *QChar) DecodeBinary(ci *ConnInfo, src []byte) error {
130 if src == nil {
131 *dst = QChar{Status: Null}
132 return nil
133 }
134
135 if len(src) != 1 {
136 return fmt.Errorf(`invalid length for "char": %v`, len(src))
137 }
138
139 *dst = QChar{Int: int8(src[0]), Status: Present}
140 return nil
141 }
142
143 func (src QChar) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
144 switch src.Status {
145 case Null:
146 return nil, nil
147 case Undefined:
148 return nil, errUndefined
149 }
150
151 return append(buf, byte(src.Int)), nil
152 }
153
View as plain text