Skip to content

Commit db9784f

Browse files
committed
Still work in progress
- Creates directory for generated package - Adds more XSD elements
1 parent 666d612 commit db9784f

File tree

3 files changed

+155
-70
lines changed

3 files changed

+155
-70
lines changed

cli.go

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
flags "github.com/jessevdk/go-flags"
55
"log"
66
"os"
7-
"sync"
87
)
98

109
const version = "v0.0.1"
1110

1211
var opts struct {
1312
Version bool `short:"v" long:"version" description:"Shows gowsdl version"`
14-
Package string `short:"p" long:"package" description:"Package under which code will be generated" default:"gowsdl"`
15-
OutputFile string `short:"o" long:"output" description:"File where the generated code will be saved" default:"./myservice.go"`
13+
Package string `short:"p" long:"package" description:"Package under which code will be generated" default:"myservice"`
14+
OutputFile string `short:"o" long:"output" description:"File where the generated code will be saved" default:"myservice.go"`
1615
}
1716

1817
func main() {
@@ -42,57 +41,31 @@ func main() {
4241
os.Exit(1)
4342
}
4443

45-
err = gowsdl.Unmarshal()
44+
gocode, err := gowsdl.Start()
4645
if err != nil {
47-
logger.Println(err)
48-
os.Exit(1)
46+
logger.Fatalln(err)
4947
}
5048

51-
var types, operations, proxy []byte
52-
var wg sync.WaitGroup
53-
54-
wg.Add(1)
55-
go func() {
56-
defer wg.Done()
57-
var err error
58-
59-
types, err = gowsdl.GenTypes()
60-
if err != nil {
61-
logger.Fatalln(err)
62-
}
63-
}()
64-
65-
wg.Add(1)
66-
go func() {
67-
defer wg.Done()
68-
var err error
49+
pkg := "./" + opts.Package
50+
err = os.Mkdir(pkg, 0744)
6951

70-
operations, err = gowsdl.GenOperations()
52+
if perr, ok := err.(*os.PathError); ok && os.IsExist(perr.Err) {
53+
logger.Println("Package directory already exist, skipping creation")
54+
} else {
7155
if err != nil {
7256
logger.Fatalln(err)
7357
}
74-
}()
75-
76-
wg.Add(1)
77-
go func() {
78-
defer wg.Done()
79-
var err error
80-
81-
proxy, err = gowsdl.GenSoapProxy()
82-
if err != nil {
83-
logger.Fatalln(err)
84-
}
85-
}()
86-
87-
wg.Wait()
58+
}
8859

89-
fd, err := os.Create(opts.OutputFile)
60+
fd, err := os.Create(pkg + "/" + opts.OutputFile)
9061
if err != nil {
9162
logger.Fatalln(err)
9263
}
9364
defer fd.Close()
9465

95-
fd.Write(types)
96-
fd.Write(proxy)
97-
fd.Write(operations)
66+
fd.Write(gocode["types"])
67+
fd.Write(gocode["proxy"])
68+
fd.Write(gocode["operations"])
69+
70+
logger.Println("Done 💩")
9871
}

gowsdl.go

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"log"
77
"os"
88
"strings"
9+
"sync"
910
)
1011

1112
type GoWsdl struct {
1213
file, pkg string
1314
logger *log.Logger
15+
wsdl *Wsdl
1416
}
1517

1618
func NewGoWsdl(file, pkg string, logger *log.Logger) (*GoWsdl, error) {
@@ -35,34 +37,114 @@ func NewGoWsdl(file, pkg string, logger *log.Logger) (*GoWsdl, error) {
3537
}, nil
3638
}
3739

