From 96a404e71ad544000337dc08a96334050394fff0 Mon Sep 17 00:00:00 2001 From: Georg Koester Date: Mon, 23 Jan 2023 11:32:13 +0100 Subject: [PATCH 1/3] correctly handling optional xml elements in golang (cherry picked from commit 3654841921dc9fac46f08d754513e9ece38ed664) --- genGo.go | 6 +++++- xmlElement.go | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/genGo.go b/genGo.go index c27fa36..e7b9885 100644 --- a/genGo.go +++ b/genGo.go @@ -218,11 +218,15 @@ func (gen *CodeGenerator) GoComplexType(v *ComplexType) { if element.Plural { plural = "[]" } + var optional string + if element.Optional { + optional = `,omitempty` + } fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree)) if fieldType == "time.Time" { gen.ImportTime = true } - content += fmt.Sprintf("\t%s\t%s%s\t`xml:\"%s\"`\n", genGoFieldName(element.Name, false), plural, fieldType, element.Name) + content += fmt.Sprintf("\t%s\t%s%s\t`xml:\"%s%s\"`\n", genGoFieldName(element.Name, false), plural, fieldType, element.Name, optional) } if len(v.Base) > 0 { // If the type is a built-in type, generate a Value field as chardata. diff --git a/xmlElement.go b/xmlElement.go index ffc3c84..37d053d 100644 --- a/xmlElement.go +++ b/xmlElement.go @@ -48,6 +48,11 @@ func (opt *Options) OnElement(ele xml.StartElement, protoTree []interface{}) (er e.Plural = true } } + if attr.Name.Local == "minOccurs" { + if attr.Value == "0" { + e.Optional = true + } + } } if e.Type == "" { From 66c5c04e44189d6cfef58dda4a1927fbef36f9f8 Mon Sep 17 00:00:00 2001 From: Dheeraj Nalluri Date: Mon, 14 Oct 2024 12:31:33 -0500 Subject: [PATCH 2/3] Validate minOccurs value Signed-off-by: Dheeraj Nalluri --- xmlElement.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xmlElement.go b/xmlElement.go index 37d053d..c46b5f2 100644 --- a/xmlElement.go +++ b/xmlElement.go @@ -49,7 +49,11 @@ func (opt *Options) OnElement(ele xml.StartElement, protoTree []interface{}) (er } } if attr.Name.Local == "minOccurs" { - if attr.Value == "0" { + var minOccurs int + if minOccurs, err = strconv.Atoi(attr.Value); err != nil { + return + } + if minOccurs == 0 { e.Optional = true } } From 22506280d63ca9989b37ed805ed58406e238a8ad Mon Sep 17 00:00:00 2001 From: Dheeraj Nalluri Date: Mon, 14 Oct 2024 12:42:01 -0500 Subject: [PATCH 3/3] Fix tests Signed-off-by: Dheeraj Nalluri --- test/go/base64.xsd.go | 2 +- test/rs/base64.xsd.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/go/base64.xsd.go b/test/go/base64.xsd.go index f9ab564..c78251a 100644 --- a/test/go/base64.xsd.go +++ b/test/go/base64.xsd.go @@ -50,7 +50,7 @@ type MyType7 struct { type TopLevel struct { CostAttr float64 `xml:"cost,attr,omitempty"` LastUpdatedAttr string `xml:"LastUpdated,attr,omitempty"` - Nested *MyType7 `xml:"nested"` + Nested *MyType7 `xml:"nested,omitempty"` MyType1 []string `xml:"myType1"` MyType2 []*MyType2 `xml:"myType2"` *MyType6 diff --git a/test/rs/base64.xsd.rs b/test/rs/base64.xsd.rs index e42ba8b..fadcc04 100644 --- a/test/rs/base64.xsd.rs +++ b/test/rs/base64.xsd.rs @@ -84,7 +84,7 @@ pub struct TopLevel { #[serde(rename = "LastUpdated")] pub last_updated: Option, #[serde(rename = "nested")] - pub nested: MyType7, + pub nested: Option, #[serde(rename = "myType1")] pub my_type1: Vec, #[serde(rename = "myType2")]