GoComply / xsd2go

Automatically generate golang xml parser based on XSD
http://isimluk.com/posts/2020/05/xsd2go-automatically-generate-golang-xml-parsers/
The Unlicense
71 stars 42 forks source link

Inlined complex types with same name result in duplicate struct definitions #129

Open nettnikl opened 2 weeks ago

nettnikl commented 2 weeks ago

Hi!

The tool is installed via

go install github.com/gocomply/xsd2go/cli/gocomply_xsd2go

Consider the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ak="http://www.bafa.de/ausfuhrkontrolle/schemas" targetNamespace="http://www.bafa.de/ausfuhrkontrolle/schemas" elementFormDefault="qualified" attributeFormDefault="qualified" version="1.0">
    <xsd:annotation/>
    <xsd:complexType name="AAA">
        <xsd:annotation/>
        <xsd:sequence>
        <xsd:element name="List" minOccurs="0">
            <xsd:annotation/>
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="CCC" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="BBB">
        <xsd:annotation/>
        <xsd:sequence>
            <xsd:element name="List" minOccurs="0">
                <xsd:annotation/>
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="DDD" type="xsd:int" minOccurs="1" maxOccurs="unbounded"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

This results in:

// Element
type List struct {
    XMLName xml.Name `xml:"List"`

    Ccc []string `xml:",any"`
}

// Element
type List struct {
    XMLName xml.Name `xml:"List"`

    Ddd []int `xml:",any"`
}
[...]

While i would expect this to be prefixed, or solved via anonymous structs, like e.g.:

// Element
type AaaList struct {
    XMLName xml.Name `xml:"List"`

    Ccc []string `xml:",any"`
}

// Element
type BbbList struct {
    XMLName xml.Name `xml:"List"`

    Ddd []int `xml:",any"`
}
[...]

or

// XSD ComplexType declarations

type Aaa struct {
    XMLName xml.Name

    List *struct {
        XMLName xml.Name `xml:"List"`

        Ccc []string `xml:",any"`
    } `xml:",any"`
}

type Bbb struct {
    XMLName xml.Name

    List *struct {
        XMLName xml.Name `xml:"List"`

        Ddd []int `xml:",any"`
    } `xml:",any"`
}
[...]

Thanks for consideration!

nettnikl commented 2 weeks ago

One way would be to set nameOverride for the dupe elements so no duplicates are returned by GoName().

nettnikl commented 2 weeks ago

Tested this change locally, seems to work. Can it be merged? Unit test is included.