golang/protobuf

Why is .String() not configurable for generated pb models? forced to use prototext

gdpaulmil opened this issue · 2 comments

Why not allow the users to configure this - ie. using protojson instead of prototext,
sure you can probably do some shenanigans with wrappers and custom "proxies", but why not let do that out of the box

func (Export) MessageStringOf(m protoreflect.ProtoMessage) string {
	return prototext.MarshalOptions{Multiline: false}.Format(m)
}

which returns stuff in weird format of: address:{city:"LOS ANGELES" state:"CA" zip:"90033"
i'd rather want to have this as JSON, so its more parser/monitoring friendly and transparent

The String receiver method is intended for debugging purposes, and follows the Text Format Language Specification, and was designed to be more human friendly than JSON, because that’s who is primarily consuming debugging output.

When considering to add a new feature, we want to look at what the expected benefits vs costs are, and here, I’m not sure I see significant value in providing a dial to customize the String method, when protojson would require basically the same amount of setup to provide. That is, once you’ve setup the MarshalOptions singleton object that you would inject into the String method, you can just use the Format on that yourself.

Additionally, this would require setting some sort of global state, which could have unexpected impacts with libraries or packages that are not expecting such a different format, or even worse, unexpected behavior when some binary includes or updates a package that now is setting the marshaller to something else.

If you want JSON string output, then it’s recommended to setup your own protojson.MarshalOptions setting the options you want/need, and then call marshaller.Format(myPB) instead of myPB.String().

These are all very good arguments. Closing.