Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around buggy encloding/xml ns attribute marshalling #218

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions fixtures/epcis/epcisquery.src
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type Document struct {
// The version of the schema corresponding to which the instance conforms.
//

SchemaVersion float64 `xml:"schemaVersion,attr,omitempty" json:"schemaVersion,omitempty"`
SchemaVersion float64 `xml:"urn:epcglobal:xsd:1 schemaVersion,attr,omitempty" json:"schemaVersion,omitempty"`

//
// The date the message was created. Used for auditing and logging.
//

CreationDate soap.XSDDateTime `xml:"creationDate,attr,omitempty" json:"creationDate,omitempty"`
CreationDate soap.XSDDateTime `xml:"urn:epcglobal:xsd:1 creationDate,attr,omitempty" json:"creationDate,omitempty"`
}

type EPC string
Expand Down Expand Up @@ -132,25 +132,25 @@ type BusinessService struct {
}

type ServiceTransaction struct {
TypeOfServiceTransaction *TypeOfServiceTransaction `xml:"TypeOfServiceTransaction,attr,omitempty" json:"TypeOfServiceTransaction,omitempty"`
TypeOfServiceTransaction *TypeOfServiceTransaction `xml:"http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader TypeOfServiceTransaction,attr,omitempty" json:"TypeOfServiceTransaction,omitempty"`

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

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

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

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

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

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

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

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

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

type StandardBusinessDocumentHeader struct {
Expand Down Expand Up @@ -268,7 +268,7 @@ type VocabularyType struct {

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

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

type VocabularyElementListType struct {
Expand All @@ -284,13 +284,13 @@ type VocabularyElementType struct {

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

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

type AttributeType struct {
AnyType

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

type IDListType struct {
Expand Down Expand Up @@ -378,7 +378,7 @@ type BusinessLocationExtensionType struct {
type BusinessTransactionType struct {
Value *BusinessTransactionIDType `xml:",chardata" json:"-,"`

Type *BusinessTransactionTypeIDType `xml:"type,attr,omitempty" json:"type,omitempty"`
Type *BusinessTransactionTypeIDType `xml:"urn:epcglobal:epcis:xsd:1 type,attr,omitempty" json:"type,omitempty"`
}

type BusinessTransactionListType struct {
Expand All @@ -388,7 +388,7 @@ type BusinessTransactionListType struct {
type SourceDestType struct {
Value *SourceDestIDType `xml:",chardata" json:"-,"`

Type *SourceDestTypeIDType `xml:"type,attr,omitempty" json:"type,omitempty"`
Type *SourceDestTypeIDType `xml:"urn:epcglobal:epcis:xsd:1 type,attr,omitempty" json:"type,omitempty"`
}

type SourceListType struct {
Expand Down
14 changes: 14 additions & 0 deletions gowsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ type GoWSDL struct {
wsdl *WSDL
resolvedXSDExternals map[string]bool
currentRecursionLevel uint8
currentNamespace string
}

// Method setNS sets (and returns) the currently active XML namespace.
func (g *GoWSDL) setNS(ns string) string {
g.currentNamespace = ns
return ns
}

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

var cacheDir = filepath.Join(os.TempDir(), "gowsdl-cache")
Expand Down Expand Up @@ -268,6 +280,8 @@ func (g *GoWSDL) genTypes() ([]byte, error) {
"goString": goString,
"findNameByType": g.findNameByType,
"removePointerFromType": removePointerFromType,
"setNS": g.setNS,
"getNS": g.getNS,
}

data := new(bytes.Buffer)
Expand Down
4 changes: 2 additions & 2 deletions gowsdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func TestAttributeRef(t *testing.T) {
Status []struct {
Value string ` + "`" + `xml:",chardata" json:"-,"` + "`" + `

Code string ` + "`" + `xml:"code,attr,omitempty" json:"code,omitempty"` + "`" + `
Code string ` + "`" + `xml:"http://www.mnb.hu/webservices/ code,attr,omitempty" json:"code,omitempty"` + "`" + `
} ` + "`" + `xml:"status,omitempty" json:"status,omitempty"` + "`" + `

ResponseCode string ` + "`" + `xml:"responseCode,attr,omitempty" json:"responseCode,omitempty"` + "`" + `
ResponseCode string ` + "`" + `xml:"http://www.mnb.hu/webservices/ responseCode,attr,omitempty" json:"responseCode,omitempty"` + "`" + `
}`
actual = string(bytes.ReplaceAll([]byte(actual), []byte("\t"), []byte(" ")))
expected = string(bytes.ReplaceAll([]byte(expected), []byte("\t"), []byte(" ")))
Expand Down
7 changes: 4 additions & 3 deletions types_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ var typesTmpl = `
{{end}}

{{define "Attributes"}}
{{ $targetNamespace := getNS }}
{{range .}}
{{if .Doc}} {{.Doc | comment}} {{end}}
{{ if ne .Type "" }}
{{ normalize .Name | makeFieldPublic}} {{toGoType .Type false}} ` + "`" + `xml:"{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ normalize .Name | makeFieldPublic}} {{toGoType .Type false}} ` + "`" + `xml:"{{with $targetNamespace}}{{.}} {{end}}{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ else }}
{{ normalize .Name | makeFieldPublic}} string ` + "`" + `xml:"{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ normalize .Name | makeFieldPublic}} string ` + "`" + `xml:"{{with $targetNamespace}}{{.}} {{end}}{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ end }}
{{end}}
{{end}}
Expand Down Expand Up @@ -106,7 +107,7 @@ var typesTmpl = `
{{end}}

{{range .Schemas}}
{{ $targetNamespace := .TargetNamespace }}
{{ $targetNamespace := setNS .TargetNamespace }}

{{range .SimpleType}}
{{template "SimpleType" .}}
Expand Down