Skip to content

Commit c91bf3a

Browse files
committed
Generates struct types with public fields.
1 parent c8367c6 commit c91bf3a

File tree

4 files changed

+138
-52
lines changed

4 files changed

+138
-52
lines changed

cli.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,11 @@ func main() {
8484

8585
source, err := format.Source(data.Bytes())
8686
if err != nil {
87+
fd.Write(data.Bytes())
8788
log.Fatalln(err)
8889
}
8990

9091
fd.Write(source)
9192

92-
// fd.Write(gocode["types"])
93-
// fd.Write(gocode["messages"])
94-
// fd.Write(gocode["operations"])
95-
// fd.Write(gocode["proxy"])
96-
9793
log.Println("Done 💩")
9894
}

gowsdl.go

+89-34
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"log"
11+
"net"
1112
"net/http"
1213
"net/url"
1314
"os"
1415
"path/filepath"
1516
"strings"
1617
"sync"
1718
"text/template"
19+
"time"
20+
"unicode"
1821
)
1922

2023
const maxRecursion uint8 = 5
@@ -35,11 +38,18 @@ func init() {
3538
}
3639
}
3740

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+
3847
func downloadFile(url string) ([]byte, error) {
3948
tr := &http.Transport{
4049
TLSClientConfig: &tls.Config{
4150
InsecureSkipVerify: true,
4251
},
52+
Dial: dialTimeout,
4353
}
4454
client := &http.Client{Transport: tr}
4555

@@ -220,42 +230,20 @@ func (g *GoWsdl) resolveXsdExternals(schema *XsdSchema, url *url.URL) error {
220230
}
221231

222232
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-
244233
funcMap := template.FuncMap{
245-
"toGoType": toGoType,
246-
//"TagDelimiter": TagDelimiter,
234+
"toGoType": toGoType,
235+
"stripns": stripns,
236+
"replaceReservedWords": replaceReservedWords,
237+
"makeFieldPublic": makeFieldPublic,
247238
}
248239

249240
data := new(bytes.Buffer)
250241
tmpl := template.Must(template.New("types").Funcs(funcMap).Parse(typesTmpl))
251242
err := tmpl.Execute(data, g.wsdl.Types)
252243
if err != nil {
253-
log.Fatalln(err)
244+
return nil, err
254245
}
255246

256-
//log.Printf("Abstract data types: %d\n", totalAdts)
257-
//log.Printf("Total schemas: %#d\n\n", len(g.wsdl.Types.Schemas))
258-
259247
return data.Bytes(), nil
260248
}
261249

@@ -270,25 +258,92 @@ func (g *GoWsdl) genOperations() ([]byte, error) {
270258
return nil, nil
271259
}
272260

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+
273297
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{}",
280310
}
281311

282312
func toGoType(xsdType string) string {
283313
//Handles name space, ie. xsd:string, xs:string
284314
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, ":")
285334
type_ := r[0]
286335

287336
if len(r) == 2 {
288337
type_ = r[1]
289338
}
290339

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)
292347
}
293348

294349
func (g *GoWsdl) genMessages() ([]byte, error) {

types_tmpl.go

+45-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,59 @@
11
package main
22

33
var typesTmpl = `
4+
{{define "SimpleType"}}
5+
{{$name := .Name}}
6+
type {{.Name}} {{toGoType .Restriction.Base}}
7+
const (
8+
{{with .Restriction}}
9+
{{range .Enumeration}}
10+
{{replaceReservedWords .Value | makeFieldPublic}} {{$name}} = "{{.Value}}" {{end}}
11+
{{end}}
12+
)
13+
{{end}}
14+
15+
{{define "ComplexType"}}
16+
{{$name := replaceReservedWords .Name}}
17+
18+
type {{.Name}} struct {
19+
{{if ne .ComplexContent.Extension.Base ""}}
20+
{{$baseType := .ComplexContent.Extension.Base}}
21+
{{ if $baseType }}
22+
*{{stripns $baseType}}
23+
{{end}}
24+
25+
{{template "Elements" .ComplexContent.Extension.Sequence.Elements}}
26+
{{ else }}
27+
{{template "Elements" .Sequence.Elements}}
28+
{{end}}
29+
}
30+
{{end}}
31+
32+
{{define "Elements"}}
33+
{{range .}}
34+
{{replaceReservedWords .Name | makeFieldPublic}} {{if eq .MaxOccurs "unbounded"}}[]{{end}}{{.Type | toGoType}}{{end}}
35+
{{end}}
36+
437
package main
538
import (
639
"encoding/xml"
7-
"github.com/c4milo/gowsdl"
40+
//"time"
41+
{{/*range .Imports*/}}
42+
{{/*.*/}}
43+
{{/*end*/}}
844
)
45+
946
{{range .Schemas}}
1047
{{range .SimpleType}}
11-
{{$name := .Name}}
12-
type {{.Name}} {{toGoType .Restriction.Base}}
13-
const (
14-
{{with .Restriction}}
15-
{{range .Enumeration}}
16-
{{.Value}} {{$name}} = "{{.Value}}" {{end}}
17-
{{end}}
18-
)
48+
{{template "SimpleType" .}}
1949
{{end}}
2050
{{range .Elements}}
51+
{{if not .Type}}
52+
53+
{{end}}
2154
{{end}}
2255
{{range .ComplexTypes}}
56+
{{template "ComplexType" .}}
2357
{{end}}
24-
{{end}}`
58+
{{end}}
59+
`

xsd.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type XsdComplexType struct {
4545
Abstract bool `xml:"abstract,attr"`
4646
Name string `xml:"name,attr"`
4747
Mixed bool `xml:"mixed,attr"`
48-
Sequence []XsdElement `xml:"sequence>element"`
48+
Sequence XsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
4949
Choice []XsdElement `xml:"choice>element"`
5050
All []XsdElement `xml:"all>element"`
5151
ComplexContent XsdComplexContent `xml:"http://www.w3.org/2001/XMLSchema complexContent"`
@@ -55,7 +55,7 @@ type XsdComplexType struct {
5555
type XsdGroup struct {
5656
Name string `xml:"name, attr"`
5757
Ref string `xml:"ref,attr"`
58-
Sequence []XsdElement `xml:"http://www.w3.org/2001/XMLSchema sequence"`
58+
Sequence XsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
5959
Choice []XsdElement `xml:"http://www.w3.org/2001/XMLSchema choice"`
6060
All []XsdElement `xml:"http://www.w3.org/2001/XMLSchema all"`
6161
}
@@ -73,7 +73,7 @@ type XsdSimpleContent struct {
7373
type XsdExtension struct {
7474
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema extension"`
7575
Base string `xml:"base,attr"`
76-
Sequence XsdSequence `xml:"http://www.w3.org/2001/XMLSchema extension sequence"`
76+
Sequence XsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
7777
}
7878

7979
type XsdSimpleType struct {

0 commit comments

Comments
 (0)