Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xsd:sequence is not respected when using layout struct to handle non-builtin types #85

Open
genez opened this issue Apr 26, 2019 · 2 comments

Comments

@genez
Copy link

genez commented Apr 26, 2019

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)

sdi.zip

@droyo
Copy link
Owner

droyo commented May 11, 2019

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.

@markfarnan
Copy link

markfarnan commented Jan 10, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants