-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.go
More file actions
87 lines (78 loc) · 2.69 KB
/
element.go
File metadata and controls
87 lines (78 loc) · 2.69 KB
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
package goxc
import (
"encoding/xml"
)
type Element struct {
XMLName xml.Name `xml:"element"`
Name string `xml:"name,attr"`
Abstract bool `xml:"abstract,attr"`
Type string `xml:"type,attr"`
SubstitutionGroup string `xml:"substitutionGroup,attr"`
Ref string `xml:"ref,attr"`
MinOccurs string `xml:"minOccurs,attr"`
MaxOccurs string `xml:"maxOccurs,attr"`
Annotation *Annotation `xml:"annotation,omitempty"`
SimpleType *SimpleType `xml:"simpleType,omitempty"`
ComplexType *ComplexType `xml:"complexType,omitempty"`
PackageName, Namespace string
Base, Parent string
OmitEmpty, Array string
TypeName string
Imports []*Import
}
func (e *Element) Generate(targetPrefix string, namespaces map[string]string) {
e.TypeName = Upper(e.Name)
if e.isOptional() {
e.OmitEmpty = ",omitempty"
}
if e.isUnbounded() {
e.Array = "[]"
}
if e.isSimple() {
e.Base = Replace(targetPrefix, e.SimpleType.Type, namespaces)
if e.SimpleType.isRestriction() {
e.Base = Replace(targetPrefix, e.SimpleType.Restriction.Base, namespaces)
}
e.Imports = append(e.Imports, e.SimpleType.Imports...)
} else if e.isComplex() {
e.ComplexType.Name = e.Parent + e.TypeName
ce := &ComplexElement{PackageName: e.PackageName, Parent: e.Parent + e.Name, ComplexType: e.ComplexType, Array: e.Array}
ce.Generate(targetPrefix, namespaces)
e.Base = e.ComplexType.Name
e.Imports = append(e.Imports, ce.ComplexType.Imports...)
} else if e.isAbstract() {
e.Base = e.Type
a := &Abstract{PackageName: e.PackageName, Name: e.TypeName}
a.Generate(targetPrefix)
} else if e.Name != "" { // && IsBaseType(e.Type) {
e.Base = Replace(targetPrefix, e.Type, namespaces)
e.Imports = Append(e.Imports, e.Base, namespaces)
se := &SimpleElement{PackageName: e.PackageName, Name: e.TypeName, Type: e.Type, Imports: e.Imports}
se.Generate(targetPrefix, namespaces)
} else {
e.Base = Replace(targetPrefix, e.Ref, namespaces)
e.TypeName = Name(e.Base)
//if strings.Contains(e.TypeName, ".") {
// e.TypeName = e.Base[strings.Index(e.Base, ".")+1:]
//}
e.Imports = Append(e.Imports, e.Base, namespaces)
}
}
func (e *Element) isAbstract() bool {
return e.Abstract
}
func (e *Element) isComplex() bool {
return e.ComplexType != nil
}
func (e *Element) isOptional() bool {
return e.MinOccurs == "0"
}
func (e *Element) isUnbounded() bool {
return e.MaxOccurs == "unbounded"
}
func (e *Element) isRef() bool {
return e.Ref != ""
}
func (e *Element) isSimple() bool {
return e.SimpleType != nil
}