-
Notifications
You must be signed in to change notification settings - Fork 11
/
element.go
288 lines (267 loc) · 8.06 KB
/
element.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package xmlstruct
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"slices"
"strings"
)
// An element describes an observed XML element, its attributes, chardata, and
// children.
type element struct {
attrValues map[xml.Name]*value
charDataValue value
childElements map[xml.Name]*element
nestedCount int
childOrder map[xml.Name]int
name xml.Name
optionalChildren map[xml.Name]struct{}
repeatedChildren map[xml.Name]struct{}
root bool
}
// newElement returns a new element.
func newElement(name xml.Name) *element {
return &element{
name: name,
attrValues: make(map[xml.Name]*value),
childElements: make(map[xml.Name]*element),
childOrder: make(map[xml.Name]int),
optionalChildren: make(map[xml.Name]struct{}),
repeatedChildren: make(map[xml.Name]struct{}),
}
}
// observeAttrs updates e's observed attributes with attrs.
func (e *element) observeAttrs(attrs []xml.Attr, options *observeOptions) {
attrCounts := make(map[xml.Name]int)
for _, attr := range attrs {
attrName := options.nameFunc(attr.Name)
if attrName == (xml.Name{}) {
continue
}
attrCounts[attrName]++
attrValue, ok := e.attrValues[attrName]
if !ok {
attrValue = &value{
name: attrName,
}
e.attrValues[attrName] = attrValue
}
attrValue.observe(attr.Value, options)
}
for attrName, count := range attrCounts {
if count > 1 {
e.attrValues[attrName].repeated = true
}
}
for attrName, attrValue := range e.attrValues {
if attrCounts[attrName] == 0 {
attrValue.optional = true
}
}
}
// observeChildElement updates e's observed chardata and child elements with
// tokens read from decoder.
func (e *element) observeChildElement(decoder *xml.Decoder, startElement xml.StartElement, depth int, options *observeOptions) error {
if options.topLevelAttributes || depth != 0 {
e.observeAttrs(startElement.Attr, options)
}
childCounts := make(map[xml.Name]int)
FOR:
for {
var token xml.Token
var err error
if options.useRawToken {
token, err = decoder.RawToken()
} else {
token, err = decoder.Token()
}
if err != nil {
return err
}
switch token := token.(type) {
case xml.StartElement:
childName := options.nameFunc(token.Name)
if childName == (xml.Name{}) {
break
}
childCounts[childName]++
childElement, ok := e.childElements[childName]
if !ok {
if options.topLevelElements != nil {
if topLevelElement, ok := options.topLevelElements[childName]; ok {
childElement = topLevelElement
} else {
topLevelElement = newElement(childName)
options.topLevelElements[childName] = topLevelElement
childElement = topLevelElement
}
if _, ok := options.typeOrder[childName]; !ok {
options.typeOrder[childName] = options.getOrder()
}
} else {
childElement = newElement(childName)
}
e.childElements[childName] = childElement
}
if childElement == e {
e.nestedCount++
}
if _, ok := e.childOrder[childName]; !ok {
e.childOrder[childName] = options.getOrder()
}
if err := childElement.observeChildElement(decoder, token, depth+1, options); err != nil {
return err
}
case xml.EndElement:
break FOR
case xml.CharData:
if trimmedToken := bytes.TrimSpace(token); len(trimmedToken) > 0 {
e.charDataValue.observe(string(token), options)
}
}
}
for childName, count := range childCounts {
if count > 1 {
e.repeatedChildren[childName] = struct{}{}
}
}
for childName := range e.childElements {
if childCounts[childName] == 0 {
e.optionalChildren[childName] = struct{}{}
}
}
return nil
}
// writeGoType writes e's Go type to w.
func (e *element) writeGoType(w io.Writer, options *generateOptions, indentPrefix string) error {
if options.compactTypes && e.isContainer() {
for _, v := range e.childElements {
if v == e {
fmt.Fprintf(w, "%s", e.charDataValue.goType(options))
return nil
}
}
}
if len(e.attrValues) == 0 && len(e.childElements) == 0 && (!e.root || !options.namedRoot) {
fmt.Fprintf(w, "%s", e.charDataValue.goType(options))
return nil
}
fmt.Fprintf(w, "struct {\n")
fieldNames := make(map[string]struct{})
attrValuesByExportedName := make(map[string]*value, len(e.attrValues))
for attrName, attrValue := range e.attrValues {
exportedAttrName := options.exportNameFunc(attrName) + options.attrNameSuffix
if _, ok := fieldNames[exportedAttrName]; ok {
return fmt.Errorf("%s: duplicate field name", exportedAttrName)
}
fieldNames[exportedAttrName] = struct{}{}
attrValuesByExportedName[exportedAttrName] = attrValue
}
if e.root && options.namedRoot {
fmt.Fprintf(w, "%s\tXMLName xml.Name `xml:\"%s\"`\n", indentPrefix, e.name.Local)
}
for _, exportedAttrName := range sortedKeys(attrValuesByExportedName) {
attrValue := attrValuesByExportedName[exportedAttrName]
fmt.Fprintf(w, "%s\t%s %s `xml:\"%s,attr\"`\n", indentPrefix, exportedAttrName, attrValue.goType(options), attrValue.name.Local)
}
if e.charDataValue.observations > 0 {
fieldName := options.charDataFieldName
if _, ok := fieldNames[fieldName]; ok {
return fmt.Errorf("%s: duplicate field name", fieldName)
}
fieldNames[fieldName] = struct{}{}
fmt.Fprintf(w, "%s\t%s string `xml:\",chardata\"`\n", indentPrefix, fieldName)
}
childElements := mapValues(e.childElements)
if options.preserveOrder {
slices.SortFunc(childElements, func(a, b *element) int {
return e.childOrder[a.name] - e.childOrder[b.name]
})
} else {
slices.SortFunc(childElements, func(a, b *element) int {
aExportedName := exportedNameWithoutSuffix(a, options)
bExportedName := exportedNameWithoutSuffix(b, options)
switch {
case aExportedName < bExportedName:
return -1
case aExportedName == bExportedName:
return 0
default:
return 1
}
})
}
for _, childElement := range childElements {
exportedChildName := exportedName(childElement, options)
if _, ok := fieldNames[exportedChildName]; ok {
fieldNames[exportedChildName] = struct{}{}
}
fieldNames[exportedChildName] = struct{}{}
fmt.Fprintf(w, "%s\t%s ", indentPrefix, exportedChildName)
if _, repeated := e.repeatedChildren[childElement.name]; repeated {
fmt.Fprintf(w, "[]")
} else if options.usePointersForOptionalFields {
if _, optional := e.optionalChildren[childElement.name]; optional {
fmt.Fprintf(w, "*")
}
}
currentChild := childElement
if options.compactTypes {
currentChild = firstNotContainerElement(childElement)
}
if topLevelElement, ok := options.namedTypes[currentChild.name]; ok {
fmt.Fprintf(w, "%s", options.exportTypeNameFunc(topLevelElement.name))
} else if _, ok := options.simpleTypes[currentChild.name]; ok {
fmt.Fprintf(w, "%s", currentChild.charDataValue.goType(options))
} else {
if err := currentChild.writeGoType(w, options, indentPrefix+"\t"); err != nil {
return err
}
}
fmt.Fprintf(w, " `xml:\"%s\"`\n", attrName(childElement, options.compactTypes))
}
fmt.Fprintf(w, "%s}", indentPrefix)
return nil
}
func (e *element) isContainer() bool {
return len(e.childElements) == 1 && len(e.attrValues) == 0 && e.charDataValue.observations == 0
}
func firstNotContainerElement(el *element) *element {
if el.isContainer() {
for _, v := range el.childElements {
if el == v {
return el
}
return firstNotContainerElement(v)
}
}
return el
}
func exportedName(el *element, options *generateOptions) string {
return exportedNameWithoutSuffix(el, options) + options.elemNameSuffix
}
func exportedNameWithoutSuffix(el *element, options *generateOptions) string {
if el.isContainer() && options.compactTypes {
for _, v := range el.childElements {
if el == v {
return options.exportNameFunc(el.name)
}
return exportedNameWithoutSuffix(v, options)
}
}
return options.exportNameFunc(el.name)
}
func attrName(el *element, compactTypes bool) string {
if el.isContainer() && compactTypes {
for _, v := range el.childElements {
if el == v {
s := el.name.Local + ">"
s += strings.Repeat(s, el.nestedCount)
return s[:len(s)-1]
}
return el.name.Local + ">" + attrName(v, true)
}
}
return el.name.Local
}