Skip to content
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

Add method that wraps oneof messages in their wrapper struct #727

Closed
nhooyr opened this issue Oct 14, 2018 · 5 comments
Closed

Add method that wraps oneof messages in their wrapper struct #727

nhooyr opened this issue Oct 14, 2018 · 5 comments

Comments

@nhooyr
Copy link

nhooyr commented Oct 14, 2018

Is your feature request related to a problem? Please describe.
oneof fields are annoying and clunky to use at the moment. In particular wrapping the actual type of the message with the wrapper struct is clunky.

Describe the solution you'd like
I want to write a function like this:

func (c *client) write(m proto.Message) {
	msg := mypbpackage.MasterMessage{
		Msg: mypbpackage.WrapMyOneOfField(m),
	}
	// Write the message to the client...
}

(I have a "master" message that contains all of the messages I can send to the client as a oneof field and this function is supposed to write one of those oneofed messages)

What's missing is the mypbpackage.WrapMyOneOfField method. I'd like that to be auto generated. It would take a proto.Message and return the interface implemented by all of the oneof wrapper structs (this interface would now have to become exported).

Describe alternatives you've considered
Manually writing this function. I'm doing this for now but it'd be nice if it was autogenerated. The other alternative is to manually wrap oneof messages every time which is annoying.

Somewhat related #708

@nhooyr nhooyr changed the title Add method that wraps oneof fields in their wrapper struct Add method that wraps oneof messages in their wrapper struct Oct 14, 2018
@puellanivis
Copy link
Collaborator

Given:

message A {
  oneof my_oneof {
    string name = 1;
    string alt_name = 2;
  }
}

How does your proposal work?

@nhooyr
Copy link
Author

nhooyr commented Oct 15, 2018

That is very unfortunate. I was not aware that was a possibility.

@dsnet
Copy link
Member

dsnet commented Oct 15, 2018

This is unrelated to #708 as that issue is regarding hiding internal details of oneof, and has nothing to do with the public API.

It seems that your use-case is more in line with the purposes of the Any message type given that you have a field in the "master" message can be only oneof a bunch of different messages.

As @puellanivis noted, oneofs do not seem to be what you want since:

  • They function as tagged unions where multiple members of the oneof may be of the same type, but with different names.
  • They types permitted in a oneof are not always messages.

Closing as infeasible. We can't add this functionality as it assumes some specific semantic of oneofs that isn't true.

@dsnet dsnet closed this as completed Oct 15, 2018
@nhooyr
Copy link
Author

nhooyr commented Oct 15, 2018

Thanks for the link to the Any message, that seems perfect.

@puellanivis
Copy link
Collaborator

Glad you’re able to find a forward path. 👍

@golang golang locked 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

3 participants