-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode.go
226 lines (208 loc) · 4.92 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package ltsv
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
)
// UnmarshalError is an error type for Unmarshal()
type UnmarshalError map[string]error
func (m UnmarshalError) Error() string {
if len(m) == 0 {
return "(no error)"
}
ee := make([]string, 0, len(m))
for name, err := range m {
ee = append(ee, fmt.Sprintf("field %q: %s", name, err))
}
return strings.Join(ee, "\n")
}
// OfField returns the error correspoinding to a given field
func (m UnmarshalError) OfField(name string) error {
return m[name]
}
// An UnmarshalTypeError describes a LTSV value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string
Type reflect.Type
}
func (e *UnmarshalTypeError) Error() string {
return "ltsv: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
}
type ltsvMap map[string]string
func data2map(data []byte) (ltsvMap, error) {
d := string(data)
l := ltsvMap{}
var i int
var v string
for i < len(d) {
j := strings.IndexByte(d[i:], '\t')
if j >= 0 {
v = d[i : i+j]
i += j + 1
} else {
v = d[i:]
i = len(d)
}
k := strings.IndexByte(v, ':')
if k < 0 {
return nil, fmt.Errorf("not a ltsv: %s", d)
}
l[strings.TrimSpace(v[:k])] = strings.TrimSpace(v[k+1:])
}
return l, nil
}
// Unmarshal parses the LTSV-encoded data and stores the result
// in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("not a pointer: %v", v)
}
rv = rv.Elem()
if rv.Kind() != reflect.Struct && rv.Kind() != reflect.Map {
return fmt.Errorf("not a pointer to a struct/map: %v", v)
}
l, err := data2map(data)
if err != nil {
return err
}
if rv.Kind() == reflect.Map {
kt := rv.Type().Key()
vt := rv.Type().Elem()
if kt.Kind() != reflect.String || vt.Kind() != reflect.String {
return fmt.Errorf("not a map[string]string")
}
for k, v := range l {
kv := reflect.ValueOf(k).Convert(kt)
vv := reflect.ValueOf(v).Convert(vt)
rv.SetMapIndex(kv, vv)
}
return nil
}
t := rv.Type()
errs := UnmarshalError{}
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i)
fv := rv.Field(i)
key := ft.Tag.Get("ltsv")
if i := strings.IndexByte(key, ','); i >= 0 {
key = key[:i]
}
if key == "-" {
continue
}
if key == "" {
key = strings.ToLower(ft.Name)
}
s, ok := l[key]
if !ok {
continue
}
potentiallyNull := s == "-" || s == ""
if fv.Kind() == reflect.Ptr {
if fv.IsNil() {
if potentiallyNull {
switch fv.Type().Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
continue
}
}
fv.Set(reflect.New(fv.Type().Elem()))
}
fv = fv.Elem()
}
if !fv.CanSet() {
continue
}
switch fv.Kind() {
case reflect.String:
fv.SetString(s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if potentiallyNull {
continue
}
i, err := strconv.ParseInt(s, 10, 64)
if err != nil || fv.OverflowInt(i) {
errs[ft.Name] = &UnmarshalTypeError{"number " + s, fv.Type()}
continue
}
fv.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if potentiallyNull {
continue
}
i, err := strconv.ParseUint(s, 10, 64)
if err != nil || fv.OverflowUint(i) {
errs[ft.Name] = &UnmarshalTypeError{"number " + s, fv.Type()}
continue
}
fv.SetUint(i)
case reflect.Float32, reflect.Float64:
if potentiallyNull {
continue
}
n, err := strconv.ParseFloat(s, fv.Type().Bits())
if err != nil || fv.OverflowFloat(n) {
errs[ft.Name] = &UnmarshalTypeError{"number " + s, fv.Type()}
continue
}
fv.SetFloat(n)
case reflect.Bool:
if potentiallyNull {
continue
}
b, err := strconv.ParseBool(s)
if err != nil {
errs[ft.Name] = &UnmarshalTypeError{Value: "bool " + s, Type: fv.Type()}
continue
}
fv.SetBool(b)
default:
u := indirect(fv)
if u == nil {
errs[ft.Name] = &UnmarshalTypeError{s, fv.Type()}
} else {
err := u.UnmarshalText([]byte(s))
if err != nil {
errs[ft.Name] = err
}
}
}
}
if len(errs) < 1 {
return nil
}
return errs
}
func indirect(v reflect.Value) encoding.TextUnmarshaler {
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
v = v.Addr()
}
for {
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && e.Elem().Kind() == reflect.Ptr {
v = e
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.Elem().Kind() != reflect.Ptr && v.CanSet() {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
if v.Type().NumMethod() > 0 {
if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
return u
}
}
}
return nil
}