-
Notifications
You must be signed in to change notification settings - Fork 232
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
[FEATURE] Use omitempty by default #216
Comments
Hi @david-its, I was able to reproduce the issue, off course this would happen only when encoding. The following can be used as workaround:
are you willing to provide an implementation to the solution ? |
Such workaround is not correct unfortunately. When we use I'm not sure which solution is better:
2nd solution is very simple and I can create a PR. |
awesome thanks. I am also leaning towards option 2, because using a flag, we can keep backward compatibility, just in case someone is misusing the old behavior. Yes, indeed, I overlooked the case where the value is nil, in this case an empty string should be returned. a key will be present in the values without a value, but this is ok I guess when dealing with form submissions or query parameters
|
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
@zak905 this doesn't work as well, cause decoder automatically allocates zero values to fields even if there is no value provided. If I remember correctly, it happens at lines 306-312 in decode():
as a result you never have nil ptr in the field even though you decode I have submitted PR for default omit empty. |
Is there an existing feature request for this?
Is your feature request related to a problem? Please describe.
We heavily use schema to encode request parameters in URL and to avoid adding "omitempty" in all tags would like to have an ability to make it default behavior by calling
Encoder.SetDefaultOmitEmpty(true)
.If no objections we can submit a patch for that.
Describe the solution that you would like.
A new method
Encoder.SetDefaultOmitEmpty(true)
which makes omitempty to be a default behavior.Describe alternatives you have considered.
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: