/
decode_utils.go
163 lines (150 loc) · 4.3 KB
/
decode_utils.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
package utils
import (
"fmt"
"reflect"
"strconv"
"github.com/mitchellh/mapstructure"
)
// DecodeHook is a valid mapstructure DecodeHook in order to correctly interpret
// json.Number, which is a subtype of string, as actual numbers in the target
// struct. This produces the correct error based on the target size (e.g.,
// the json number 256 can't be stored in a int8, so an error would be
// produced)
func DecodeHook(from reflect.Value, to reflect.Value) (interface{}, error) {
if from.Kind() == reflect.String {
switch to.Kind() {
case reflect.Int:
return strconv.Atoi(from.String())
case reflect.Int8:
a, e := strconv.ParseInt(from.String(), 10, 8)
return int8(a), e
case reflect.Int16:
a, e := strconv.ParseInt(from.String(), 10, 16)
return int16(a), e
case reflect.Int32:
a, e := strconv.ParseInt(from.String(), 10, 32)
return int32(a), e
case reflect.Int64:
return strconv.ParseInt(from.String(), 10, 64)
case reflect.Float32:
a, e := strconv.ParseFloat(from.String(), 32)
return float32(a), e
case reflect.Float64:
return strconv.ParseFloat(from.String(), 64)
}
} else if from.Kind() == reflect.Map && from.Type().Key().Kind() == reflect.String && from.Type().Elem().Kind() == reflect.String && to.Type().Key().Kind() == reflect.String {
// from is map[string]string
// to is map[string]SOMETHING
switch to.Type().Elem().Kind() {
case reflect.Int:
res := make(map[string]int, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.Atoi(v.String())
if err != nil {
return nil, err
}
res[k.String()] = parsed
}
return res, nil
case reflect.Int8:
res := make(map[string]int8, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseInt(v.String(), 10, 8)
if err != nil {
return nil, err
}
res[k.String()] = int8(parsed)
}
return res, nil
case reflect.Int16:
res := make(map[string]int16, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseInt(v.String(), 10, 16)
if err != nil {
return nil, err
}
res[k.String()] = int16(parsed)
}
return res, nil
case reflect.Int32:
res := make(map[string]int32, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseInt(v.String(), 10, 32)
if err != nil {
return nil, err
}
res[k.String()] = int32(parsed)
}
return res, nil
case reflect.Int64:
res := make(map[string]int64, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseInt(v.String(), 10, 64)
if err != nil {
return nil, err
}
res[k.String()] = parsed
}
return res, nil
case reflect.Float32:
res := make(map[string]float32, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseFloat(v.String(), 32)
if err != nil {
return nil, err
}
res[k.String()] = float32(parsed)
}
return res, nil
case reflect.Float64:
res := make(map[string]float64, from.Len())
iter := from.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
parsed, err := strconv.ParseFloat(v.String(), 64)
if err != nil {
return nil, err
}
res[k.String()] = parsed
}
return res, nil
}
}
return from.Interface(), nil
}
// DecodeWithType is a convenience method to use a mapstructure Decoder
// using the DecodeHook to decode the given map with the given target type.
func DecodeWithType(parsed map[string]interface{}, typ interface{}) (interface{}, error) {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Squash: true,
DecodeHook: DecodeHook,
Result: typ,
})
if err != nil {
return nil, fmt.Errorf("constructing decoder: %w", err)
}
err = decoder.Decode(parsed)
if err != nil {
return nil, fmt.Errorf("failed to decode: %w", err)
}
return typ, nil
}