xuri / xgen

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

Wrong Element genetarion #3

Closed luizvnasc closed 4 years ago

luizvnasc commented 4 years ago

I'm trying to generate a struct for the bellow element but the result wasn't the expected. Element

<xs:element name="infNFe" maxOccurs="unbounded">
<!-- Other elements-->
<xs:element name="PIN" minOccurs="0">
    <xs:annotation>
        <xs:documentation>PIN SUFRAMA</xs:documentation>
        <xs:documentation>PIN atribuído pela SUFRAMA para a operação.</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:whiteSpace value="preserve"/>
            <xs:minLength value="2"/>
            <xs:maxLength value="9"/>
            <xs:pattern value="[1-9]{1}[0-9]{1,8}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>
</xs:element>

Generated code :

// InfNFe ...
type InfNFe struct {
    XMLName xml.Name    `xml:"infNFe"`
    Chave   string  `xml:"chave"`
    PIN *PIN    `xml:"PIN"`
    DPrev   string  `xml:"dPrev"`
    InfUnidCarga    []*TUnidCarga   `xml:"infUnidCarga"`
    InfUnidTransp   []*TUnidadeTransp   `xml:"infUnidTransp"`
}

Expected result:

// InfNFe ...
type InfNFe struct {
    XMLName xml.Name    `xml:"infNFe"`
    Chave   string  `xml:"chave"`
    PIN *string `xml:"PIN"`
    DPrev   string  `xml:"dPrev"`
    InfUnidCarga    []*TUnidCarga   `xml:"infUnidCarga"`
    InfUnidTransp   []*TUnidadeTransp   `xml:"infUnidTransp"`
}

Output of go version:

go version go1.14.2 windows/amd64

Environment details (OS, physical, etc.):

windows 10 64bits
xuri commented 4 years ago

Thanks for your issue. Could you provide more info about XSD? Maybe I need more context to find the reason.

luizvnasc commented 4 years ago

The xsd is here.

I found other problem that all, or almost all, type that is created is a pointer in the struct. So it's very difficult to manage the struct where you have a lot of pointers or pointers of pointers. But it probably could be an other issue to duscuss.

xuri commented 4 years ago

Hi @luizvnasc, I have fixed it, please try to upgrade the command line tools with the master branch code.

luizvnasc commented 4 years ago

Hi @xuri, thanks, but as you can see in #4 the bug is still happening. What I did:

$ go get -u github.com/xuri/xgen
go: github.com/xuri/xgen upgrade => v0.0.0-20200517172312-9d8630233132
go: finding module for package golang.org/x/net/html/charset
go: found golang.org/x/net/html/charset in golang.org/x/net v0.0.0-20200513185701-a91f0712d120
go: golang.org/x/text upgrade => v0.3.2
$ xgen -l Go -p evento -o ./v1.00/evento/ -i ./schemas/Evento_AlertaFisco_v1.01/e400101_v1.00.xsd
done