40+
func (g *GoWsdl) Start() (map[string][]byte, error) {
41+
gocode := make(map[string][]byte)
42+
43+
err := g.Unmarshal()
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
var wg sync.WaitGroup
49+
50+
wg.Add(1)
51+
go func() {
52+
defer wg.Done()
53+
var err error
54+
55+
gocode["types"], err = g.GenTypes()
56+
if err != nil {
57+
g.logger.Println(err)
58+
}
59+
}()
60+
61+
wg.Add(1)
62+
go func() {
63+
defer wg.Done()
64+
var err error
65+
66+
gocode["operations"], err = g.GenOperations()
67+
if err != nil {
68+
g.logger.Println(err)
69+
}
70+
}()
71+
72+
wg.Add(1)
73+
go func() {
74+
defer wg.Done()
75+
var err error
76+
77+
gocode["proxy"], err = g.GenSoapProxy()
78+
if err != nil {
79+
g.logger.Println(err)
80+
}
81+
}()
82+
83+
wg.Wait()
84+
85+
return gocode, nil
86+
}
87+
3888
func (g *GoWsdl) Unmarshal() error {
3989
g.logger.Printf("Using %s...\n", g.file)
4090

41-
//URL or local file?
91+
//g.file is URL or local file?
4292
//if URL, download!
4393

4494
data, err := ioutil.ReadFile(g.file)
4595
if err != nil {
4696
return err
4797
}
4898

49-
wsdl := Wsdl{}
50-
err = xml.Unmarshal(data, &wsdl)
99+
g.wsdl = &Wsdl{}
100+
err = xml.Unmarshal(data, g.wsdl)
51101
if err != nil {
52102
return err
53103
}
54104

105+
//Resolve wsdl imports
106+
//Resolve xsd includes
107+
//Resolve xsd imports
108+
55109
return nil
56110
}
57111

58112
func (g *GoWsdl) GenTypes() ([]byte, error) {
113+
if g.wsdl == nil {
114+
g.logger.Fatalln("You have to unmarshal the WSDL file first")
115+
}
116+
117+
//element > complexType
118+
119+
for _, schema := range g.wsdl.Types.Schemas {
120+
g.logger.Println(schema.XMLName)
121+
// for _, element := range schema.Elements {
122+
// g.logger.Printf("Type: %s\n", element.Name)
123+
// }
124+
g.logger.Printf("Total types: %d\n", len(schema.Elements))
125+
}
126+
59127
return nil, nil
60128
}
61129

62130
func (g *GoWsdl) GenOperations() ([]byte, error) {
131+
if g.wsdl == nil {
132+
g.logger.Fatalln("You have to unmarshal the WSDL file first")
133+
}
134+
135+
for _, pt := range g.wsdl.PortTypes {
136+
// for _, o := range pt.Operations {
137+
// g.logger.Printf("Operation: %s", o.Name)
138+
// }
139+
g.logger.Printf("Total ops: %d\n", len(pt.Operations))
140+
}
141+
63142
return nil, nil
64143
}
65144

66145
func (g *GoWsdl) GenSoapProxy() ([]byte, error) {
146+
if g.wsdl == nil {
147+
g.logger.Fatalln("You have to unmarshal the WSDL file first")
148+
}
67149
return nil, nil
68150
}

xsd.go

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,96 @@
11
package main
22

3-
// import (
4-
// "encoding/xml"
5-
// )
3+
import (
4+
"encoding/xml"
5+
)
66

77
type XsdSchema struct {
8+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema schema"`
9+
Tns string `xml:"xmlns tns",attr`
10+
Xs string `xml:"xmlns xs,attr"`
11+
Version string `xml:"version,attr"`
812
TargetNamespace string `xml:"targetNamespace,attr"`
913
ElementFormDefault string `xml:"elementFormDefault,attr"`
1014
Includes []*XsdInclude `xml:"http://www.w3.org/2001/XMLSchema include"`
1115
Imports []*XsdImport `xml:"http://www.w3.org/2001/XMLSchema import"`
1216
Elements []*XsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
13-
ComplexTypes []*XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
17+
ComplexTypes []*XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"` //global
18+
SimpleType []*XsdSimpleType `xml:"http://www.w3.org/2001/XMLSchema simpleType"`
1419
}
1520

1621
type XsdInclude struct {
1722
SchemaLocation string `xml:"schemaLocation,attr"`
1823
}
1924

2025
type XsdImport struct {
21-
SchemaLocation string `xml:"schemaLocation,attr"`
22-
Namespace string `xml:"namespace,attr"`
26+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema import"`
27+
SchemaLocation string `xml:"schemaLocation,attr"`
28+
Namespace string `xml:"namespace,attr"`
2329
}
2430

