0

我正在尝试使用协议缓冲区来生成 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)
    }
  };
4

1 回答 1

1

它按预期工作。生成的案例类具有原型中每个字段的成员。生成的其余代码负责序列化和反序列化。您可以在此处阅读有关生成的代码以及如何使用它的更多信息:https ://scalapb.github.io/docs/generated-code

于 2021-09-21T14:12:51.080 回答