-
Notifications
You must be signed in to change notification settings - Fork 2
/
structs_map_structure.go
194 lines (179 loc) · 4.93 KB
/
structs_map_structure.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
package xconf
import (
"fmt"
"reflect"
"strings"
"github.com/sandwich-go/xconf/xfield"
)
// Struct Struct类型定义
type Struct struct {
raw interface{}
value reflect.Value
tagName string
tagNameDefaultValue string
fieldTagConvertor FieldTagConvertor
}
// NewStruct 构造Struct类型
func NewStruct(s interface{}, tagName, tagNameDefaultValue string, ff FieldTagConvertor) *Struct {
return &Struct{
raw: s,
value: strctVal(s),
tagName: tagName,
tagNameDefaultValue: tagNameDefaultValue,
fieldTagConvertor: ff,
}
}
func strctVal(s interface{}) reflect.Value {
v := reflect.ValueOf(s)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic("not struct")
}
return v
}
// StructFieldPathInfo field信息
type StructFieldPathInfo struct {
TagListXConf xfield.TagList
Tag reflect.StructTag
FieldName string
FieldNameList []string
DefaultGot bool
DefaultString string
}
// Map 返回数据及字段类型信息
func (s *Struct) Map() (map[string]interface{}, map[string]StructFieldPathInfo) {
out := make(map[string]interface{})
outPath := make(map[string]StructFieldPathInfo)
s.fillMapStructure(out, outPath, "", nil)
return out, outPath
}
func (s *Struct) fillMapStructure(out map[string]interface{}, outPath map[string]StructFieldPathInfo, prefix string, fieldNames []string) {
if out == nil || outPath == nil {
return
}
if prefix != "" {
prefix += DefaultKeyDelim
}
fields := s.structFields()
for _, field := range fields {
name := field.Name
val := s.value.FieldByName(name)
tagVal, tagOpts := xfield.ParseTag(field.Tag.Get(s.tagName))
if tagVal == "" {
name = s.fieldTagConvertor(name)
} else {
name = tagVal
}
fullKey := prefix + name
fileNameNow := append(fieldNames, field.Name)
// TODO 指针类型且数据为nil,自动构造一个默认值便于后续分析,conf中的sub最好不要使用指针类型?
if val.Kind() == reflect.Ptr && val.IsNil() {
val = reflect.New(val.Type().Elem())
}
v := reflect.ValueOf(val.Interface())
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
isSubStruct := false
switch v.Kind() {
case reflect.Map, reflect.Struct:
isSubStruct = true
}
squash := false
if tagOpts.Has("squash") && isSubStruct {
fullKey = prefix
squash = true
}
finalVal := s.nested(val, outPath, fullKey, fileNameNow)
if squash {
for k := range finalVal.(map[string]interface{}) {
out[k] = finalVal.(map[string]interface{})[k]
}
} else {
out[name] = finalVal
}
defaultVal, defaultValGot := field.Tag.Lookup(s.tagNameDefaultValue)
// 忽略的字段
noConf := strings.HasPrefix(fullKey, "-") || strings.HasSuffix(fullKey, "-") || strings.Contains(fullKey, ".-.")
if !noConf {
outPath[fullKey] = StructFieldPathInfo{
DefaultString: defaultVal,
DefaultGot: defaultValGot,
FieldNameList: fileNameNow,
TagListXConf: tagOpts,
Tag: field.Tag,
FieldName: field.Name,
}
}
}
}
func (s *Struct) structFields() []reflect.StructField {
t := s.value.Type()
var f []reflect.StructField
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// 跳过私有字段
if field.PkgPath != "" {
continue
}
// 跳过omitted字段
if tag := field.Tag.Get(s.tagName); tag == "-" {
continue
}
f = append(f, field)
}
return f
}
// nested retrieves recursively all types for the given value and returns the
// nested value.
func (s *Struct) nested(val reflect.Value, outPath map[string]StructFieldPathInfo, prefix string, fieldNames []string) interface{} {
var finalVal interface{}
v := reflect.ValueOf(val.Interface())
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
n := NewStruct(val.Interface(), s.tagName, s.tagNameDefaultValue, s.fieldTagConvertor)
m := make(map[string]interface{})
n.fillMapStructure(m, outPath, prefix, fieldNames)
//do not add the converted value if there are no exported fields, ie:
//time.Time
if len(m) == 0 {
finalVal = val.Interface()
} else {
finalVal = m
}
case reflect.Map:
if len(val.MapKeys()) == 0 {
finalVal = val.Interface()
} else {
m := make(map[string]interface{}, val.Len())
for _, k := range val.MapKeys() {
m[fmt.Sprintf("%v", k)] = s.nested(val.MapIndex(k), outPath, prefix, fieldNames)
}
finalVal = m
}
case reflect.Slice, reflect.Array:
if val.Type().Kind() == reflect.Interface {
finalVal = val.Interface()
break
}
if val.Type().Elem().Kind() != reflect.Struct &&
!(val.Type().Elem().Kind() == reflect.Ptr &&
val.Type().Elem().Elem().Kind() == reflect.Struct) {
finalVal = val.Interface()
break
}
slices := make([]interface{}, val.Len())
for x := 0; x < val.Len(); x++ {
slices[x] = s.nested(val.Index(x), outPath, prefix, fieldNames)
}
finalVal = slices
default:
finalVal = val.Interface()
}
return finalVal
}