Skip to content

protoc-gen-go + omitempty JSON struct tags #799

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

Closed
evanfrawley opened this issue Feb 10, 2019 · 4 comments
Closed

protoc-gen-go + omitempty JSON struct tags #799

evanfrawley opened this issue Feb 10, 2019 · 4 comments

Comments

@evanfrawley
Copy link

message GameData {
    uint32 kills = 1;
    uint32 assists = 2;
    uint32 deaths = 3;
    uint32 seconds = 4;
    uint32 minutes = 5;
}

proto3 and protoc-go-gen generates this message into a struct with json:...,omitempty tags on them. Is there a way to prevent ,omitempty tags from being generated on the structs?

For the data, it is important that kill, deaths, or assists, can be 0 -- and I'd like that to appear instead of being empty when I generate JSON from the struct.

@farzaa
Copy link

farzaa commented Feb 11, 2019

Same issue!

@cybrcodr
Copy link
Contributor

cybrcodr commented Feb 12, 2019

Marshaling protobuf messages into JSON requires the use of the github.com/golang/protobuf/jsonpb package due to special rules and nuances around it. The Marshaler has EmitDefaults field option, if set to true, Marshaler will produce zero values for fields that are not set. Hope that works for you.

I'm curious though in terms of your use case, does the consumer require having those fields? It would be nice if the consumer is more flexible to deal with missing fields.

@evanfrawley
Copy link
Author

Thanks for the reply @cybrcodr !

When I ran into this problem, I was using a protobuf struct as a value in another struct's field (before sending it over to a client via http). I circumvented this issue by making a "client side" struct, that would take the protobuf's struct K/V pairs and translate them into the new struct, doing some data transformations as necessary. It would seem problematic to send over the raw decoded protobuf struct back up to a client via http, since protobuf fields can change and potentially break the client side code. Does this approach seem naive or have glaring issues?

I do agree with you that it would be nice if the consumer would be more flexible.

@dsnet
Copy link
Member

dsnet commented Feb 21, 2019

I'm going to close this in favor of #256. At this point, we're unlikely to allow manual control of the json tags. Generation of them in the first place was a mistake. For the use-cases (like yours) where you want to nest a protobuf message as part of a larger type tree, having the messages implement MarshalJSON and UnmarshalJSON would fix your problem.

@dsnet dsnet closed this as completed Feb 21, 2019
@golang golang locked as too heated and limited conversation to collaborators Jun 26, 2020
@golang golang unlocked this conversation Jun 26, 2020
@golang golang locked as resolved and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants