-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.go
More file actions
71 lines (63 loc) · 1.79 KB
/
attribute.go
File metadata and controls
71 lines (63 loc) · 1.79 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
package goxc
import (
"encoding/xml"
"strings"
)
type Attribute struct {
Class
XMLName xml.Name `xml:"attribute"`
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
Use string `xml:"use,attr"`
Default string `xml:"default,attr"`
Annotation *Annotation `xml:"annotation,omitempty"`
SimpleType *SimpleType `xml:"simpleType,omitempty"`
PackageName string
Parent, OmitEmpty string
TypeName string
Imports []*Import
}
func (a *Attribute) Generate(targetPrefix string, namespaces map[string]string) {
a.TypeName = Upper(a.Name)
if a.hasSimpleType() {
a.prepareSimpleType()
a.SimpleType.Generate(targetPrefix, namespaces)
a.finish()
} else {
a.prepareMe(targetPrefix, namespaces)
}
a.Version = version
a.Rev = rev
generateStruct(a, "templates/attribute.tmpl", a.PackageName, a.TypeName, "attribute")
}
func (a *Attribute) hasParent() bool {
return a.Parent != ""
}
func (a *Attribute) hasSimpleType() bool {
return a.SimpleType != nil
}
func (a *Attribute) finish() {
if a.PackageName != a.SimpleType.PackageName {
a.Imports = a.SimpleType.Imports
}
a.Type = a.SimpleType.Type
if a.hasParent() {
a.Type = a.SimpleType.Name
}
}
func (a *Attribute) prepareMe(targetPrefix string, namespaces map[string]string) {
a.Type = Replace(targetPrefix, a.Type, namespaces)
if strings.Contains(a.Type, ".") && !strings.Contains(a.Type, targetPrefix) {
a.Imports = Append(a.Imports, a.Type, namespaces)
}
if a.Use == "optional" {
a.OmitEmpty = ",omitempty"
}
}
func (a *Attribute) prepareSimpleType() {
a.SimpleType.PackageName = a.PackageName
a.SimpleType.Parent = a.Parent + a.TypeName
if a.SimpleType.Name == "" {
a.SimpleType.Name = a.Parent + a.TypeName
}
}