-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Add support for inlining struct fields. #40
Conversation
fbf0494
to
dca6e0d
Compare
Looks solid, thanks. I had something similar before, but it caused #20 and was reverted rjeczalik@f2a814a. I still have to think about implications. |
if ftyp.Kind() == reflect.Ptr { | ||
ftyp = ftyp.Elem() | ||
} | ||
if opts.Contains("inline") && ftyp.Kind() == reflect.Struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably inline -> embed to match Go naming of the concept?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed embed
would fit better, however there was already precedence and inline
shows up in couple of places:
- proposed
inline
opt forencoding/json
(proposal: encoding/json: add "inline" struct tag golang/go#6213) - existing
inline
opt ingopkg.in/yaml.v2
(https://godoc.org/gopkg.in/yaml.v2#Marshal)
So I think it'd be better to stick with inline
.
Can you please add following test case:
What is the expected behaviour in such case? |
c7a3141
to
520d3f3
Compare
msgpack encoder differ in behaviour from the json one - the latter inlines fields of an embedded struct as they were part of the parent struct. While we can't change the behaviour due to compatibility reasons we can introduce `inline` opt, which will make the encoding similar in behaviour to the json one.
@vmihailenco Rebased with master and added a test-case for
I think the simplest rule here would be: if a struct has no fields to inline it'll be encoded/decode as a whole. In particular marshalling:
and
will give the same results. The same behaviour is in |
// a whole later. | ||
if len(flds.Fields) != 0 { | ||
continue | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this block a bit confusing to understand. Probably:
inlinedFields := getFields(ftyp)
if len(inlinedFields) > 0 {
inlineFields(fs, inlinedFields)
continue
}
What do you think?
What about inlining struct that implements |
Yup, sorry, I did not take custom serialisation into account in my initial implementation - I reworked this in my latest commit, which makes The other option instead of ignoring would be to firstly encode the field with custom encoder or globally registered one and then inline all fields of such struct. The implementation would be a bit more complex, so I think we could defer it to a point where such use-case would be needed and right now live with simpler implementation. @vmihailenco What do you think? |
Actually I think |
@vmihailenco Sure, it's fine by me. |
Thanks. I will change it in separate PR. |
Add support for inlining struct fields.
msgpack encoder differ in behaviour from the json one - the latter inlines
fields of an embedded struct as they were part of the parent struct.
While we can't change the behaviour due to compatibility reasons we can
introduce
inline
opt, which will make the encoding similar inbehaviour to the json one.
/cc @vmihailenco Please take a look.