forked from digisan/go-generics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
353 lines (311 loc) · 8.28 KB
/
struct.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package gogenerics
import (
"fmt"
"log"
"reflect"
"strings"
"time"
"unicode"
)
// get all fields
func Fields(object any) (fields []string) {
if reflect.ValueOf(object).Kind() == reflect.Ptr {
ptr := reflect.ValueOf(object).Elem()
object = ptr.Interface()
}
typ := reflect.TypeOf(object)
// fmt.Println("Type:", typ.Name(), "Kind:", typ.Kind())
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fields = append(fields, field.Name)
}
return
}
// get only exported field value
func FieldValue(object any, field string) (any, error) {
defer func() {
if r := recover(); r != nil {
log.Printf("Cannot get field '%s' in struct '%v'\n", field, reflect.TypeOf(object))
}
}()
if IsArrOrSlc(object) {
arr := TypesAsAnyToAnys(object)
idx, ok := AnyTryToType[int](field)
if !ok {
return nil, fmt.Errorf("field for array must be number")
}
if idx >= len(arr) {
return nil, fmt.Errorf("field(idx) is out of range of object(array/slice)")
}
return arr[idx], nil
}
if len(field) > 0 && unicode.IsUpper(rune(field[0])) {
ov := reflect.ValueOf(object)
f := reflect.Indirect(ov).FieldByName(field)
if !f.IsValid() || f.Kind() == 0 {
return nil, fmt.Errorf("field '%s' is NOT in struct '%v'", field, reflect.TypeOf(object))
}
return f.Interface(), nil
}
return nil, fmt.Errorf("'%v' field '%s' is NOT exported", reflect.TypeOf(object), field)
}
func PathValue(object any, path string) (v any, err error) {
for _, seg := range strings.Split(path, ".") {
if v, err = FieldValue(object, seg); err != nil {
return nil, err
}
object = v
}
return v, err
}
// field must be exported, AND param value type can be converted to field value type.
func SetField(object any, field string, value any) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
ov := reflect.ValueOf(object)
if IsArrOrSlc(object) {
idx, ok := AnyTryToType[int](field)
if !ok {
return fmt.Errorf("field for array must be number")
}
if idx >= len(TypesAsAnyToAnys(object)) {
return fmt.Errorf("field(idx) is out of range of object(array/slice)")
}
if e := ov.Index(idx); e.IsValid() {
e.Set(reflect.ValueOf(value))
return nil
}
goto ERR
}
if len(field) > 0 && unicode.IsUpper(rune(field[0])) {
if ov.Kind() != reflect.Ptr {
return fmt.Errorf("object need to pass its address")
}
if f := reflect.Indirect(ov).FieldByName(field); f.IsValid() {
fKind, fType := f.Kind().String(), f.Type().String()
switch fKind {
case "struct", "slice", "array", "map", "ptr", "interface":
if fType != "time.Time" {
f.Set(reflect.ValueOf(value))
return nil
}
}
///////////////////////////////////////////////////
switch fType {
case "string":
if val, ok := AnyTryToType[string](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "int":
if val, ok := AnyTryToType[int](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "int8":
if val, ok := AnyTryToType[int8](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "int16":
if val, ok := AnyTryToType[int16](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "int32":
if val, ok := AnyTryToType[int32](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "int64":
if val, ok := AnyTryToType[int64](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "uint":
if val, ok := AnyTryToType[uint](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "uint8":
if val, ok := AnyTryToType[uint8](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "uint16":
if val, ok := AnyTryToType[uint16](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "uint32":
if val, ok := AnyTryToType[uint32](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "uint64":
if val, ok := AnyTryToType[uint64](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "float64":
if val, ok := AnyTryToType[float64](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "float32":
if val, ok := AnyTryToType[float32](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "bool":
if val, ok := AnyTryToType[bool](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
case "time.Time":
if val, ok := AnyTryToType[time.Time](value); ok {
f.Set(reflect.ValueOf(val))
return nil
}
goto ERR
default:
log.Fatalf("need type [%v] for setting '%v' value @ [%v]", fType, field, value)
}
}
}
ERR:
return fmt.Errorf("field '%v' failed to set value @ [%v]", field, value)
}
// set simple primitive
func FlatMapSetField[T any](fm map[string]any, object any, field string) error {
if v, ok := FlatMapValTryToType[T](fm, field); ok {
if err := SetField(object, field, v); err != nil {
return err
}
} else {
return fmt.Errorf("[%v] cannot be found or set as [%T]", field, *new(T))
}
return nil
}
// set primitive array or slice
func FlatMapSetFieldAsSlc[T any](fm map[string]any, object any, field string) error {
if v, ok := FlatMapValsTryToTypes[T](fm, field); ok {
if err := SetField(object, field, v); err != nil {
return err
}
} else {
return fmt.Errorf("[%v] cannot be found or set as [[]%T]", field, *new(T))
}
return nil
}
// set map of primitive key & value
func FlatMapSetFieldAsMap[T1 comparable, T2 any](fm map[string]any, object any, field string) error {
m := make(map[T1]T2)
for _, k := range FlatMapSubKeys(fm, field) {
pk := field + "." + k
if v, ok := FlatMapValTryToType[T2](fm, pk); ok {
if key, ok := AnyTryToType[T1](k); ok {
m[key] = v
} else {
return fmt.Errorf("map key cannot be converted to type %T", *new(T1))
}
} else {
return fmt.Errorf("[%v] cannot be found or set as [map[%T]%T]", field, *new(T1), *new(T2))
}
}
if len(m) > 0 {
return SetField(object, field, m)
}
return nil
}
// set map of primitive key & slice of primitive value
func FlatMapSetFieldAsSlcValMap[T1 comparable, T2 any](fm map[string]any, object any, field string) error {
m := make(map[T1][]T2)
for _, k := range FlatMapSubKeys(fm, field) {
pk := field + "." + k
if v, ok := FlatMapValsTryToTypes[T2](fm, pk); ok {
if key, ok := AnyTryToType[T1](k); ok {
m[key] = v
} else {
return fmt.Errorf("map key cannot be converted to type %T", *new(T1))
}
} else {
return fmt.Errorf("[%v] cannot be found or set as [map[%T][]%T]", field, *new(T1), *new(T2))
}
}
if len(m) > 0 {
return SetField(object, field, m)
}
return nil
}
// func PartialAsMap(object any, fields ...string) (any, error) {
// part := make(map[string]any)
// for _, field := range fields {
// v, err := FieldValue(object, field)
// if err != nil {
// return nil, err
// }
// part[field] = v
// }
// return part, nil
// }
func PartialAsMap(object any, paths ...string) (any, error) {
mFlat := make(map[string]any)
for _, path := range paths {
v, err := PathValue(object, path)
if err != nil {
return nil, err
}
mFlat[path] = v
}
return MapFlatToNested(mFlat, nil), nil
}
// get all tags
func Tags(object any, tag string, exclTags ...string) (tags []string) {
if NotIn(tag, "json", "validate") {
panic("tag must be [json, validate]")
}
if reflect.ValueOf(object).Kind() == reflect.Ptr {
ptr := reflect.ValueOf(object).Elem()
object = ptr.Interface()
}
typ := reflect.TypeOf(object)
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get(tag)
// fmt.Printf("%d. %v (%v), tag: '%v'\n", i+1, field.Name, field.Type.Name(), tag)
tags = append(tags, strings.Split(tag, ",")...)
}
tags = Settify(tags...)
FilterFast(&tags,
func(i int, e string) bool {
return len(e) > 0 && NotIn(e, exclTags...)
},
)
return
}
// get all validator tags
func ValidatorTags(object any, exclTags ...string) (tags []string) {
return Tags(object, "validate", exclTags...)
}
// get all json tags
func JsonTags(object any, exclTags ...string) (tags []string) {
return Tags(object, "json", exclTags...)
}