-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields_iterator.go
127 lines (98 loc) · 2.61 KB
/
fields_iterator.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
package validator
import (
"reflect"
"strconv"
"strings"
vr "github.com/donatorsky/go-validator/rule"
)
func newFieldsIterator(field string, data any) <-chan fieldValue {
fieldsValues := make(chan fieldValue, 1)
go func() {
defer close(fieldsValues)
fieldParts := strings.Split(field, ".")
iterateOverFieldPart(fieldsValues, make([]string, len(fieldParts)), fieldParts, 0, data)
}()
return fieldsValues
}
type fieldValue struct {
field string
value any
}
func iterateOverFieldPart(fieldsValues chan<- fieldValue, fieldName []string, fieldParts []string, position int, value any) {
isNil := false
if value == nil {
isNil = true
} else {
value, isNil = vr.Dereference(value)
}
if isNil {
for idx := position; idx < len(fieldParts); idx++ {
fieldName[idx] = fieldParts[idx]
}
fieldsValues <- fieldValue{
field: strings.Join(fieldName, "."),
value: nil,
}
return
}
if len(fieldParts) == position {
fieldsValues <- fieldValue{
field: strings.Join(fieldName, "."),
value: value,
}
return
}
valueOf := reflect.ValueOf(value)
if fieldParts[position] == "*" {
if valueOf.Kind() == reflect.Slice || valueOf.Kind() == reflect.Array {
for idx := 0; idx < valueOf.Len(); idx++ {
fieldName[position] = strconv.Itoa(idx)
iterateOverFieldPart(fieldsValues, fieldName, fieldParts, position+1, valueOf.Index(idx).Interface())
}
return
}
for idx := position; idx < len(fieldParts); idx++ {
fieldName[idx] = fieldParts[idx]
}
fieldsValues <- fieldValue{
value: nil,
field: strings.Join(fieldName, "."),
}
return
}
switch valueOf.Kind() {
case reflect.Map:
mapIndex := valueOf.MapIndex(reflect.ValueOf(fieldParts[position]))
if mapIndex.IsValid() {
value = mapIndex.Interface()
} else {
value = nil
}
case reflect.Struct:
fieldByName := valueOf.FieldByName(fieldParts[position])
if fieldByName.IsValid() {
value = fieldByName.Interface()
} else {
typeOf := reflect.TypeOf(value)
value = nil
for idx := 0; idx < typeOf.NumField(); idx++ {
structField := typeOf.Field(idx)
nameFromTag := structField.Tag.Get("validation")
if nameFromTag != "" && nameFromTag == fieldParts[position] {
value = valueOf.FieldByName(structField.Name).Interface()
}
}
}
case reflect.Slice, reflect.Array:
idx, err := strconv.Atoi(fieldParts[position])
if err != nil || idx < 0 || idx >= valueOf.Len() {
value = nil
break
}
value = valueOf.Index(idx).Interface()
default:
value = nil
}
fieldName[position] = fieldParts[position]
iterateOverFieldPart(fieldsValues, fieldName, fieldParts, position+1, value)
}