我正在尝试使用协议缓冲区来生成 Scala 案例类。
基本设置是: 1.在 project/plugins.sbt 中:
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.0")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.10"
2.在 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"
)
.proto 文件的内容:
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;
}
但是在目标目录中它会生成非常奇怪的类“GeoPoint”(这个“短”类的一部分):
@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)
}
};