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
proposal: cmd/vet: warn about time.Time struct fields marked json omitempty #51261
Comments
Scanning all modules, I found ~73k fields where the type was either That's pretty significant evidence that this is a highly frequent problem. |
Is there any reason to limit this to time.Time instead of applying to all structs? |
I advocate expanding it to all structs. |
Yes this shall be extending any non-primitive and non-reference types, mainly struct. Interestingly, the official document specifies:
which actually indicates that a conflicting check that is supposed to report usage of |
I don't write much JSON code, so take my thoughts with a grain of salt, but this problem seems to indicate that there should be some form of [1] Admittedly this is still a problem for |
I agree there should be some type of |
It is a little bit confusing what "empty" means. Currently:
Regarding a composite object, what does "empty" or "zero" mean? Consider these cases:
Users seems to assume that (2) is "empty" when using "omitempty". |
I think this issue getting off topic. There's a lot of valid questions with regard to |
One concern I have is how to educate users. It seems like the most likely cause is the user misunderstanding of how structs interact with encoding/json with omitempty being a symptom. Vet's message have historically been quite terse. I am hesitant to give suggestions like "rewrite time.Time to *time.Time here" without giving an explanation as to why (and can cause other bugs during the transition). Similarly a message like "struct field %s is not emitted by encoding/json so the omitempty tag is redundant" seems like it would often [usually?] result in the omitempty tag being removed which does not help the underlying confusion. Do folks have a suggestion for what to tell the user in the diagnostic? What I really want to do is point users at a longer discussion of the issue (like 1-2 paragraphs describing the issue and possible solutions), but to the best of my knowledge this has not done this before in vet. |
Change https://go.dev/cl/388574 mentions this issue: |
Marking a struct field of type
time.Time
as json omitempty has no effect. You must use a*time.Time
for it to work (which is wire compatible). Vet should detect this situation and perhaps suggest*time.Time
instead.This might be reasonably extended to any struct type; see the linked omitzero proposals for more discussion. If an omitzero proposal is accepted, vet could switch to suggesting the use of omitzero rather than suggesting a pointer type.
cc @dsnet @robpike @dominikh
The text was updated successfully, but these errors were encountered: