forked from GoComply/xsd2go
/
types.go
299 lines (258 loc) · 7.58 KB
/
types.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
289
290
291
292
293
294
295
296
297
298
299
package xsd
import (
"encoding/xml"
"github.com/iancoleman/strcase"
)
type Type interface {
GoName() string
GoTypeName() string
Schema() *Schema
Attributes() []Attribute
Elements() []Element
ContainsText() bool
compile(*Schema, *Element)
}
func injectSchemaIntoAttributes(schema *Schema, intermAttributes []Attribute) []Attribute {
attributesWithProperScema := make([]Attribute, len(intermAttributes))
for idx, attribute := range intermAttributes {
attribute.schema = schema
attributesWithProperScema[idx] = attribute
}
return attributesWithProperScema
}
func setXmlNameAnyForSingleElements(elements []Element) []Element {
if len(elements) == 1 {
result := make([]Element, 1)
element := elements[0]
element.XmlNameOverride = ",any"
result[0] = element
return result
} else {
for idx := range elements {
element := &elements[idx]
element.XmlNameOverride = ""
}
}
return elements
}
type ComplexType struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema complexType"`
Name string `xml:"name,attr"`
Mixed bool `xml:"mixed,attr"`
AttributesDirect []Attribute `xml:"attribute"`
Sequence *Sequence `xml:"sequence"`
SequenceAll *SequenceAll `xml:"all"`
schema *Schema `xml:"-"`
SimpleContent *SimpleContent `xml:"simpleContent"`
ComplexContent *ComplexContent `xml:"complexContent"`
Choice *Choice `xml:"choice"`
content GenericContent `xml:"-"`
}
func (ct *ComplexType) Attributes() []Attribute {
if ct.content != nil {
return ct.content.Attributes()
}
return ct.AttributesDirect
}
func (ct *ComplexType) HasXmlNameAttribute() bool {
for _, attribute := range ct.Attributes() {
if attribute.GoName() == "XMLName" {
return true
}
}
return false
}
func (ct *ComplexType) Elements() []Element {
if ct.Sequence != nil {
return setXmlNameAnyForSingleElements(ct.Sequence.Elements())
} else if ct.SequenceAll != nil {
return setXmlNameAnyForSingleElements(ct.SequenceAll.Elements())
} else if ct.content != nil {
return setXmlNameAnyForSingleElements(ct.content.Elements())
} else if ct.Choice != nil {
return ct.Choice.Elements()
}
return []Element{}
}
func (ct *ComplexType) GoName() string {
return strcase.ToCamel(ct.Name)
}
func (ct *ComplexType) GoTypeName() string {
return ct.GoName()
}
func (ct *ComplexType) ContainsInnerXml() bool {
return ct.Mixed
}
func (ct *ComplexType) ContainsText() bool {
return ct.content != nil && ct.content.ContainsText()
}
func (ct *ComplexType) Schema() *Schema {
return ct.schema
}
func (ct *ComplexType) compile(sch *Schema, parentElement *Element) {
ct.schema = sch
if ct.Sequence != nil {
ct.Sequence.compile(sch, parentElement)
}
if ct.SequenceAll != nil {
if ct.Sequence != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:sequence and xsd:all")
}
ct.SequenceAll.compile(sch, parentElement)
}
// Handle improbable name clash. Consider XSD defining two attributes on the element:
// "id" and "Id", this would create name clash given the camelization we do.
goNames := map[string]uint{}
for idx := range ct.Attributes() {
attribute := &ct.Attributes()[idx]
attribute.compile(sch)
count := goNames[attribute.GoName()]
count += 1
goNames[attribute.GoName()] = count
attribute.DuplicateCount = count
// Second GoName may be different depending on the DuplicateCount
goNames[attribute.GoName()] = count
}
if ct.ComplexContent != nil {
ct.content = ct.ComplexContent
if ct.SimpleContent != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:simpleContent and xsd:complexContent together")
}
} else if ct.SimpleContent != nil {
ct.content = ct.SimpleContent
}
if ct.content != nil {
if len(ct.AttributesDirect) > 1 {
panic("Not implemented: xsd:complexType " + ct.Name + " defines direct attribute and xsd:content")
}
if ct.Sequence != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:sequence and xsd:content")
}
if ct.SequenceAll != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:all and xsd:content")
}
ct.content.compile(sch, parentElement)
}
if ct.Choice != nil {
if ct.content != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:choice and xsd:content")
}
if ct.Sequence != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:choice and xsd:sequence")
}
if ct.SequenceAll != nil {
panic("Not implemented: xsd:complexType " + ct.Name + " defines xsd:all and xsd:sequence")
}
ct.Choice.compile(sch, parentElement)
}
}
type SimpleType struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema simpleType"`
Name string `xml:"name,attr"`
Restriction *Restriction `xml:"restriction"`
schema *Schema `xml:"-"`
}
func (st *SimpleType) GoName() string {
return strcase.ToCamel(st.Name)
}
func (st *SimpleType) GoTypeName() string {
if st.Restriction != nil && st.Restriction.typ != nil {
return st.Restriction.typ.GoTypeName()
}
return "string"
}
func (st *SimpleType) Schema() *Schema {
return st.schema
}
func (st *SimpleType) compile(sch *Schema, parentElement *Element) {
if st.schema == nil {
st.schema = sch
}
if st.Restriction != nil {
st.Restriction.compile(sch, parentElement)
}
}
func (st *SimpleType) Attributes() []Attribute {
return []Attribute{}
}
func (st *SimpleType) Elements() []Element {
return []Element{}
}
func (st *SimpleType) Enums() []Enumeration {
if st.Restriction != nil {
return st.Restriction.Enums()
}
return []Enumeration{}
}
func (st *SimpleType) ContainsText() bool {
return true
}
type staticType string
func (st staticType) GoName() string {
return string(st)
}
func (ct staticType) GoTypeName() string {
return ct.GoName()
}
func (st staticType) Attributes() []Attribute {
return []Attribute{}
}
func (st staticType) Elements() []Element {
return []Element{}
}
func (st staticType) Schema() *Schema {
return nil
}
func (staticType) ContainsText() bool {
return true
}
func (st staticType) compile(*Schema, *Element) {
}
var staticTypes = map[string]staticType{
"string": "string",
"language": "string",
"dateTime": "string",
"date": "string",
"base64Binary": "string",
"normalizedString": "string",
"token": "string",
"NCName": "string",
"NMTOKENS": "string",
"anySimpleType": "string",
"anyType": "string",
"int": "int",
"integer": "int64",
"long": "int64",
"negativeInteger": "int64",
"nonNegativeInteger": "uint64",
"anyURI": "string",
"double": "float64",
"decimal": "float64", // no: http://books.xmlschemata.org/relaxng/ch19-77057.html
"float": "float64",
"boolean": "bool",
"ID": "string",
"IDREF": "string",
"positiveInteger": "uint64",
"unsignedInt": "uint64",
"gYear": "string",
"gYearMonth": "string",
"gMonthDay": "string",
"gDay": "string",
"gMonth": "string",
"time": "string",
"unsignedShort": "uint16",
"unsignedByte": "uint8",
"short": "int16",
"byte": "int8",
}
func StaticType(name string) staticType {
typ, found := staticTypes[name]
if found {
return typ
}
panic("Type xsd:" + name + " not implemented")
}
func IsStaticType(name string) bool {
_, found := staticTypes[name]
return found
}