golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.66k stars 1.58k forks source link

I want to be able to override String method generation on a per-message basis #1477

Closed EricRabil closed 1 year ago

EricRabil commented 1 year ago

Is your feature request related to a problem? Please describe. I have a message/struct I am trying to integrate into an existing codebase:

message IPCGUID {
    string Service = 1;
    bool IsGroup = 2;
    string LocalID = 3;
}

This message already has an established stringified value {{Service}};{{IsGroup ? + : -}};{{LocalID}} value which I would like to implement in a sidecar file within the same generated package. However, this generator does not have any mechanism to influence which helper methods are/are not generated.

Describe the solution you'd like I would like to be able to specify, either by command line or annotation within the proto file, that specific methods on specific messages should not be generated.

Describe alternatives you've considered Writing a typealias against IPCGUID and shadowing the String() method is an alternative, this introduces nuances in how IPCGUID and AliasedIPCGUID can be interchanged, and makes protobuf less useful.

An alternative solution would be the ability to generate the String method in a way where it cannot be called, signaling developers to use the correct stringifier.

puellanivis commented 1 year ago

We’ve discussed this matter before recently.

I would recommend instead to introduce a method by a different name which encodes your specific use, c.f. GoString() string.

As an aside, note that you cannot add or override receiver methods on type aliases. You would have to instead use an embedded struct.