forked from hooklift/gowsdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmpl_funcs.go
281 lines (250 loc) · 6.45 KB
/
tmpl_funcs.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
package gowsdl
import (
"errors"
"log"
"strings"
"text/template"
"unicode"
)
type tmplFunctions struct {
funcMap template.FuncMap
}
var reservedWords = map[string]string{
"break": "break_",
"default": "default_",
"func": "func_",
"interface": "interface_",
"select": "select_",
"case": "case_",
"defer": "defer_",
"go": "go_",
"map": "map_",
"struct": "struct_",
"chan": "chan_",
"else": "else_",
"goto": "goto_",
"package": "package_",
"switch": "switch_",
"const": "const_",
"fallthrough": "fallthrough_",
"if": "if_",
"range": "range_",
"type": "type_",
"continue": "continue_",
"for": "for_",
"import": "import_",
"return": "return_",
"var": "var_",
}
var xsd2GoTypes = map[string]string{
"string": "string",
"token": "string",
"float": "float32",
"double": "float64",
"decimal": "float64",
"integer": "int32",
"int": "int32",
"short": "int16",
"byte": "int8",
"long": "int64",
"boolean": "bool",
"datetime": "time.Time",
"date": "time.Time",
"time": "time.Time",
"base64binary": "[]byte",
"hexbinary": "[]byte",
"unsignedint": "uint32",
"unsignedshort": "uint16",
"unsignedbyte": "byte",
"unsignedlong": "uint64",
"anytype": "interface{}",
}
func createTmplFunctions(g *GoWSDL) *tmplFunctions {
// Normalizes value to be used as a valid Go identifier, avoiding compilation issues
normalize := func(value string) string {
mapping := func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
return r
}
return -1
}
return strings.Map(mapping, value)
}
replaceReservedWords := func(identifier string) string {
value := reservedWords[identifier]
if value != "" {
return value
}
return normalize(identifier)
}
removeNS := func(xsdType string) string {
// Handles name space, ie. xsd:string, xs:string
r := strings.Split(xsdType, ":")
if len(r) == 2 {
return r[1]
}
return r[0]
}
toGoTypeNs := func(xsdType string, ns string) string {
log.Printf("xsdType: %s, ns: %s", xsdType, ns)
// Handles name space, ie. xsd:string, xs:string
r := strings.Split(xsdType, ":")
t := r[0]
if len(r) == 2 {
t = r[1]
}
value := xsd2GoTypes[strings.ToLower(t)]
if value != "" {
return value
}
if !g.ignoreTypeNs && ns != "" {
t = ns + t
}
return "*" + replaceReservedWords(makePublic(t))
}
toGoType := func(xsdType string) string {
return toGoTypeNs(xsdType, "")
}
// TODO(c4milo): Add namespace support instead of stripping it
stripns := func(xsdType string) string {
r := strings.Split(xsdType, ":")
t := r[0]
if len(r) == 2 {
t = r[1]
}
return t
}
makePublic := func(identifier string) string {
if !g.exportAllTypes {
return identifier
}
return makePublic(identifier)
}
comment := func(text string) string {
lines := strings.Split(text, "\n")
var output string
if len(lines) == 1 && lines[0] == "" {
return ""
}
// Helps to determine if there is an actual comment without screwing newlines
// in real comments.
hasComment := false
for _, line := range lines {
line = strings.TrimLeftFunc(line, unicode.IsSpace)
if line != "" {
hasComment = true
}
output += "\n// " + line
}
if hasComment {
return output
}
return ""
}
// Given a message, finds its type.
//
// I'm not very proud of this function but
// it works for now and performance doesn't
// seem critical at this point
findType := func(message string) string {
message = stripns(message)
for _, msg := range g.wsdl.Messages {
if msg.Name != message {
continue
}
// Assumes document/literal wrapped WS-I
if len(msg.Parts) == 0 {
// Message does not have parts. This could be a Port
// with HTTP binding or SOAP 1.2 binding, which are not currently
// supported.
log.Printf("[WARN] %s message doesn't have any parts, ignoring message...", msg.Name)
continue
}
part := msg.Parts[0]
if part.Type != "" {
return stripns(part.Type)
}
elRef := stripns(part.Element)
for _, schema := range g.wsdl.Types.Schemas {
for _, el := range schema.Elements {
if strings.EqualFold(elRef, el.Name) {
if el.Type != "" {
return stripns(el.Type)
}
return el.Name
}
}
}
}
return ""
}
// TODO(c4milo): Add support for namespaces instead of striping them out
// TODO(c4milo): improve runtime complexity if performance turns out to be an issue.
findSOAPAction := func(operation, portType string) string {
for _, binding := range g.wsdl.Binding {
if stripns(binding.Type) != portType {
continue
}
for _, soapOp := range binding.Operations {
if soapOp.Name == operation {
return soapOp.SOAPOperation.SOAPAction
}
}
}
return ""
}
findServiceAddress := func(name string) string {
for _, service := range g.wsdl.Service {
for _, port := range service.Ports {
if port.Name == name {
return port.SOAPAddress.Location
}
}
}
return ""
}
return &tmplFunctions{
funcMap: map[string]interface{}{
"normalize": normalize,
"replaceReservedWords": replaceReservedWords,
"removeNS": removeNS,
"toGoTypeNs": toGoTypeNs,
"toGoType": toGoType,
"stripns": stripns,
"comment": comment,
"makePublic": makePublic,
"makeFieldPublic": makePublic,
"goString": goString,
"dict": dict,
"findType": findType,
"findSOAPAction": findSOAPAction,
"findServiceAddress": findServiceAddress,
},
}
}
func goString(s string) string {
return strings.Replace(s, "\"", "\\\"", -1)
}
func dict(values ...interface{}) (map[string]interface{}, error) {
valuesCount := len(values)
if valuesCount%2 != 0 {
return nil, errors.New("[ERROR] Odd number of arguments, even expected")
}
resultDict := make(map[string]interface{}, valuesCount/2)
for i := 0; i < valuesCount; i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("[ERROR] dict keys must be strings")
}
resultDict[key] = values[i+1]
}
return resultDict, nil
}
func makePublic(identifier string) string {
field := []rune(identifier)
if len(field) == 0 {
return identifier
}
field[0] = unicode.ToUpper(field[0])
return string(field)
}