scalaprotocol-buffersscalapb

ScalaPB: compilePlugin generate inappropriate class


I'm trying to use Protocol Buffers for generate Scala case classes.

Base settings are: 1.In project/plugins.sbt:

  addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.0")

  libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.10"

2.In build.sbt:

name := "GeoService"

scalaVersion := "2.12.14"

Compile / PB.targets := Seq(
   scalapb.gen() -> (Compile / sourceManaged).value
)

 libraryDependencies ++= Seq(
     "com.thesamet.scalapb" %% "scalapb-runtime" % 
    scalapb.compiler.Version.scalapbVersion % "protobuf"
)

Containt of .proto-file:

 syntax = "proto3";

 package ru.spb.geo.model;

 import "google/protobuf/timestamp.proto";

  message GeoPoint {
       int32 id = 1;
       int32 latitude = 2;
       int32 longitude = 3;
       google.protobuf.Timestamp time = 4;
  }

But in target directory it generates very strange class "GeoPoint" (a part of this "short" class):

@SerialVersionUID(0L)
final case class GeoPoint(
  id: _root_.scala.Int = 0,
  latitude: _root_.scala.Int = 0,
  longitude: _root_.scala.Int = 0,
  time: _root_.scala.Option[com.google.protobuf.timestamp.Timestamp] =
  _root_.scala.None,
       unknownFields: _root_.scalapb.UnknownFieldSet =  
 .scalapb.UnknownFieldSet.empty
      ) extends scalapb.GeneratedMessage with scalapb.lenses.Updatable[GeoPoint] {
     @transient
      private[this] var __serializedSizeCachedValue: _root_.scala.Int = 0
      private[this] def __computeSerializedValue(): _root_.scala.Int = {
         var __size = 0
  
  {
    val __value = id
    if (__value != 0) {
      __size += _root_.com.google.protobuf.CodedOutputStream.computeInt32Size(1, 
 __value)
    }
  };
  
  {
    val __value = latitude
    if (__value != 0) {
      __size += _root_.com.google.protobuf.CodedOutputStream.computeInt32Size(2, 
  __value)
    }
  };

Solution

  • It works as intended. The case class generated has members for each of the fields in the proto. The rest of the generated code takes care of serialization and deserialization. You can read more about the generated code and how to use it here: https://scalapb.github.io/docs/generated-code