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

Alternative to reflection for union types #68

Open
mmaz opened this issue Aug 20, 2013 · 2 comments
Open

Alternative to reflection for union types #68

mmaz opened this issue Aug 20, 2013 · 2 comments

Comments

@mmaz
Copy link

mmaz commented Aug 20, 2013

Is there an idiomatic way in ScalaBuff for deserializing a union-type container? This would obviate the need for protocol buffer reflection via getAllFields with Java-generated classes. For example, using OneMessage from the protocol buffer documentation :

//not using ScalaBuff
OneMessage.parseFrom(msg).getAllFields.values.asScala.foreach { m =>
  //do something with Foo, Bar, or Baz, e.g. post to Guava EventBus
  bus.post(m)  //m is an AnyRef but EventBus dispatches to correct subscriber
}
@mmaz
Copy link
Author

mmaz commented Aug 20, 2013

My current workaround is to explicitly match on each type. Using the above as an example again:

val bus = new EventBus()
val om = OneMessage.parseFrom(bytes)
List(om.foo, om.bar, om.baz).flatten.map(bus.post)

This works, but it is a bit more laborious when the protobuf definition changes

@SandroGrzicic
Copy link
Owner

You can propose a solution that doesn't impact the generated code size too much and if it sounds good, I'll implement it.

I'm not sure but I think you can maybe use the Shapeless library to treat case classes as functional constructs. Maybe some sort of direct support for Shapeless could be added as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants