-
Notifications
You must be signed in to change notification settings - Fork 212
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
marshal: do not encode embedded structs as sub-table #368
Conversation
Codecov Report
@@ Coverage Diff @@
## master #368 +/- ##
==========================================
+ Coverage 95.08% 95.12% +0.03%
==========================================
Files 10 10
Lines 2118 2134 +16
==========================================
+ Hits 2014 2030 +16
Misses 64 64
Partials 40 40
Continue to review full report at Codecov.
|
Hi, please let me know if you agree with this approach. In case we cannot do this, because it is a breaking change, we could also add another option to the tag. Something like |
Great catch! It is a breaking change, but I see it like a bugfix (it was not supposed to do this in the first place). If you wouldn't mind, I'd love to this is as the default behavior, but maybe with an option on Encoder to be able to revert to he previous behavior in case people depend on it (I think a per-field flag is overkill). |
Alright. I updated the code with a I also changed the behavior on handling duplicate keys. It is now in line with encoding/json. This should now be ready to review. |
Currently, the marshalling code encodes the embedded structs as sub-tables. This is a bit unexpected, as it differs from what encoding/json does in that case: https://play.golang.org/p/KDPaGtrijV1 Unmarshalling code handles this scenario gracefully. This PR adapts the encoder to behave like encoding/json. Fields in an embedded struct are promoted to the top level table. In case the embedded struct is named in the tag, it will still encode as a sub-table.
The added PromoteAnonymous option on the Encoder allows configuring the old behavior, where anonymous structs are encoded as sub-tables. On duplicate keys, the behavior of encoding/json is mimicked: Fields from anonymous structs are shadowed by regular fields. An example is added to show the affects of setting PromoteAnonymous.
Name looks good, patch is great! Thanks a lot for taking care of this. |
Currently, the marshalling code encodes the embedded structs as sub-tables.
This is a bit unexpected, as it differs from what encoding/json does in
that case: https://play.golang.org/p/KDPaGtrijV1
This PR adapts the encoder to behave like encoding/json.
Fields in an embedded struct are promoted to the top level table.
In case the embedded struct is named in the tag, it will still
encode as a sub-table.