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

Support for optional element #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion genGo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/go/base64.xsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/rs/base64.xsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct TopLevel {
#[serde(rename = "LastUpdated")]
pub last_updated: Option<u8>,
#[serde(rename = "nested")]
pub nested: MyType7,
pub nested: Option<MyType7>,
#[serde(rename = "myType1")]
pub my_type1: Vec<String>,
#[serde(rename = "myType2")]
Expand Down
9 changes: 9 additions & 0 deletions xmlElement.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (opt *Options) OnElement(ele xml.StartElement, protoTree []interface{}) (er
e.Plural = true
}
}
if attr.Name.Local == "minOccurs" {
var minOccurs int
if minOccurs, err = strconv.Atoi(attr.Value); err != nil {
return
}
if minOccurs == 0 {
e.Optional = true
}
}
}

if e.Type == "" {
Expand Down