-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
198 lines (187 loc) · 5.4 KB
/
unmarshal.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
package redmap
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
)
// StringMapUnmarshaler is the interface implemented by types that can unmarshal themselves
// from a map of strings. Implementations must copy the given map if they wish to modify it.
type StringMapUnmarshaler interface {
UnmarshalStringMap(map[string]string) error
}
// Unmarshal sets v's fields according to its map representation contained by data.
// v must be a pointer to struct or an interface. Neither data nor v can be nil.
//
// Unmarshal uses the inverse of the encodings that Marshal uses, so all the types supported
// by it are also supported in Unmarshal, except fmt.Stringer which doesn't have an inverse.
//
// The decoding of each struct field can be customized by the format string documented in Marshal.
func Unmarshal(data map[string]string, v interface{}) error {
if data == nil {
return errIs("map passed", ErrNilValue)
}
val, err := ptrValidValue(v)
if err != nil {
return err
}
return unmarshalRecursive(data, "", val)
}
func ptrValidValue(v interface{}) (reflect.Value, error) {
val := reflect.ValueOf(v)
kin := val.Kind()
switch kin {
case reflect.Ptr:
case reflect.Invalid:
return reflect.Value{}, errIs("argument provided", ErrNilValue)
default:
return reflect.Value{}, errIs(val.Type(), ErrNotPointer)
}
for kin == reflect.Ptr {
val = val.Elem()
kin = val.Kind()
}
if kin == reflect.Invalid {
return reflect.Value{}, errIs(reflect.TypeOf(v), ErrNilValue)
}
return val, nil
}
func unmarshalRecursive(mp map[string]string, prefix string, stru reflect.Value) error {
if ptr := stru.Addr(); ptr.Type().Implements(mapUnmarshalerType) {
return mapToStruct(mp, prefix, ptr)
}
if stru.Kind() != reflect.Struct {
return errIs(stru.Type(), ErrNoCodec)
}
typ := stru.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if field.PkgPath != "" {
// We don't want to unmarshal unexported fields. PkgPath is empty for exported fields.
// TODO: In Go 1.17, use field.IsExported().
continue
}
tags := redmapTags(field.Tag)
if tags.ignored {
continue
}
value := stru.Field(i)
if tags.name == "" {
tags.name = field.Name
}
tags.name = prefix + tags.name
for value.Kind() == reflect.Ptr {
if value.IsNil() && !tags.omitempty {
if !value.CanSet() {
return fmt.Errorf("cannot set embedded pointer to unexported type %s", value.Elem().Type())
}
value.Set(reflect.New(value.Type().Elem()))
}
value = value.Elem()
}
if tags.inline {
err := unmarshalRecursive(mp, tags.name+inlineSep, value)
if err != nil {
return err
}
} else {
str, ok := mp[tags.name]
if !ok {
continue
}
err := stringToField(str, value, tags.omitempty)
if err != nil {
return err
}
}
}
return nil
}
func mapToStruct(mp map[string]string, prefix string, stru reflect.Value) error {
if prefix != "" {
// FIXME: Creating a submap is O(n). Can we think of a better algorithm?
subMP := make(map[string]string, len(mp))
for k, v := range mp {
if !strings.HasPrefix(k, prefix) {
continue
}
subMP[k[len(prefix):]] = v
}
mp = subMP
}
return stru.Interface().(StringMapUnmarshaler).UnmarshalStringMap(mp)
}
func stringToField(str string, field reflect.Value, omitempty bool) error {
addr := field.Addr() // Unmarshaling always requires a pointer receiver.
if addr.Type().Implements(textUnmarshalerType) {
return addr.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(str))
}
var (
val reflect.Value
err error
)
switch field.Kind() {
case reflect.Bool:
v, e := strconv.ParseBool(str)
val, err = reflect.ValueOf(v), e
case reflect.Int:
v, e := strconv.ParseInt(str, 10, 0)
val, err = reflect.ValueOf(int(v)), e
case reflect.Int8:
v, e := strconv.ParseInt(str, 10, 8)
val, err = reflect.ValueOf(int8(v)), e
case reflect.Int16:
v, e := strconv.ParseInt(str, 10, 16)
val, err = reflect.ValueOf(int16(v)), e
case reflect.Int32:
v, e := strconv.ParseInt(str, 10, 32)
val, err = reflect.ValueOf(int32(v)), e
case reflect.Int64:
v, e := strconv.ParseInt(str, 10, 64)
val, err = reflect.ValueOf(v), e
case reflect.Uint:
v, e := strconv.ParseUint(str, 10, 0)
val, err = reflect.ValueOf(uint(v)), e
case reflect.Uint8:
v, e := strconv.ParseUint(str, 10, 8)
val, err = reflect.ValueOf(uint8(v)), e
case reflect.Uint16:
v, e := strconv.ParseUint(str, 10, 16)
val, err = reflect.ValueOf(uint16(v)), e
case reflect.Uint32:
v, e := strconv.ParseUint(str, 10, 32)
val, err = reflect.ValueOf(uint32(v)), e
case reflect.Uint64:
v, e := strconv.ParseUint(str, 10, 64)
val, err = reflect.ValueOf(v), e
case reflect.Float32:
v, e := strconv.ParseFloat(str, 32)
val, err = reflect.ValueOf(float32(v)), e
case reflect.Float64:
v, e := strconv.ParseFloat(str, 64)
val, err = reflect.ValueOf(v), e
case reflect.Complex64:
v, e := strconv.ParseComplex(str, 64)
val, err = reflect.ValueOf(complex64(v)), e
case reflect.Complex128:
v, e := strconv.ParseComplex(str, 128)
val, err = reflect.ValueOf(v), e
case reflect.String:
val, err = reflect.ValueOf(str), nil
default:
return fmt.Errorf("%s doesn't implement TextUnmarshaler", addr)
}
if err != nil {
return err
}
if omitempty && val.IsZero() {
return nil
}
field.Set(val)
return nil
}
var (
mapUnmarshalerType = reflect.TypeOf(new(StringMapUnmarshaler)).Elem()
textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
)