-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
294 lines (251 loc) · 6.48 KB
/
helpers.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package mpath
import (
"reflect"
"strings"
"github.com/shopspring/decimal"
)
func repeatTabs(numTabs int) string {
return strings.Repeat("\t", numTabs)
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
}
return false
}
func convertToDecimalIfNumberAndCheck(val any) (wasNumber bool, out decimal.Decimal) {
v := reflect.ValueOf(val)
if !isEmptyValue(v) {
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
v = v.Elem()
}
}
if !(isNumberKind(v.Kind()) || v.Kind() == reflect.String) {
return
}
switch outType := val.(type) {
case string:
var err error
out, err = decimal.NewFromString(outType)
if err != nil {
return false, decimal.Zero
}
case int:
out = decimal.NewFromInt(int64(outType))
case int8:
out = decimal.NewFromInt(int64(outType))
case int16:
out = decimal.NewFromInt(int64(outType))
case int32:
out = decimal.NewFromInt(int64(outType))
case int64:
out = decimal.NewFromInt(int64(outType))
case uint:
out = decimal.NewFromInt(int64(outType))
case uint8:
out = decimal.NewFromInt(int64(outType))
case uint16:
out = decimal.NewFromInt(int64(outType))
case uint32:
out = decimal.NewFromInt(int64(outType))
case uint64:
out = decimal.NewFromInt(int64(outType))
case float32:
out = decimal.NewFromFloat(float64(outType))
case float64:
out = decimal.NewFromFloat(outType)
}
wasNumber = true
return
}
func convertToDecimalIfNumber(val any) (out any) {
if wasNumber, number := convertToDecimalIfNumberAndCheck(val); wasNumber {
return number
}
return val
}
func isNumberKind(k reflect.Kind) bool {
switch k {
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:
return true
}
return false
}
func getValuesByName(identName string, data any) (out any, err error) {
v := reflect.ValueOf(data)
if !isEmptyValue(v) {
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
var wasFound bool
out, wasFound = getFieldValueByNameFromStruct(identName, v)
if wasFound {
return
}
return nil, ErrKeyNotFound
case reflect.Array, reflect.Slice:
if v.Len() == 0 {
return nil, ErrKeyNotFound
}
fev := v.Index(0)
switch fev.Kind() {
case reflect.Pointer, reflect.Interface:
fev = fev.Elem()
}
if k := fev.Kind(); !(k == reflect.Struct || k == reflect.Map) {
return nil, ErrKeyNotFound
}
var slc []any
var found bool
for i := 0; i < v.Len(); i++ {
if out, found = getFieldValueByNameFromStruct(identName, v.Index(i)); found {
slc = append(slc, out)
}
}
if len(slc) > 0 {
return slc, nil
}
}
}
return nil, ErrKeyNotFound
}
func getAsStructOrSlice(data any) (out any, ok, wasStruct bool) {
if m, ok := data.(map[string]any); ok {
// this is the JSON version of a struct
return m, true, true
}
v := reflect.ValueOf(data)
// if !isEmptyValue(v) {
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
return v.Interface(), true, true
case reflect.Array, reflect.Slice:
if v.Len() == 0 {
return []any{}, true, false
}
if slc, ok := v.Interface().([]any); ok {
return slc, true, false
}
var slc []any
for i := 0; i < v.Len(); i++ {
slc = append(slc, v.Index(i).Interface())
}
return slc, true, false
}
// }
return nil, false, false
}
func getFieldValueByNameFromStruct(identName string, structValue reflect.Value) (out any, found bool) {
if isEmptyValue(structValue) {
return nil, false
}
switch structValue.Kind() {
case reflect.Pointer, reflect.Interface:
structValue = structValue.Elem()
}
svk := structValue.Kind()
if svk == reflect.Map {
for _, e := range structValue.MapKeys() {
mks, ok := e.Interface().(string)
if !ok {
if reflect.TypeOf(e.Interface()).ConvertibleTo(reflect.TypeOf("")) {
mksTemp := reflect.ValueOf(e.Interface()).Convert(reflect.TypeOf("")).Interface()
mks, ok = mksTemp.(string)
if !ok || mks == "" {
continue
}
} else {
continue
}
}
if !strings.EqualFold(mks, identName) {
continue
}
return convertToDecimalIfNumber(structValue.MapIndex(e).Interface()), true
}
return nil, false
}
if svk != reflect.Struct {
return nil, false
}
st := structValue.Type()
for fn := 0; fn < structValue.NumField(); fn++ {
structFieldName := st.Field(fn).Name
if strings.EqualFold(structFieldName, identName) {
out = structValue.Field(fn).Interface()
switch outType := out.(type) {
case float64:
out = decimal.NewFromFloat(outType)
case float32:
out = decimal.NewFromFloat(float64(outType))
case int:
out = decimal.NewFromInt(int64(outType))
case int8:
out = decimal.NewFromInt(int64(outType))
case int16:
out = decimal.NewFromInt(int64(outType))
case int32:
out = decimal.NewFromInt(int64(outType))
case int64:
out = decimal.NewFromInt(int64(outType))
case uint:
out = decimal.NewFromInt(int64(outType))
case uint8:
out = decimal.NewFromInt(int64(outType))
case uint16:
out = decimal.NewFromInt(int64(outType))
case uint32:
out = decimal.NewFromInt(int64(outType))
case uint64:
out = decimal.NewFromInt(int64(outType))
}
return out, true
}
}
return nil, false
}
func doForMapPerKey(valueThatShouldBeMap any, doFunc func(keyAsString string, keyAsValue, mapAsValue reflect.Value)) {
v := reflect.ValueOf(valueThatShouldBeMap)
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
v = v.Elem()
}
if v.Kind() == reflect.Map {
for _, e := range v.MapKeys() {
mks, ok := e.Interface().(string)
if !ok {
if reflect.TypeOf(e.Interface()).ConvertibleTo(reflect.TypeOf("")) {
mksTemp := reflect.ValueOf(e.Interface()).Convert(reflect.TypeOf("")).Interface()
mks, ok = mksTemp.(string)
if !ok || mks == "" {
continue
}
} else {
continue
}
}
doFunc(mks, e, v)
}
}
}