Description
If XSD contains xs:union in type definition, it produces struct that contains both values. As result, XML fails to be parsed
Steps to reproduce the issue:
4. Here is sample XML to parse (report.xml):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<NETWORK_ACCESS_URL>
<DomainCensus AccessCount="149511825">84786</DomainCensus>
</NETWORK_ACCESS_URL>
Test code (file report_test.go):
package report
import (
"encoding/xml"
"os"
"testing"
)
func TestReport(t *testing.T) {
data, err := os.ReadFile("report.xml")
if err != nil {
t.Fatal(err)
}
var report NETWORKACCESSURL
if err := xml.Unmarshal(data, &report); err != nil {
t.Fatal(err)
}
}
**Describe the results you received:**
go test produces the following:
--- FAIL: TestReport (0.00s)
report_test.go:16: cannot unmarshal into report.Integerorempty
FAIL
exit status 1
FAIL github.com/mpkondrashin/ddan/report/t 0.251s
**Describe the results you expected:**
I have expected to parse file successfully
**Output of `go version`:**
```text
go version go1.18.3 darwin/amd64
Possible solution
I have no clue, how to solve it as unions of types are not supported by Golang (not sure about other languages).
What worked for me: Changing Integerorempty to int through whole report.go file (using Search & Replace) fixes this issue
I assume some option to force some types to be substituted as other types would be the solution. See https://github.com/xuri/xgen/issues/10 issue. This option would solve this issue too (interpret xsd "integer" as int, int64 or big.Int as user whats it to)
Description If XSD contains xs:union in type definition, it produces struct that contains both values. As result, XML fails to be parsed Steps to reproduce the issue:
package report
import ( "encoding/xml" )
// Integerorempty ... type Integerorempty struct { XMLName xml.Name
xml:"integer-or-empty"
Emptystring *Emptystring Integer int }// Emptystring ... type Emptystring string
// DomainCensus ... type DomainCensus struct { AccessCountAttr *Integerorempty
xml:"AccessCount,attr,omitempty"
Value stringxml:",chardata"
}// NETWORKACCESSURL ... type NETWORKACCESSURL struct { XMLName xml.Name
xml:"NETWORK_ACCESS_URL"
DomainCensus *DomainCensusxml:"DomainCensus"
}import ( "encoding/xml" "os" "testing" )
func TestReport(t *testing.T) { data, err := os.ReadFile("report.xml") if err != nil { t.Fatal(err) } var report NETWORKACCESSURL if err := xml.Unmarshal(data, &report); err != nil { t.Fatal(err) } }
--- FAIL: TestReport (0.00s) report_test.go:16: cannot unmarshal into report.Integerorempty FAIL exit status 1 FAIL github.com/mpkondrashin/ddan/report/t 0.251s
xgen version or commit ID:
Environment details (OS, physical, etc.): MacOS Monterey
Possible solution I have no clue, how to solve it as unions of types are not supported by Golang (not sure about other languages).
What worked for me: Changing Integerorempty to int through whole report.go file (using Search & Replace) fixes this issue
I assume some option to force some types to be substituted as other types would be the solution. See https://github.com/xuri/xgen/issues/10 issue. This option would solve this issue too (interpret xsd "integer" as int, int64 or big.Int as user whats it to)