Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf: how to use existing case classes

There are case classes which are already used in the project. These classes are used in the slick-mapping too. And these classes extends some additional traits.

I don't want to generate all these classes from *.proto description.

Is there an opportunity to extend them in protobuf? Or should I use a wrappers for them. And these wrappers will be described in *.proto and generated from it.

like image 394
John Mullins Avatar asked Nov 08 '22 09:11

John Mullins


1 Answers

For proto definition

message PBPerson {
  int64 id = 1;
  string name = 2;
  google.protobuf.StringValue phone = 3;
  repeated string hobbies = 4;
}

and scala case class definition

case class Person(
  id: Long,
  name: String,
  phone: Option[String],
  hobbies: Seq[String])

you can use https://github.com/changvvb/scala-protobuf-java

import pbconverts.{ Protoable, Scalable }
val convertedPBPerson:PBPerson = Protoable[Person,PBPerson].toProto(person)
val convertedPerson:Person = Scalable[Person,PBPerson].toScala(pbPerson)

Additionally, this lib uses scala macro to ensure it's a type-safe conversion.

like image 86
V Chang Avatar answered Nov 15 '22 09:11

V Chang