protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.51k stars 15.46k forks source link

`TextFormat::Printer` add `always_print_primitive_fields` option #17150

Open filippobrizzi opened 4 months ago

filippobrizzi commented 4 months ago

What language does this apply to? If it's a proto syntax change, is it for proto2 or proto3? Any If it's about generated code change, what programming language? C++

Describe the problem you are trying to solve. TextFormat::Printer doesn't print primitive fields that have default values.

Describe the solution you'd like JsonPrintOptions provide a always_print_primitive_fields option, it would be amazing if we could have the same option for TextFormat::Printer

Additional context Curiosity: why is the default not to print primitive default values at all? Considering that TextFormat is primarily for humans to read proto message it feel counterintuitive.

lhruby commented 3 months ago

The field changed name from always_print_primitive_fields to always_print_fields_with_no_presence in the 2024 Protobuf version, but the question remains valid.

I would also be interested in the reasoning behind hiding primitives, which includes e.g. boolean flags, where true is printed, but false is hidden. This behavior is counter intuitive.

JasonLunn commented 2 weeks ago

Default values for primitives don't have to be serialized in text proto for the same reason as they don't need to be serialized in the binary format - their absence is synonymous with having the default value, so sending it to the recipient is redundant.

The Protobuf team does not have the bandwidth internally to prioritize landing a new option to control this aspect of text proto serialization. Is that something you would want contribute? Keep in mind, that would involve introducing support for the option across the various language ecosystems.

filippobrizzi commented 1 week ago

Thanks for the reply, it makes perfect sense, however, I would argue that in the case of text proto the priority should be to optimize for readability rather than performance. I'll see if I can manage to contribute to the feature.