2531
type XsdElement struct {
32+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema element"`
2633
Name string `xml:"name,attr"`
2734
Nillable bool `xml:"nillable,attr"`
2835
Type string `xml:"type,attr"`
2936
MinOccurs string `xml:"minOccurs,attr"`
3037
MaxOccurs string `xml:"maxOccurs,attr"`
31-
ComplexType *XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
38+
ComplexType *XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"` //local
3239
SimpleType *XsdSimpleType `xml:"http://www.w3.org/2001/XMLSchema simpleType"`
40+
Groups []*XsdGroup `xml:"http://www.w3.org/2001/XMLSchema group"`
3341
}
3442

3543
type XsdComplexType struct {
36-
Name string `xml:"name,attr"`
37-
Sequence *XsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
44+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema complexType"`
45+
Abstract bool `xml:"abstract,attr"`
46+
Name string `xml:"name,attr"`
47+
Mixed bool `xml:"mixed,attr"`
48+
Sequence []XsdElement `xml:"sequence>element"`
49+
Choice []XsdElement `xml:"choice>element"`
50+
All []XsdElement `xml:"all>element"`
51+
Content XsdComplexContent `xml:"http://www.w3.org/2001/XMLSchema complexContent"`
3852
}
3953

40-
type XsdSimpleType struct {
41-
Name string `xml:"name,attr"`
42-
Sequence *XsdRestriction `xml:"http://www.w3.org/2001/XMLSchema restriction"`
54+
type XsdGroup struct {
55+
Name string `xml:"name, attr"`
56+
Ref string `xml:"ref,attr"`
57+
Sequence []XsdElement `xml:"http://www.w3.org/2001/XMLSchema sequence"`
58+
Choice []XsdElement `xml:"http://www.w3.org/2001/XMLSchema choice"`
59+
All []XsdElement `xml:"http://www.w3.org/2001/XMLSchema all"`
4360
}
4461

45-
type XsdSequence struct {
46-
Elements []*XsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
62+
type XsdComplexContent struct {
63+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema complexContent"`
64+
Extension XsdExtension `xml:"http://www.w3.org/2001/XMLSchema extension"`
4765
}
4866

49-
type XsdRestriction struct {
50-
Base string `xml:"base,attr"`
51-
Pattern *XsdPattern `xml:"http://www.w3.org/2001/XMLSchema pattern"`
52-
MinInclusive *XsdMinInclusive `xml:"http://www.w3.org/2001/XMLSchema minInclusive"`
53-
MaxInclusive *XsdMaxInclusive `xml:"http://www.w3.org/2001/XMLSchema maxInclusive"`
67+
type XsdExtension struct {
68+
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema extension"`
69+
Base string `xml:"base,attr"`
70+
Sequence XsdSequence `xml:"http://www.w3.org/2001/XMLSchema extension sequence"`
5471
}
5572

56-
type XsdPattern struct {
57-
Value string `xml:"value,attr"`
73+
type XsdSimpleType struct {
74+
Name string `xml:"name,attr"`
75+
Retriction XsdRestriction `xml:"http://www.w3.org/2001/XMLSchema restriction"`
5876
}
5977

60-
type XsdMinInclusive struct {
61-
Value string `xml:"value,attr"`
78+
type XsdSequence struct {
79+
Elements []XsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
80+
}
81+
82+
type XsdRestriction struct {
83+
Base string `xml:"base,attr"`
84+
Enumeration []XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema enumeration"`
85+
Pattern XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema pattern"`
86+
MinInclusive XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema minInclusive"`
87+
MaxInclusive XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema maxInclusive"`
88+
WhiteSpace XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema whitespace"`
89+
Length XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema length"`
90+
MinLength XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema minLength"`
91+
MaxLength XsdRestrictionValue `xml:"http://www.w3.org/2001/XMLSchema maxLength"`
6292
}
6393

64-
type XsdMaxInclusive struct {
94+
type XsdRestrictionValue struct {
6595
Value string `xml:"value,attr"`
6696
}

0 commit comments

Comments
 (0)