-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.go
169 lines (123 loc) · 3.74 KB
/
map.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
package mapper
import (
"fmt"
"reflect"
"strconv"
)
type mapper struct {
}
// From create map form source
func (m *mapper) From(src interface{}) Result {
retVal := result{}
// Create Map
source := reflect.ValueOf(src)
if source.Kind() == reflect.Ptr {
src = source.Elem().Interface()
source = reflect.ValueOf(source.Elem().Interface())
}
if source.Kind() == reflect.Map {
retVal.fieldMap = m.buildMapFromMap("", src)
} else if source.Kind() == reflect.Slice {
retVal.fieldMap = m.buildMapFromSlice("", src)
} else if source.Kind() == reflect.Interface {
retVal = m.From(source.Interface()).(result)
} else {
retVal.fieldMap = m.buildMapFromStruct("", src)
}
return retVal
}
func (m *mapper) buildMapFromMap(rootFieldName string, src interface{}) (mapField map[string]interface{}) {
source := reflect.ValueOf(src)
mapField = make(map[string]interface{})
srcMap := make(map[interface{}]interface{})
iter := source.MapRange()
for iter.Next() {
fieldName := rootFieldName
if fieldName == "" {
fieldName = fmt.Sprintf("[%v]", iter.Key())
} else {
fieldName = fmt.Sprintf("%s.[%v]", fieldName, iter.Key())
}
if iter.Value().Kind() == reflect.Struct && iter.Value().Type().String() != timeType {
mapItem := m.buildMapFromStruct(fieldName, iter.Value().Interface())
for k, v := range mapItem {
mapField[k] = v
}
mapField[fieldName] = mapItem
key := iter.Key().Interface()
srcMap[key] = mapItem
} else {
itemMap := reflect.ValueOf(iter.Value())
mapField[fieldName] = itemMap
key := iter.Key().Interface()
srcMap[key] = itemMap
}
}
mapField[rootFieldName] = srcMap
return mapField
}
func (m *mapper) buildMapFromSlice(rootFieldName string, src interface{}) (mapField map[string]interface{}) {
source := reflect.ValueOf(src)
mapField = make(map[string]interface{})
list := []interface{}{}
for i := 0; i < source.Len(); i++ {
sourceItem := source.Index(i)
fieldName := rootFieldName
if fieldName == "" {
fieldName = strconv.Itoa(i)
} else {
fieldName = fieldName + "." + strconv.Itoa(i)
}
if sourceItem.Kind() == reflect.Struct && sourceItem.Type().String() != timeType {
mapItem := m.buildMapFromStruct(fieldName, sourceItem.Interface())
for k, v := range mapItem {
mapField[k] = v
}
list = append(list, mapItem)
} else {
itemSlice := reflect.ValueOf(sourceItem)
mapField[fieldName] = itemSlice
list = append(list, itemSlice)
}
}
mapField[rootFieldName] = list
return mapField
}
func (m *mapper) buildMapFromStruct(rootFieldName string, item interface{}) (mapField map[string]interface{}) {
itemStruct := reflect.ValueOf(item)
mapField = make(map[string]interface{})
for i := 0; i < itemStruct.NumField(); i++ {
field := itemStruct.Field(i)
fieldMetada := itemStruct.Type().Field(i)
tagValue, foundTag := fieldMetada.Tag.Lookup(MapperTag)
if tagValue == "-" && foundTag {
continue
}
if rootFieldName != "" && foundTag {
tagValue = rootFieldName + "." + tagValue
} else if rootFieldName != "" && !foundTag {
tagValue = rootFieldName + "." + fieldMetada.Name
} else if rootFieldName == "" && !foundTag {
tagValue = fieldMetada.Name
}
if field.Kind() == reflect.Map {
mapItem := m.buildMapFromMap(tagValue, field.Interface())
for k, v := range mapItem {
mapField[k] = v
}
} else if field.Kind() == reflect.Slice {
mapItem := m.buildMapFromSlice(tagValue, field.Interface())
for k, v := range mapItem {
mapField[k] = v
}
} else if field.Kind() == reflect.Struct && field.Type().String() != timeType {
mapStruct := m.buildMapFromStruct(tagValue, field.Interface())
for k, v := range mapStruct {
mapField[k] = v
}
} else {
mapField[tagValue] = field
}
}
return mapField
}