schema: add encoder.SetOmitEmptyDefault() method to properly encode ptrs#219
schema: add encoder.SetOmitEmptyDefault() method to properly encode ptrs#219david-its wants to merge 1 commit intogorilla:mainfrom
Conversation
By default encoder encodes all nil ptr fields as "null" and decoder is
not able to decode them. Encoding such fields as empty ("") also doesn't
work properly as decoder always initializes ptr fields to zero values
even empty value provided. The only option is to add omitempty tags.
encoder.SetOmitEmptyDefault() allows to make omitempty to be a default
behaviour.
This closes issue gorilla#216
|
Wouldn't it be sufficient to test only the cases where the SetOmitEmptyDefault method is executed? |
|
Hi @david-its , I thought about this change set and after all, it sounds like more of a "nice to have", but not really necessary. I apologize but I initially misunderstood the intention. I thought that "omitempty" was not a feature of schema, and that you are going to provide such feature. I saw that "omitempty" is already provided, and the goal of this change set is to have an option that sets this behavior by default. I understand that in the case of your project this may be helpful, but I think that verbosity may have a benefit here (setting explicitly the "omitempty" option). Anyway I am not against it, but I see that all it will provide is saving a bit of typing. Ps: I don't have write permissions, so I cannot merge. @jaitaiwan any thoughts about this ? |
|
I actually really like this idea of changing the behaviour overall, this PR does it in a way that doesn’t break existing functionality but adds a feature I think is overall a completely valid use case but is not easy to be done by outside consumers. To make this PR complete we need to:
|
By default encoder encodes all nil ptr fields as "null" and decoder is not able to decode them. Encoding such fields as empty ("") also doesn't work properly as decoder always initializes ptr fields to zero values even empty value provided. The only option is to add omitempty tags.
encoder.SetOmitEmptyDefault() allows to make omitempty to be a default behaviour.
This closes issue #216