我正在尝试使用Protocol Buffers来生成Scala case类。
基本设置为: 1.In 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-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;
}但在目标目录中,它会生成非常奇怪的类"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)
}
};发布于 2021-09-21 14:12:51
它按预期工作。生成的case类具有proto中每个字段的成员。生成代码的其余部分负责序列化和反序列化。您可以在此处阅读有关生成的代码以及如何使用它的更多信息:https://scalapb.github.io/docs/generated-code
https://stackoverflow.com/questions/69268035
复制相似问题