-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
252 lines (214 loc) · 5.23 KB
/
field.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
package astconf
import (
"reflect"
"sort"
)
type fieldSet []field
func (fs fieldSet) Len() int {
return len(fs)
}
func (fs fieldSet) Swap(i, j int) {
fs[i], fs[j] = fs[j], fs[i]
}
func (fs fieldSet) Less(i, j int) bool {
// Any field that could possibly start its own section should always go
// at the end. This helps to avoid accidental mixing of fields that
// belong to different sections.
//
// Right now we do this in a hacky way by assuming that every "block" is
// a potential section starter. The term "block" here is nebulous, but
// right now blocks are things that could print multiple lines, including
// structs, slices, and types with custom marshalers.
//
// Probably the more correct thing to do is to accurately determine
// whether a type really could be a section starter by examining it and
// all its descedent elements. A type with any of these qualities should
// be considered:
// 1. The type itself is a Sectioner
// 2. The type is a struct that contains an embedded sectioner
// 3. The type itself has a MarshalAsterisk() function
// 4. The type is a struct that ontains an embedded MarshalAsterisk() function
// 5. The type is a struct with any non-embedded member that meets any of 1-4
//
// Such an analysis should probably be perform by typeFeatures().
a := &fs[i]
b := &fs[j]
// Note: We skip multi-value slices because their elements aren't blocks
aIsBlock := a.Block() && !a.MultiValue()
bIsBlock := b.Block() && !b.MultiValue()
// Non-blocks before blocks
if !aIsBlock && bIsBlock {
return true
}
if aIsBlock && !bIsBlock {
return false
}
// If both are blocks, non-sectioners before sectioners
if aIsBlock && bIsBlock {
if !a.Sectioner() && b.Sectioner() {
return true
}
if a.Sectioner() && !b.Sectioner() {
return false
}
}
return false
}
type field struct {
index []int
name string
embedded bool
typeFeature
}
// TODO: Cache fieldsets for each type
//var tfieldsMap sync.Map // map[reflect.Type]*fieldSet
// typeFields returns a list of struct fields that the encoder/decoder
// should process.
func typeFields(typ reflect.Type, allowAddr bool) (fields fieldSet) {
if typ.Kind() != reflect.Struct {
return nil
}
for i, n := 0, typ.NumField(); i < n; i++ {
sf := typ.Field(i)
tag := parseTag(sf.Tag.Get("astconf"))
tagFeatures := tagFeatures(&tag)
features := typeFeatures(sf.Type) | tagFeatures
name := tag.name
if name == "" {
name = sf.Name
}
fields = append(fields, field{
index: []int{i},
name: name,
embedded: sf.Anonymous,
typeFeature: features,
})
// Embed fields from anonymous structs
if sf.Anonymous {
t := sf.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Struct {
for _, child := range typeFields(t, allowAddr) {
child.index = append([]int{i}, child.index...)
child.typeFeature |= tagFeatures
fields = append(fields, child)
}
continue
}
}
// FIXME: Hide hidden fields
/*
finfo := structFieldInfo(typ, &sf)
if finfo.Hidden() {
continue
}
*/
// FIXME: Avoid infinite recursion
}
// Make sure blocks are put at the end of the field list
sort.Stable(fields)
return fields
}
func typeByIndex(t reflect.Type, index []int) reflect.Type {
for _, i := range index {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
t = t.Field(i).Type
}
return t
}
func fieldByIndex(v reflect.Value, index []int) reflect.Value {
for _, i := range index {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return reflect.Value{}
}
v = v.Elem()
}
v = v.Field(i)
}
return v
}
/*
// Anonymous fields to explore at the current level and the next.
current := []fieldInfo{}
next := []fieldInfo{{typ: t}}
var (
//count = map[reflect.Type]int{}
visited = map[reflect.Type]struct{}{}
)
func typeFields(typ reflect.Type) []field {
var fi []fieldInfo
for len(current) > 0 {
for _, f := range current {
if _, ok := visited[f.typ]; ok {
continue
}
visited[f.typ] = struct{}{}
if f.
fi = structFields(f.typ, f.index, fi)
}
current, next = next, current[:0]
}
return nil
}
*/
/*
type fieldInfo struct {
field
typ reflect.Type
anonymous bool
unexported bool
tag fieldTag
target reflect.Type
}
func structFieldInfo(container reflect.Type, sf *reflect.StructField) fieldInfo {
t := sf.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
omit, name, opts := parseTag(sf.Tag.Get("astconf"))
if name == "" {
name = sf.Name
}
if ft.Implements(sectionNamerType) {
if se.section == nil || len(se.section) >= len(f.index) {
se.section = f.index
}
}
mode := typeMode(t)
if opts.Contains("object") && mode != modeBlock {
mode = modeObject
}
return fieldInfo{
field: field{
name: name,
index: sf.Index,
mode: mode,
},
anonymous: sf.Anonymous,
unexported: sf.PkgPath != "",
omit: omit,
options: opts,
target: t,
}
}
func (f *fieldInfo) Hidden() bool {
if f.anonymous {
if f.unexported && f.target.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct types.
return true
}
} else if f.unexported {
// Ignore unexported non-embedded fields.
return true
}
if f.omit {
return true
}
return false
}
*/