-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/xml: embedded interface doesn't get marshaled correctly #69941
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
Comments
this is the same behavior as json, perhaps we just need to document this, ref #54322 |
I don't think it should be like that, we are embedding the interface, so it should follow the semantics of embedding (outer struct contains all of the fields of inner struct). |
interfaces don't have fields though, they have methods. and (most) methods of structs aren't called when marshaling |
Change https://go.dev/cl/621076 mentions this issue: |
Ok that makes sense, sorry. Updating the docs would be good. |
Go version
go version go1.22.4 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Embedding interface type in a struct, pass struct that implements that interface, then marshal it:
https://go.dev/play/p/SBfpU8nOB2S
Embedding concrete struct type in a struct, then marshal it:
https://go.dev/play/p/Wwf-DztRIlO
What did you see happen?
Output of first Playground link (https://go.dev/play/p/SBfpU8nOB2S):
Output of second Playground link (https://go.dev/play/p/Wwf-DztRIlO):
What did you expect to see?
I expected to get second output in both first and second Playground links.
According to the
encoding/xml
documentation, it says this two things:https://pkg.go.dev/encoding/xml#Marshal
So it should know that the interface value is a struct and is embedded, so it should treat the inner struct values as if it were part of the outer struct (basically accomplishing the second output)
The text was updated successfully, but these errors were encountered: