Skip to content

Commit

Permalink
Fixed attribute namespace handling client and server stub generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola.gaggi committed Dec 9, 2024
1 parent 4c2ddb2 commit 46b6722
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build
dist
.idea/
fixtures/epcis/epcisquery_gen.src
*.exe
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}
86 changes: 49 additions & 37 deletions cmd/gowsdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var outFile = flag.String("o", "myservice.go", "File where the generated code wi
var dir = flag.String("d", "./", "Directory under which package directory will be created")
var insecure = flag.Bool("i", false, "Skips TLS Verification")
var makePublic = flag.Bool("make-public", true, "Make the generated types public/exported")
var unqualifiedAttrs = flag.Bool("unqualified", false, "Make attribute name unqualified")
var clientStub = flag.Bool("client", true, "Generate client stub")
var serverStub = flag.Bool("server", false, "Generate server stub")

func init() {
log.SetFlags(0)
Expand Down Expand Up @@ -103,7 +106,7 @@ func main() {
}

// load wsdl
gowsdl, err := gen.NewGoWSDL(wsdlPath, *pkg, *insecure, *makePublic)
gowsdl, err := gen.NewGoWSDL(wsdlPath, *pkg, *insecure, *makePublic, *unqualifiedAttrs)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -115,47 +118,56 @@ func main() {
}

pkg := filepath.Join(*dir, *pkg)
err = os.Mkdir(pkg, 0744)

file, err := os.Create(filepath.Join(pkg, *outFile))
if err != nil {
log.Fatalln(err)
}
defer file.Close()

data := new(bytes.Buffer)
data.Write(gocode["header"])
data.Write(gocode["types"])
data.Write(gocode["operations"])
data.Write(gocode["soap"])

// go fmt the generated code
source, err := format.Source(data.Bytes())
if err != nil {
file.Write(data.Bytes())
log.Fatalln(err)
if *clientStub || *serverStub {
if err := os.Mkdir(pkg, 0744); err != nil {
log.Fatalln(err)
}
}

file.Write(source)

// server
serverFile, err := os.Create(pkg + "/" + "server" + *outFile)
if err != nil {
log.Fatalln(err)
if *clientStub {
// Generate client code
file, err := os.Create(filepath.Join(pkg, *outFile))
if err != nil {
log.Fatalln(err)
}
defer file.Close()

data := new(bytes.Buffer)
data.Write(gocode["header"])
data.Write(gocode["types"])
data.Write(gocode["operations"])
data.Write(gocode["soap"])

// go fmt the generated code
source, err := format.Source(data.Bytes())
if err != nil {
file.Write(data.Bytes())
log.Fatalln(err)
}

file.Write(source)
}
defer serverFile.Close()

serverData := new(bytes.Buffer)
serverData.Write(gocode["server_header"])
serverData.Write(gocode["server_wsdl"])
serverData.Write(gocode["server"])

serverSource, err := format.Source(serverData.Bytes())
if err != nil {
serverFile.Write(serverData.Bytes())
log.Fatalln(err)
if *serverStub {
// Generate server stub
serverFile, err := os.Create(pkg + "/" + "server" + *outFile)
if err != nil {
log.Fatalln(err)
}
defer serverFile.Close()

serverData := new(bytes.Buffer)
serverData.Write(gocode["server_header"])
serverData.Write(gocode["server_wsdl"])
serverData.Write(gocode["server"])

serverSource, err := format.Source(serverData.Bytes())
if err != nil {
serverFile.Write(serverData.Bytes())
log.Fatalln(err)
}
serverFile.Write(serverSource)
}
serverFile.Write(serverSource)

log.Println("Done 👍")
}
60 changes: 27 additions & 33 deletions fixtures/epcis/epcisquery.src
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ import (
var _ time.Time
var _ xml.Name

type AnyType struct {
InnerXML string `xml:",innerxml"`
}

type AnyURI string

type NCName string

type Document struct {

//
Expand Down Expand Up @@ -59,9 +51,11 @@ type Partner struct {
}

type PartnerIdentification struct {
XMLName xml.Name `xml:"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader Identifier"`

Value string `xml:",chardata" json:"-,"`

Authority string `xml:"Authority,attr,omitempty" json:"Authority,omitempty"`
Authority string `xml:"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader Authority,attr,omitempty" json:"Authority,omitempty"`
}

type ContactInformation struct {
Expand Down Expand Up @@ -96,7 +90,7 @@ type Manifest struct {
type ManifestItem struct {
MimeTypeQualifierCode *MimeTypeQualifier `xml:"MimeTypeQualifierCode,omitempty" json:"MimeTypeQualifierCode,omitempty"`

UniformResourceIdentifier AnyURI `xml:"UniformResourceIdentifier,omitempty" json:"UniformResourceIdentifier,omitempty"`
UniformResourceIdentifier soap.AnyURI `xml:"UniformResourceIdentifier,omitempty" json:"UniformResourceIdentifier,omitempty"`

Description string `xml:"Description,omitempty" json:"Description,omitempty"`

Expand All @@ -111,7 +105,7 @@ const (
TypeOfServiceTransactionRespondingServiceTransaction TypeOfServiceTransaction = "RespondingServiceTransaction"
)

type ScopeInformation AnyType
type ScopeInformation soap.AnyType

type BusinessScope struct {
Scope []*Scope `xml:"Scope,omitempty" json:"Scope,omitempty"`
Expand Down Expand Up @@ -187,33 +181,33 @@ const (
ActionTypeDELETE ActionType = "DELETE"
)

type ParentIDType AnyURI
type ParentIDType soap.AnyURI

type BusinessStepIDType AnyURI
type BusinessStepIDType soap.AnyURI

type DispositionIDType AnyURI
type DispositionIDType soap.AnyURI

type EPCClassType AnyURI
type EPCClassType soap.AnyURI

type UOMType string

type ReadPointIDType AnyURI
type ReadPointIDType soap.AnyURI

type BusinessLocationIDType AnyURI
type BusinessLocationIDType soap.AnyURI

type BusinessTransactionIDType AnyURI
type BusinessTransactionIDType soap.AnyURI

type BusinessTransactionTypeIDType AnyURI
type BusinessTransactionTypeIDType soap.AnyURI

type SourceDestIDType AnyURI
type SourceDestIDType soap.AnyURI

type SourceDestTypeIDType AnyURI
type SourceDestTypeIDType soap.AnyURI

type TransformationIDType AnyURI
type TransformationIDType soap.AnyURI

type EventIDType AnyURI
type EventIDType soap.AnyURI

type ErrorReasonIDType AnyURI
type ErrorReasonIDType soap.AnyURI

type EPCISDocument EPCISDocumentType

Expand Down Expand Up @@ -288,7 +282,7 @@ type VocabularyType struct {

Items []string `xml:",any" json:"items,omitempty"`

Type AnyURI `xml:"urn:epcglobal:epcis:xsd:1 type,attr,omitempty" json:"type,omitempty"`
Type soap.AnyURI `xml:"urn:epcglobal:epcis:xsd:1 type,attr,omitempty" json:"type,omitempty"`
}

type VocabularyElementListType struct {
Expand All @@ -308,21 +302,21 @@ type VocabularyElementType struct {

Items []string `xml:",any" json:"items,omitempty"`

Id AnyURI `xml:"urn:epcglobal:epcis:xsd:1 id,attr,omitempty" json:"id,omitempty"`
Id soap.AnyURI `xml:"urn:epcglobal:epcis:xsd:1 id,attr,omitempty" json:"id,omitempty"`
}

type AttributeType struct {
XMLName xml.Name `xml:"urn:epcglobal:epcis:xsd:1 attribute"`

AnyType
soap.AnyType

Id AnyURI `xml:"urn:epcglobal:epcis:xsd:1 id,attr,omitempty" json:"id,omitempty"`
Id soap.AnyURI `xml:"urn:epcglobal:epcis:xsd:1 id,attr,omitempty" json:"id,omitempty"`
}

type IDListType struct {
XMLName xml.Name `xml:"urn:epcglobal:epcis:xsd:1 children"`

Id []AnyURI `xml:"id,omitempty" json:"id,omitempty"`
Id []soap.AnyURI `xml:"id,omitempty" json:"id,omitempty"`
}

type VocabularyExtensionType struct {
Expand Down Expand Up @@ -719,7 +713,7 @@ type TransformationEventExtensionType struct {
Items []string `xml:",any" json:"items,omitempty"`
}

type ImplementationExceptionSeverity NCName
type ImplementationExceptionSeverity soap.NCName

const (
ImplementationExceptionSeverityERROR ImplementationExceptionSeverity = "ERROR"
Expand Down Expand Up @@ -828,7 +822,7 @@ type Subscribe struct {

Params *QueryParams `xml:"params,omitempty" json:"params,omitempty"`

Dest AnyURI `xml:"dest,omitempty" json:"dest,omitempty"`
Dest soap.AnyURI `xml:"dest,omitempty" json:"dest,omitempty"`

Controls *SubscriptionControls `xml:"controls,omitempty" json:"controls,omitempty"`

Expand Down Expand Up @@ -864,7 +858,7 @@ type SubscriptionControls struct {

Schedule *QuerySchedule `xml:"schedule,omitempty" json:"schedule,omitempty"`

Trigger AnyURI `xml:"trigger,omitempty" json:"trigger,omitempty"`
Trigger soap.AnyURI `xml:"trigger,omitempty" json:"trigger,omitempty"`

InitialRecordTime soap.XSDDateTime `xml:"initialRecordTime,omitempty" json:"initialRecordTime,omitempty"`

Expand Down Expand Up @@ -918,7 +912,7 @@ type QueryParam struct {

Name string `xml:"name,omitempty" json:"name,omitempty"`

Value AnyType `xml:"value,omitempty" json:"value,omitempty"`
Value soap.AnyType `xml:"value,omitempty" json:"value,omitempty"`
}

type QueryResults struct {
Expand Down
22 changes: 14 additions & 8 deletions gowsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type GoWSDL struct {
rawWSDL []byte
pkg string
ignoreTLS bool
unqualifiedAttrs bool
makePublicFn func(string) string
wsdl *WSDL
resolvedXSDExternals map[string]bool
Expand All @@ -47,6 +48,10 @@ func (g *GoWSDL) setNS(ns string) string {

// Method setNS returns the currently active XML namespace.
func (g *GoWSDL) getNS() string {
if g.unqualifiedAttrs {
return ""
}

return g.currentNamespace
}

Expand Down Expand Up @@ -94,7 +99,7 @@ func downloadFile(url string, ignoreTLS bool) ([]byte, error) {
}

// NewGoWSDL initializes WSDL generator.
func NewGoWSDL(file, pkg string, ignoreTLS bool, exportAllTypes bool) (*GoWSDL, error) {
func NewGoWSDL(file, pkg string, ignoreTLS bool, exportAllTypes bool, unqualifiedAttrs bool) (*GoWSDL, error) {
file = strings.TrimSpace(file)
if file == "" {
return nil, errors.New("WSDL file is required to generate Go proxy")
Expand All @@ -115,10 +120,11 @@ func NewGoWSDL(file, pkg string, ignoreTLS bool, exportAllTypes bool) (*GoWSDL,
}

return &GoWSDL{
loc: r,
pkg: pkg,
ignoreTLS: ignoreTLS,
makePublicFn: makePublicFn,
loc: r,
pkg: pkg,
ignoreTLS: ignoreTLS,
makePublicFn: makePublicFn,
unqualifiedAttrs: unqualifiedAttrs,
}, nil
}

Expand Down Expand Up @@ -524,9 +530,9 @@ var xsd2GoTypes = map[string]string{
"unsignedshort": "uint16",
"unsignedbyte": "byte",
"unsignedlong": "uint64",
"anytype": "AnyType",
"ncname": "NCName",
"anyuri": "AnyURI",
"anytype": "soap.AnyType",
"ncname": "soap.NCName",
"anyuri": "soap.AnyURI",
}

func removeNS(xsdType string) string {
Expand Down
Loading

0 comments on commit 46b6722

Please sign in to comment.