Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To create a Scala case class from a Protocol Buffer Compiler generated code, you could use a library such as "ScalaPB". Here are the steps to follow:

  1. Install the ScalaPB plugin for sbt build tool by adding the following line to your "plugins.sbt" file:
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.1")
  1. Add the ScalaPB dependency to your build.sbt file:
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.1"
  1. Create a .proto file that defines your message types:
syntax = "proto3";

package mypackage;

message MyMessage {
  int32 id = 1;
  string name = 2;
}
  1. Compile the .proto file using the sbt-protoc plugin:
sbt compile
  1. The ScalaPB plugin should generate a file called "MyMessage.scala" in your "target/scala-2.12/src_managed/main" directory. This file will contain a case class definition for your message:
package mypackage

case class MyMessage(
  id: Int = 0,
  name: String = ""
)
  1. You can now use the MyMessage case class like any other Scala class:
val message = MyMessage(1, "John")
println(message.id) // prints 1
println(message.name) // prints "John"