Skip to content
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

schema: add encoder.SetOmitEmptyDefault() method to properly encode ptrs #219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

david-its
Copy link

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

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
@lll-lll-lll-lll
Copy link
Contributor

lll-lll-lll-lll commented Aug 29, 2024

Wouldn't it be sufficient to test only the cases where the SetOmitEmptyDefault method is executed?

@zak905
Copy link
Contributor

zak905 commented Sep 7, 2024

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 ?

@jaitaiwan
Copy link
Member

jaitaiwan commented Sep 7, 2024

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:

  • Add a annotation which is the reverse of omitempty e.g. “emitempty”
  • Add documentation for the new annotation and function
  • Add tests showing the functionality
  • Add a test for the functionality being turned on and off again (stupid but could happen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants