go-xml
go-xml copied to clipboard
xsd:sequence is not respected when using layout struct to handle non-builtin types
I have an xsd:sequence that has an xsd:dateTime in the 3rd position. As you know, xsd:sequence is strictly positional.
The generated MarshalXML code declares a layout struct to handle the conversion from time.Time to *xsdDateTime with a new member with the same same (member shadowing)
Unfortunately the shadowing member is serialized at last position, so the generated XML is not validated against the original XSD file
I hope my explanation is clear enough, I am attaching XSD files I am using to reproduce this issue (Italian electronic invoice formats)
This is tricky, because the original struct is embedded in the layout struct:
var layout struct{
*T
{{- range .Overrides}}
{{.FieldName}} *{{.ToType}}`+"`{{.Tag}}`"+`
{{end -}}
}
The most straightforward way I can see to preserve order is to do away with the embedding, name each field of the layout struct with the appropriate type, and essentially copy the original type by assigning to each field.
This will make the MarshalXML functions for large structs very long, as it will require at least two lines per field. I could use the reflect package to reduce it, but I was hoping to avoid that.
I had this problem also.
Solution was I changed the type in the struct from time.Time to xsdDateTime, and now this just uses the custom marshal/unmarshal interface on that type directly, rather than going through a layout stuff for the higher level struct.
Unclear if I've caused some edge case or not, but so far it appears to work just fine, and order is preserved.
It does mean I have to type override it when using the struct, but that's fine for my usecase.