-
Notifications
You must be signed in to change notification settings - Fork 1
/
result.go
269 lines (201 loc) · 5.8 KB
/
result.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
package mapper
import (
"fmt"
"reflect"
"strconv"
"github.com/IQ-tech/go-errors"
)
type result struct {
fieldMap map[string]interface{}
}
// Merge mixes maps from source
func (r result) Merge(source interface{}) (retVal Result) {
// TODO
return retVal
}
// To sets target form map value
func (r result) To(trg interface{}) (err error) {
target := reflect.ValueOf(trg)
if target.Kind() != reflect.Ptr {
return errors.NewApplicationError("Error mapper: must receive a pointer, but received " + target.Kind().String())
}
if target.Elem().Kind() == reflect.Map {
err = r.setMap("", target.Elem())
if err != nil {
return errors.Wrap(err)
}
} else if target.Elem().Kind() == reflect.Slice {
err = r.setSlice("", target.Elem())
if err != nil {
return errors.Wrap(err)
}
} else if target.Elem().Kind() == reflect.Struct {
err = r.setStruct("", target)
if err != nil {
return errors.Wrap(err)
}
} else if target.Elem().Kind() == reflect.Interface {
err = r.To(target.Elem().Interface())
if err != nil {
return errors.Wrap(err)
}
} else {
err = r.setField("", target)
if err != nil {
return errors.Wrap(err)
}
}
return nil
}
func (r result) setMap(rootFieldName string, target reflect.Value) (err error) {
src, ok := r.fieldMap[rootFieldName]
if !ok {
return errors.NewApplicationError("Error mapper: Field is not found")
}
source := reflect.ValueOf(src)
target.Set(reflect.MakeMapWithSize(target.Type(), 0))
iter := source.MapRange()
for iter.Next() {
elementType := reflect.TypeOf(target.Interface()).Elem()
if elementType.Kind() == reflect.Struct && elementType.String() != timeType {
targetItem := reflect.New(elementType)
fieldName := rootFieldName
if fieldName == "" {
fieldName = fmt.Sprintf("[%v]", iter.Key())
} else {
fieldName = fmt.Sprintf("%s.[%v]", fieldName, iter.Key())
}
err = r.setStruct(fieldName, targetItem)
if err != nil {
return errors.Wrap(err)
}
key := reflect.ValueOf(iter.Key().Interface())
target.SetMapIndex(key, targetItem.Elem())
} else {
key := reflect.ValueOf(iter.Key().Interface())
rv := reflect.ValueOf(iter.Value().Interface()).Interface()
item, ok := rv.(reflect.Value)
if !ok {
return errors.NewApplicationError("Error mapper: Value from map is not an instance of reflect.Value")
}
value, ok := item.Interface().(reflect.Value)
if !ok {
return errors.NewApplicationError("Error mapper: Value from map is not an instance of reflect.Value")
}
target.SetMapIndex(key, value)
}
}
return nil
}
func (r result) setSlice(rootFieldName string, target reflect.Value) (err error) {
src, ok := r.fieldMap[rootFieldName]
if !ok {
return nil
}
source := reflect.ValueOf(src)
if source.Len() == 0 {
return nil
}
target.Set(reflect.MakeSlice(target.Type(), source.Len(), source.Cap()))
for i := 0; i < source.Len(); i++ {
sourceItem := source.Index(i)
targetItem := target.Index(i)
var value reflect.Value
// Corrigir para os casos de lista de structs
if targetItem.Kind() == reflect.Struct && targetItem.Type().String() != timeType {
fieldName := rootFieldName
if fieldName == "" {
fieldName = strconv.Itoa(i)
} else {
fieldName = fieldName + "." + strconv.Itoa(i)
}
err = r.setStruct(fieldName, targetItem.Addr())
if err != nil {
return errors.Wrap(err)
}
} else {
item, ok := sourceItem.Interface().(reflect.Value)
if !ok {
return errors.NewApplicationError("Error mapper: Value from list is not an instance of reflect.Value")
}
value, ok = item.Interface().(reflect.Value)
if !ok {
return errors.NewApplicationError("Error mapper: Value from list is not an instance of reflect.Value")
}
targetItem.Set(value)
}
}
return nil
}
func (r result) setStruct(rootFieldName string, trg reflect.Value) (err error) {
target := trg.Elem()
for i := 0; i < target.NumField(); i++ {
fieldMetada := target.Type().Field(i)
tagValue, foundTag := fieldMetada.Tag.Lookup(MapperTag)
field := target.Field(i)
fieldType := field.Kind()
if tagValue == "-" && foundTag {
continue
}
if rootFieldName != "" && foundTag {
tagValue = rootFieldName + "." + tagValue
} else if rootFieldName != "" {
tagValue = rootFieldName + "." + fieldMetada.Name
}
if tagValue == "" && foundTag {
return errors.NewApplicationError("Error mapper: tag view must have a value when it's present")
}
if tagValue == "" {
tagValue = fieldMetada.Name
}
if fieldType == reflect.Map {
err = r.setMap(tagValue, field)
if err != nil {
return errors.Wrap(err)
}
} else if fieldType == reflect.Slice {
err = r.setSlice(tagValue, field)
if err != nil {
return errors.Wrap(err)
}
} else if fieldType == reflect.Struct && field.Type().String() != timeType {
err = r.setStruct(tagValue, field.Addr())
if err != nil {
return errors.Wrap(err)
}
} else {
err = r.setField(tagValue, field)
if err != nil {
return errors.Wrap(err)
}
}
}
return nil
}
func (r result) setField(fieldName string, field reflect.Value) error {
item, ok := r.fieldMap[fieldName]
if !ok {
return nil
}
value, ok := item.(reflect.Value)
if !ok {
return errors.NewApplicationError("Error mapper: Value from map is not a reflect.Value")
}
if !field.IsValid() && !field.CanSet() {
return errors.NewApplicationError("Error mapper: Field is not valid")
}
if field.Kind() != value.Kind() {
return errors.NewApplicationError("Mapper error: different type field mapping")
}
switch field.Kind() {
case reflect.String:
field.SetString(value.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(value.Int())
case reflect.Float32, reflect.Float64:
field.SetFloat(value.Float())
default:
field.Set(value)
}
return nil
}