xuri / xgen

XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
BSD 3-Clause "New" or "Revised" License
339 stars 80 forks source link

Support for xsd:choice plurality #38

Closed alexandre-normand closed 2 years ago

alexandre-normand commented 3 years ago

Is your feature request related to a problem? Please describe.

I've got an XSD using xsd:choice which isn't currently supported by xgen. Choices don't provide much as far as something that needs to be represented in generated code except for the plurality since a choice can be something like:

<complexType name="TopLevel">
  <choice minOccurs="0" maxOccurs="unbounded">
      <element name="myType1" type="here:myType1" />
      <element name="myType2" type="here:myType2" />
  </choice>
</complexType>

In the example case above, the generated code should be plural versions of myType1 and myType2 because the choice container says that maxOccurs is unbounded.

Describe the solution you'd like xgen should see choice elements and propagate the choice's plurality to the elements it contains. Since xgen doesn't generate anything regarding validation of min/max occurrences, this should be all there is to it to support xsd:choice elements.

Generated code for the example above (Go) would be something like:

type TopLevel struct {
    MyType1         []*MyType1   `xml:"myType1"`
    MyType2         []*MyType2   `xml:"myType2"`
}

Additional context I've got a PR almost ready for this one.