Skip to content

Commit

Permalink
Merge pull request #193 from moihn/nillable-support
Browse files Browse the repository at this point in the history
add nillable=true support for data fields
  • Loading branch information
c4milo committed Feb 24, 2021
2 parents 1759fd8 + 43805e6 commit ef7ee60
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
5 changes: 4 additions & 1 deletion gowsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func removeNS(xsdType string) string {
return r[0]
}

func toGoType(xsdType string) string {
func toGoType(xsdType string, nillable bool) string {
// Handles name space, ie. xsd:string, xs:string
r := strings.Split(xsdType, ":")

Expand All @@ -468,6 +468,9 @@ func toGoType(xsdType string) string {
value := xsd2GoTypes[strings.ToLower(t)]

if value != "" {
if nillable {
value = "*" + value
}
return value
}

Expand Down
24 changes: 12 additions & 12 deletions types_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ var typesTmpl = `
{{$type := replaceReservedWords .Name | makePublic}}
{{if .Doc}} {{.Doc | comment}} {{end}}
{{if ne .List.ItemType ""}}
type {{$type}} []{{toGoType .List.ItemType }}
type {{$type}} []{{toGoType .List.ItemType false}}
{{else if ne .Union.MemberTypes ""}}
type {{$type}} string
{{else if .Union.SimpleType}}
type {{$type}} string
{{else if .Restriction.Base}}
type {{$type}} {{toGoType .Restriction.Base}}
type {{$type}} {{toGoType .Restriction.Base false}}
{{else}}
type {{$type}} interface{}
{{end}}
Expand All @@ -32,7 +32,7 @@ var typesTmpl = `
{{end}}
{{define "ComplexContent"}}
{{$baseType := toGoType .Extension.Base}}
{{$baseType := toGoType .Extension.Base false}}
{{ if $baseType }}
{{$baseType}}
{{end}}
Expand All @@ -46,15 +46,15 @@ var typesTmpl = `
{{range .}}
{{if .Doc}} {{.Doc | comment}} {{end}}
{{ if ne .Type "" }}
{{ normalize .Name | makeFieldPublic}} {{toGoType .Type}} ` + "`" + `xml:"{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ normalize .Name | makeFieldPublic}} {{toGoType .Type .Nillable}} ` + "`" + `xml:"{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ else }}
{{ normalize .Name | makeFieldPublic}} string ` + "`" + `xml:"{{.Name}},attr,omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ end }}
{{end}}
{{end}}
{{define "SimpleContent"}}
Value {{toGoType .Extension.Base}} ` + "`xml:\",chardata\" json:\"-,\"`" + `
Value {{toGoType .Extension.Base false}} ` + "`xml:\",chardata\" json:\"-,\"`" + `
{{template "Attributes" .Extension.Attributes}}
{{end}}
Expand All @@ -79,22 +79,22 @@ var typesTmpl = `
{{define "Elements"}}
{{range .}}
{{if ne .Ref ""}}
{{removeNS .Ref | replaceReservedWords | makePublic}} {{if eq .MaxOccurs "unbounded"}}[]{{end}}{{.Ref | toGoType}} ` + "`" + `xml:"{{.Ref | removeNS}},omitempty" json:"{{.Ref | removeNS}},omitempty"` + "`" + `
{{removeNS .Ref | replaceReservedWords | makePublic}} {{if eq .MaxOccurs "unbounded"}}[]{{end}}{{toGoType .Ref .Nillable }} ` + "`" + `xml:"{{.Ref | removeNS}},omitempty" json:"{{.Ref | removeNS}},omitempty"` + "`" + `
{{else}}
{{if not .Type}}
{{if .SimpleType}}
{{if .Doc}} {{.Doc | comment}} {{end}}
{{if ne .SimpleType.List.ItemType ""}}
{{ normalize .Name | makeFieldPublic}} []{{toGoType .SimpleType.List.ItemType}} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ normalize .Name | makeFieldPublic}} []{{toGoType .SimpleType.List.ItemType false}} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{else}}
{{ normalize .Name | makeFieldPublic}} {{toGoType .SimpleType.Restriction.Base}} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{ normalize .Name | makeFieldPublic}} {{toGoType .SimpleType.Restriction.Base false}} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + `
{{end}}
{{else}}
{{template "ComplexTypeInline" .}}
{{end}}
{{else}}
{{if .Doc}}{{.Doc | comment}} {{end}}
{{replaceAttrReservedWords .Name | makeFieldPublic}} {{if eq .MaxOccurs "unbounded"}}[]{{end}}{{.Type | toGoType}} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + ` {{end}}
{{replaceAttrReservedWords .Name | makeFieldPublic}} {{if eq .MaxOccurs "unbounded"}}[]{{end}}{{toGoType .Type .Nillable }} ` + "`" + `xml:"{{.Name}},omitempty" json:"{{.Name}},omitempty"` + "`" + ` {{end}}
{{end}}
{{end}}
{{end}}
Expand Down Expand Up @@ -134,16 +134,16 @@ var typesTmpl = `
}
{{end}}
{{else}}
{{if ne ($name | replaceReservedWords | makePublic) (toGoType .Type | removePointerFromType)}}
type {{$name | replaceReservedWords | makePublic}} {{toGoType .Type | removePointerFromType}}
{{if ne ($name | replaceReservedWords | makePublic) (toGoType .Type .Nillable | removePointerFromType)}}
type {{$name | replaceReservedWords | makePublic}} {{toGoType .Type .Nillable | removePointerFromType}}
{{end}}
{{end}}
{{end}}
{{range .ComplexTypes}}
{{/* ComplexTypeGlobal */}}
{{$name := replaceReservedWords .Name | makePublic}}
{{if eq (toGoType .SimpleContent.Extension.Base) "string"}}
{{if eq (toGoType .SimpleContent.Extension.Base false) "string"}}
type {{$name}} string
{{else}}
type {{$name}} struct {
Expand Down

0 comments on commit ef7ee60

Please sign in to comment.