forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
180 lines (169 loc) · 3.32 KB
/
decode.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
package pg
import (
"database/sql"
"encoding/hex"
"reflect"
"strconv"
"time"
"gopkg.in/pg.v3/pgutil"
)
func Decode(dst interface{}, b []byte) error {
switch v := dst.(type) {
case *string:
*v = string(b)
return nil
case *int:
if b == nil {
*v = 0
return nil
}
var err error
*v, err = strconv.Atoi(string(b))
return err
case *int32:
if b == nil {
*v = 0
return nil
}
n, err := strconv.ParseInt(string(b), 10, 32)
*v = int32(n)
return err
case *int64:
if b == nil {
*v = 0
return nil
}
var err error
*v, err = strconv.ParseInt(string(b), 10, 64)
return err
case *time.Time:
if b == nil {
*v = time.Time{}
return nil
}
var err error
*v, err = pgutil.ParseTime(b)
return err
}
v := reflect.ValueOf(dst)
if !v.IsValid() {
return errorf("pg: Decode(nil)")
}
if v.Kind() != reflect.Ptr {
return errorf("pg: Decode(nonsettable %T)", dst)
}
vv := v.Elem()
if !vv.IsValid() {
return errorf("pg: Decode(nonsettable %T)", dst)
}
return DecodeValue(vv, b)
}
func decodeScanner(scanner sql.Scanner, b []byte) error {
if b == nil {
return scanner.Scan(nil)
}
return scanner.Scan(b)
}
func decodeBytes(f []byte) ([]byte, error) {
f = f[2:] // Trim off "\\x".
b := make([]byte, hex.DecodedLen(len(f)))
_, err := hex.Decode(b, f)
if err != nil {
return nil, err
}
return b, nil
}
func decodeIntSlice(f []byte) ([]int, error) {
p := newArrayParser(f[1 : len(f)-1])
s := make([]int, 0)
for p.Valid() {
elem, err := p.NextElem()
if err != nil {
return nil, err
}
if elem == nil {
return nil, errorf("pg: unexpected NULL: %q", f)
}
n, err := strconv.Atoi(string(elem))
if err != nil {
return nil, err
}
s = append(s, n)
}
return s, nil
}
func decodeInt64Slice(f []byte) ([]int64, error) {
p := newArrayParser(f[1 : len(f)-1])
s := make([]int64, 0)
for p.Valid() {
elem, err := p.NextElem()
if err != nil {
return nil, err
}
if elem == nil {
return nil, errorf("pg: unexpected NULL: %q", f)
}
n, err := strconv.ParseInt(string(elem), 10, 64)
if err != nil {
return nil, err
}
s = append(s, n)
}
return s, nil
}
func decodeFloat64Slice(b []byte) ([]float64, error) {
p := newArrayParser(b[1 : len(b)-1])
slice := make([]float64, 0)
for p.Valid() {
elem, err := p.NextElem()
if err != nil {
return nil, err
}
if elem == nil {
return nil, errorf("pg: unexpected NULL: %q", b)
}
n, err := strconv.ParseFloat(string(elem), 64)
if err != nil {
return nil, err
}
slice = append(slice, n)
}
return slice, nil
}
func decodeStringSlice(f []byte) ([]string, error) {
p := newArrayParser(f[1 : len(f)-1])
s := make([]string, 0)
for p.Valid() {
elem, err := p.NextElem()
if err != nil {
return nil, err
}
if elem == nil {
return nil, errorf("pg: unexpected NULL: %q", f)
}
s = append(s, string(elem))
}
return s, nil
}
func decodeStringStringMap(f []byte) (map[string]string, error) {
p := newHstoreParser(f)
m := make(map[string]string)
for p.Valid() {
key, err := p.NextKey()
if err != nil {
return nil, err
}
if key == nil {
return nil, errorf("pg: unexpected NULL: %q", f)
}
value, err := p.NextValue()
if err != nil {
return nil, err
}
if value == nil {
return nil, errorf("pg: unexpected NULL: %q", f)
}
m[string(key)] = string(value)
}
return m, nil
}