-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
265 lines (224 loc) · 6.04 KB
/
features.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
package astconf
import (
"encoding"
"reflect"
)
var (
blockMarshalerType = reflect.TypeOf(new(Marshaler)).Elem()
objectMarshalerType = reflect.TypeOf(new(ObjectMarshaler)).Elem()
settingMarshalerType = reflect.TypeOf(new(SettingMarshaler)).Elem()
textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
preambleMarshalerType = reflect.TypeOf(new(PreambleMarshaler)).Elem()
sectionerType = reflect.TypeOf(new(SectionNamer)).Elem()
indenterType = reflect.TypeOf(new(Indenter)).Elem()
)
type typeFeature int
const (
tfBlockMarshaler typeFeature = 1 << iota
tfBlockMarshalerAddr
tfObjectMarshaler
tfObjectMarshalerAddr
tfSettingMarshaler
tfSettingMarshalerAddr
tfTextMarshaler
tfTextMarshalerAddr
tfPreambleMarshaler
tfPreambleMarshalerAddr
tfSectioner
tfSectionerAddr
tfIndenter
tfIndenterAddr
tfMultiValue
tfMultiValueAddr
tfBlock
tfBlockAddr
tfObject
tfObjectAddr
tfOmit
tfOmitEmpty
tfCommaSeparated
tfMarshalerMask = tfBlockMarshaler | tfObjectMarshaler | tfSettingMarshaler | tfTextMarshaler
tfMarshalerAddrMask = tfBlockMarshalerAddr | tfObjectMarshalerAddr | tfSettingMarshalerAddr | tfTextMarshalerAddr
)
func tagFeatures(tag *fieldTag) (features typeFeature) {
if tag.omit {
features |= tfOmit
}
if tag.Contains("omitempty") {
features |= tfOmitEmpty
}
if tag.Contains("object") {
features |= tfObject
}
if tag.Contains("commaseparated") {
features |= tfCommaSeparated
}
return features
}
func typeFeatures(t reflect.Type) (features typeFeature) {
// Marshaler
switch {
case typeImplements(t, blockMarshalerType):
features |= tfBlockMarshaler
features |= tfBlock
case typeAddrImplements(t, blockMarshalerType):
features |= tfBlockMarshalerAddr
features |= tfBlockAddr
}
// ObjectMarshaler
switch {
case typeImplements(t, objectMarshalerType):
features |= tfObjectMarshaler
features |= tfObject
case typeAddrImplements(t, objectMarshalerType):
features |= tfObjectMarshalerAddr
features |= tfObjectAddr
}
// SettingMarshaler
switch {
case typeImplements(t, settingMarshalerType):
features |= tfSettingMarshaler
case typeAddrImplements(t, settingMarshalerType):
features |= tfSettingMarshalerAddr
}
// TextMarshaler
switch {
case typeImplements(t, textMarshalerType):
features |= tfTextMarshaler
case typeAddrImplements(t, textMarshalerType):
features |= tfTextMarshalerAddr
}
// PreambleMarshaler
switch {
case typeImplements(t, preambleMarshalerType):
features |= tfPreambleMarshaler
case typeAddrImplements(t, preambleMarshalerType):
features |= tfPreambleMarshalerAddr
}
// Sectioner
switch {
case typeImplements(t, sectionerType):
features |= tfSectioner
case typeAddrImplements(t, sectionerType):
features |= tfSectionerAddr
}
// Indenter
switch {
case typeImplements(t, indenterType):
features |= tfIndenter
case typeAddrImplements(t, indenterType):
features |= tfIndenterAddr
}
// MultiValue
switch t.Kind() {
case reflect.Slice:
// Slices of non-blocks get written out as multiple values with the
// same field name, unless they perform their own marshaling or have
// the commaseparated tag.
elemFeatures := typeFeatures(t.Elem())
if !features.Marshaler() && !elemFeatures.Block() {
features |= tfMultiValue
}
if !features.Marshaler() && !features.MarshalerAddr() && !elemFeatures.Block() && !elemFeatures.BlockAddr() {
features |= tfMultiValueAddr
}
}
// Block
switch t.Kind() {
case reflect.Struct, reflect.Slice:
// Structs and slices are considered blocks unless they perform
// their own marshaling.
if !features.Marshaler() {
features |= tfBlock
}
if !features.Marshaler() && !features.MarshalerAddr() {
features |= tfBlockAddr
}
case reflect.Ptr:
// Pointers are considered blocks if their elements are, unless
// they perform their own marshaling.
elemFeatures := typeFeatures(t.Elem())
if !features.Marshaler() && !elemFeatures.Block() {
features |= tfBlock
}
if !features.Marshaler() && !features.MarshalerAddr() && !elemFeatures.Block() && !elemFeatures.BlockAddr() {
features |= tfBlockAddr
}
}
return features
}
func (tf typeFeature) BlockMarshaler() bool {
return tf&tfBlockMarshaler != 0
}
func (tf typeFeature) BlockMarshalerAddr() bool {
return tf&tfBlockMarshalerAddr != 0
}
func (tf typeFeature) ObjectMarshaler() bool {
return tf&tfObjectMarshaler != 0
}
func (tf typeFeature) ObjectMarshalerAddr() bool {
return tf&tfObjectMarshalerAddr != 0
}
func (tf typeFeature) SettingMarshaler() bool {
return tf&tfSettingMarshaler != 0
}
func (tf typeFeature) SettingMarshalerAddr() bool {
return tf&tfSettingMarshalerAddr != 0
}
func (tf typeFeature) TextMarshaler() bool {
return tf&tfTextMarshaler != 0
}
func (tf typeFeature) TextMarshalerAddr() bool {
return tf&tfTextMarshalerAddr != 0
}
func (tf typeFeature) PreambleMarshaler() bool {
return tf&tfPreambleMarshaler != 0
}
func (tf typeFeature) PreambleMarshalerAddr() bool {
return tf&tfPreambleMarshalerAddr != 0
}
func (tf typeFeature) Sectioner() bool {
return tf&tfSectioner != 0
}
func (tf typeFeature) SectionerAddr() bool {
return tf&tfSectionerAddr != 0
}
func (tf typeFeature) Indenter() bool {
return tf&tfIndenter != 0
}
func (tf typeFeature) IndenterAddr() bool {
return tf&tfIndenterAddr != 0
}
func (tf typeFeature) MultiValue() bool {
return tf&tfMultiValue != 0
}
func (tf typeFeature) MultiValueAddr() bool {
return tf&tfMultiValueAddr != 0
}
func (tf typeFeature) Block() bool {
return tf&tfBlock != 0
}
func (tf typeFeature) BlockAddr() bool {
return tf&tfBlockAddr != 0
}
func (tf typeFeature) Object() bool {
return tf&tfObject != 0
}
func (tf typeFeature) ObjectAddr() bool {
return tf&tfObjectAddr != 0
}
func (tf typeFeature) Omit() bool {
return tf&tfOmit != 0
}
func (tf typeFeature) OmitEmtpy() bool {
return tf&tfOmitEmpty != 0
}
func (tf typeFeature) CommaSeparated() bool {
return tf&tfCommaSeparated != 0
}
func (tf typeFeature) Marshaler() bool {
return tf&tfMarshalerMask != 0
}
func (tf typeFeature) MarshalerAddr() bool {
return tf&tfMarshalerAddrMask != 0
}