@@ -8,13 +8,16 @@ import (
8
8
"fmt"
9
9
"io/ioutil"
10
10
"log"
11
+ "net"
11
12
"net/http"
12
13
"net/url"
13
14
"os"
14
15
"path/filepath"
15
16
"strings"
16
17
"sync"
17
18
"text/template"
19
+ "time"
20
+ "unicode"
18
21
)
19
22
20
23
const maxRecursion uint8 = 5
@@ -35,11 +38,18 @@ func init() {
35
38
}
36
39
}
37
40
41
+ var timeout = time .Duration (30 * time .Second )
42
+
43
+ func dialTimeout (network , addr string ) (net.Conn , error ) {
44
+ return net .DialTimeout (network , addr , timeout )
45
+ }
46
+
38
47
func downloadFile (url string ) ([]byte , error ) {
39
48
tr := & http.Transport {
40
49
TLSClientConfig : & tls.Config {
41
50
InsecureSkipVerify : true ,
42
51
},
52
+ Dial : dialTimeout ,
43
53
}
44
54
client := & http.Client {Transport : tr }
45
55
@@ -220,42 +230,20 @@ func (g *GoWsdl) resolveXsdExternals(schema *XsdSchema, url *url.URL) error {
220
230
}
221
231
222
232
func (g * GoWsdl ) genTypes () ([]byte , error ) {
223
- //totalAdts := 0
224
- // for _, schema := range g.wsdl.Types.Schemas {
225
- // for _, el := range schema.Elements {
226
- // if el.Type == "" {
227
- // // log.Printf("Complex %s -> %#v\n\n", strings.TrimSuffix(el.ComplexType.Name, "Type"), el.ComplexType.Sequence)
228
- // totalAdts++
229
- // } else if el.SimpleType != nil {
230
- // log.Printf("Simple %s -> %#v\n\n", el.SimpleType.Name, el.SimpleType.Retriction)
231
- // }
232
- // }
233
-
234
- // for _ /*complexType*/, _ = range schema.ComplexTypes {
235
- // // log.Printf("Complex %s -> %#v\n\n", strings.TrimSuffix(complexType.Name, "Type"), complexType.Sequence)
236
- // totalAdts++
237
- // }
238
-
239
- // for _, simpleType := range schema.SimpleType {
240
- // log.Printf("Simple %s -> %#v\n\n", simpleType.Name, simpleType.Retriction)
241
- // }
242
- // }
243
-
244
233
funcMap := template.FuncMap {
245
- "toGoType" : toGoType ,
246
- //"TagDelimiter": TagDelimiter,
234
+ "toGoType" : toGoType ,
235
+ "stripns" : stripns ,
236
+ "replaceReservedWords" : replaceReservedWords ,
237
+ "makeFieldPublic" : makeFieldPublic ,
247
238
}
248
239
249
240
data := new (bytes.Buffer )
250
241
tmpl := template .Must (template .New ("types" ).Funcs (funcMap ).Parse (typesTmpl ))
251
242
err := tmpl .Execute (data , g .wsdl .Types )
252
243
if err != nil {
253
- log . Fatalln ( err )
244
+ return nil , err
254
245
}
255
246
256
- //log.Printf("Abstract data types: %d\n", totalAdts)
257
- //log.Printf("Total schemas: %#d\n\n", len(g.wsdl.Types.Schemas))
258
-
259
247
return data .Bytes (), nil
260
248
}
261
249
@@ -270,25 +258,92 @@ func (g *GoWsdl) genOperations() ([]byte, error) {
270
258
return nil , nil
271
259
}
272
260
261
+ var reservedWords = map [string ]string {
262
+ "break" : "break_" ,
263
+ "default" : "default_" ,
264
+ "func" : "func_" ,
265
+ "interface" : "interface_" ,
266
+ "select" : "select_" ,
267
+ "case" : "case_" ,
268
+ "defer" : "defer_" ,
269
+ "go" : "go_" ,
270
+ "map" : "map_" ,
271
+ "struct" : "struct_" ,
272
+ "chan" : "chan_" ,
273
+ "else" : "else_" ,
274
+ "goto" : "goto_" ,
275
+ "package" : "package_" ,
276
+ "switch" : "switch_" ,
277
+ "const" : "const_" ,
278
+ "fallthrough" : "fallthrough_" ,
279
+ "if" : "if_" ,
280
+ "range" : "range_" ,
281
+ "type" : "type_" ,
282
+ "continue" : "continue_" ,
283
+ "for" : "for_" ,
284
+ "import" : "import_" ,
285
+ "return" : "return_" ,
286
+ "var" : "var_" ,
287
+ }
288
+
289
+ func replaceReservedWords (identifier string ) string {
290
+ value := reservedWords [identifier ]
291
+ if value != "" {
292
+ return value
293
+ }
294
+ return identifier
295
+ }
296
+
273
297
var xsd2GoTypes = map [string ]string {
274
- "string" : "string" ,
275
- "decimal" : "float" ,
276
- "integer" : "" ,
277
- "boolean" : "bool" ,
278
- "date" : "" ,
279
- "time" : "" ,
298
+ "string" : "string" ,
299
+ "decimal" : "float64" ,
300
+ "integer" : "int64" ,
301
+ "int" : "int32" ,
302
+ "short" : "int16" ,
303
+ "byte" : "int8" ,
304
+ "long" : "int64" ,
305
+ "boolean" : "bool" ,
306
+ "dateTime" : "time.Time" ,
307
+ "date" : "time.Time" ,
308
+ "time" : "time.Time" ,
309
+ "anyType" : "interface{}" ,
280
310
}
281
311
282
312
func toGoType (xsdType string ) string {
283
313
//Handles name space, ie. xsd:string, xs:string
284
314
r := strings .Split (xsdType , ":" )
315
+
316
+ type_ := r [0 ]
317
+
318
+ if len (r ) == 2 {
319
+ type_ = r [1 ]
320
+ }
321
+
322
+ value := xsd2GoTypes [type_ ]
323
+
324
+ if value != "" {
325
+ return value
326
+ }
327
+
328
+ return "*" + type_
329
+ }
330
+
331
+ //TODO: Add namespace support instead of stripping it
332
+ func stripns (xsdType string ) string {
333
+ r := strings .Split (xsdType , ":" )
285
334
type_ := r [0 ]
286
335
287
336
if len (r ) == 2 {
288
337
type_ = r [1 ]
289
338
}
290
339
291
- return xsd2GoTypes [type_ ]
340
+ return type_
341
+ }
342
+
343
+ func makeFieldPublic (field_ string ) string {
344
+ field := []rune (field_ )
345
+ field [0 ] = unicode .ToUpper (field [0 ])
346
+ return string (field )
292
347
}
293
348
294
349
func (g * GoWsdl ) genMessages () ([]byte , error ) {
0 commit comments