Skip to content

Commit 8c60951

Browse files
authored
This closes #47, support for optional element (#81)
1 parent 220da2b commit 8c60951

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

genGo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,15 @@ func (gen *CodeGenerator) GoComplexType(v *ComplexType) {
218218
if element.Plural {
219219
plural = "[]"
220220
}
221+
var optional string
222+
if element.Optional {
223+
optional = `,omitempty`
224+
}
221225
fieldType := genGoFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree))
222226
if fieldType == "time.Time" {
223227
gen.ImportTime = true
224228
}
225-
content += fmt.Sprintf("\t%s\t%s%s\t`xml:\"%s\"`\n", genGoFieldName(element.Name, false), plural, fieldType, element.Name)
229+
content += fmt.Sprintf("\t%s\t%s%s\t`xml:\"%s%s\"`\n", genGoFieldName(element.Name, false), plural, fieldType, element.Name, optional)
226230
}
227231
if len(v.Base) > 0 {
228232
// If the type is a built-in type, generate a Value field as chardata.

test/go/base64.xsd.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rs/base64.xsd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub struct TopLevel {
8282
#[serde(rename = "LastUpdated")]
8383
pub last_updated: Option<u8>,
8484
#[serde(rename = "nested")]
85-
pub nested: MyType7,
85+
pub nested: Option<MyType7>,
8686
#[serde(rename = "myType1")]
8787
pub my_type1: Vec<String>,
8888
#[serde(rename = "myType2")]

xmlElement.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ func (opt *Options) OnElement(ele xml.StartElement, protoTree []interface{}) (er
4848
e.Plural = true
4949
}
5050
}
51+
if attr.Name.Local == "minOccurs" {
52+
var minOccurs int
53+
if minOccurs, err = strconv.Atoi(attr.Value); err != nil {
54+
return
55+
}
56+
if minOccurs == 0 {
57+
e.Optional = true
58+
}
59+
}
5160
}
5261

5362
alreadyPushedElement := false

0 commit comments

Comments
 (0)