-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMessageElement.go
187 lines (168 loc) · 5.18 KB
/
MessageElement.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
package main
import (
"bytes"
"fmt"
"log"
"strings"
"text/template"
)
const (
complexAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Add{{.Element}}() *{{.ElementType}} {
{{.Receiver}}.{{.Element}} = new({{.ElementType}})
return {{.Receiver}}.{{.Element}}
}
`
complexArrayAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Add{{.Element}}() *{{.ElementType}} {
newValue := new ({{.ElementType}})
{{.Receiver}}.{{.Element}} = append({{.Receiver}}.{{.Element}}, newValue)
return newValue
}
`
simpleAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Set{{.Element}}(value string) {
{{.Receiver}}.{{.Element}} = (*{{.ElementType}})(&value)
}
`
simpleArrayAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Add{{.Element}}(value string) {
{{.Receiver}}.{{.Element}} = append({{.Receiver}}.{{.Element}}, (*{{.ElementType}})(&value))
}
`
amountAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Set{{.Element}}(value, currency string) {
{{.Receiver}}.{{.Element}} = New{{.ElementType}}(value, currency)
}
`
amountArrayAccessTemplate = `
func ({{.Receiver}} *{{.ReceiverType}}) Add{{.Element}}(value, currency string) {
{{.Receiver}}.{{.Element}} = append({{.Receiver}}.{{.Element}}, New{{.ElementType}}(value, currency))
}
`
)
var (
complexAccess *template.Template
complexArrayAccess *template.Template
simpleAccess *template.Template
simpleArrayAccess *template.Template
amountAccess *template.Template
amountArrayAccess *template.Template
)
func init() {
complexAccess = prepareTemplate("complexAccess", complexAccessTemplate)
complexArrayAccess = prepareTemplate("complexArrayAccess", complexArrayAccessTemplate)
simpleAccess = prepareTemplate("simpleAccess", simpleAccessTemplate)
simpleArrayAccess = prepareTemplate("simpleArrayAccess", simpleArrayAccessTemplate)
amountAccess = prepareTemplate("amountAccess", amountAccessTemplate)
amountArrayAccess = prepareTemplate("amountArrayAccess", amountArrayAccessTemplate)
}
func prepareTemplate(name, body string) *template.Template {
tmpl, err := template.New(name).Parse(body)
if err != nil {
log.Fatalf("could not compile template %s - %s", name, err.Error())
}
return tmpl
}
type MessageElement struct {
XSIType string `xml:"xsitype,attr"`
XMIId string `xml:"http://www.omg.org/XMI id,attr"`
Definition string `xml:"definition,attr"`
Name string `xml:"name,attr"`
MaxOccurs string `xml:"maxOccurs,attr"`
MinOccurs string `xml:"minOccurs,attr"`
XMLTag string `xml:"xmlTag,attr"`
SimpleType string `xml:"simpleType,attr"`
ComplexType string `xml:"complexType,attr"`
Type string `xml:"type,attr"`
}
func (m *MessageElement) typeID() (typeID string, complex bool) {
if len(m.SimpleType) > 0 {
return m.SimpleType, false
}
if len(m.ComplexType) > 0 {
return m.ComplexType, true
}
if len(m.Type) > 0 {
return m.Type, true
}
panic(fmt.Sprintf("message element with undefined type: %+v", m))
}
func (m *MessageElement) IsArray() bool {
return m.MaxOccurs == "" || (m.MaxOccurs != "0" && m.MaxOccurs != "1")
}
func (m *MessageElement) ArrayDeclaration() string {
if m.IsArray() {
return "[]"
}
return ""
}
func (m *MessageElement) Declaration() string {
return fmt.Sprintf(
"// %s\n\t%s %s*%s `xml:\"%s%s\"`",
strings.Replace(m.Definition, "\n", "\n\t// ", -1),
m.Name, m.ArrayDeclaration(), m.MemberType(), m.XMLTag, m.optional())
}
func (m *MessageElement) DeclarationOut(basePackageName string) string {
return fmt.Sprintf(
"// %s\n\t%s %s*%s.%s `xml:\"%s%s\"`",
strings.Replace(m.Definition, "\n", "\n\t// ", -1),
m.Name, m.ArrayDeclaration(), basePackageName, m.MemberType(), m.XMLTag, m.optional())
}
type context struct {
Receiver string
ReceiverType string
Element string
ElementType string
ElementXSIType string
}
func (m *MessageElement) Access(basePackageName, receiverType string) string {
c := &context{
Receiver: strings.ToLower(receiverType[:1]),
ReceiverType: receiverType,
Element: m.Name,
}
typeID, complex := m.typeID()
t := typeMap[typeID]
c.ElementXSIType = t.XSIType
c.ElementType = t.Name
if basePackageName != "" {
c.ElementType = basePackageName + "." + c.ElementType
}
tmpl := chooseTemplate(complex, m.IsArray(), c.ElementXSIType == "iso20022:Amount")
return m.buildAccess(c, tmpl)
}
func chooseTemplate(complex, array, amount bool) *template.Template {
if complex {
return choose(array, complexArrayAccess, complexAccess)
}
if amount {
return choose(array, amountArrayAccess, amountAccess)
}
return choose(array, simpleArrayAccess, simpleAccess)
}
func choose(array bool, arrayTemplate, singleTemplate *template.Template) *template.Template {
if array {
return arrayTemplate
}
return singleTemplate
}
func (m *MessageElement) buildAccess(c *context, tmpl *template.Template) string {
var buf bytes.Buffer
tmpl.Execute(&buf, c)
return buf.String()
}
func (m *MessageElement) optional() string {
if m.MinOccurs == "0" || m.MinOccurs == "" {
return ",omitempty"
}
return ""
}
func (m *MessageElement) MemberType() string {
typeID, _ := m.typeID()
return typeMap[typeID].Name
}
func (m *MessageElement) Analyse() {
typeID, _ := m.typeID()
typeMap[typeID].Used()
}