Scala代码生成类型不匹配(ScalaPB)

时间:2018-03-05 21:07:20

标签: scala scalapb

  1. 我有一个protobuf文件......
  2. 我使用ScalaPB转换为Scala文件。这样我就可以......
  3. 在我的Juypter笔记本*中使用它进行转换。可悲的是,当我...
  4. 运行特定的单元格我得到一个类型 错配错误,我不知道为什么?
  5. 由于protobuf文件正在使用Python并且生成了Scala代码,这里有什么不正确的?这可能是个错误吗?

    *笔记本使用com.google.protobuf:protobuf-java:3.5.0,com.thesamet.scalapb:sparksql-scalapb_2.11:0.7.0作为导入

    来源&错误

    protobuf文件:

    syntax = "proto2";
    
    import "scalapb/scalapb.proto";
    
    option (scalapb.options) = {
      flat_package: true
      single_file: true
    };
    
    message JSONEntry {
      required uint64 ts = 1;
      required string data = 2;
    }
    
    message JSONOutput {
      optional string metadata = 1;
      repeated JSONEntry entry = 2;
    }
    

    Scala(生成)代码

    // Generated by the Scala Plugin for the Protocol Buffer Compiler.
    // Do not edit!
    //
    // Protofile syntax: PROTO2
    
    
    @SerialVersionUID(0L)
    final case class JSONEntry(
        ts: _root_.scala.Long,
        data: _root_.scala.Predef.String
        ) extends scalapb.GeneratedMessage with scalapb.Message[JSONEntry] with scalapb.lenses.Updatable[JSONEntry] {
        @transient
        private[this] var __serializedSizeCachedValue: _root_.scala.Int = 0
        private[this] def __computeSerializedValue(): _root_.scala.Int = {
          var __size = 0
          __size += _root_.com.google.protobuf.CodedOutputStream.computeUInt64Size(1, ts)
          __size += _root_.com.google.protobuf.CodedOutputStream.computeStringSize(2, data)
          __size
        }
        final override def serializedSize: _root_.scala.Int = {
          var read = __serializedSizeCachedValue
          if (read == 0) {
            read = __computeSerializedValue()
            __serializedSizeCachedValue = read
          }
          read
        }
        def writeTo(`_output__`: _root_.com.google.protobuf.CodedOutputStream): Unit = {
          _output__.writeUInt64(1, ts)
          _output__.writeString(2, data)
        }
        def mergeFrom(`_input__`: _root_.com.google.protobuf.CodedInputStream): JSONEntry = {
          var __ts = this.ts
          var __data = this.data
          var __requiredFields0: _root_.scala.Long = 0x3L
          var _done__ = false
          while (!_done__) {
            val _tag__ = _input__.readTag()
            _tag__ match {
              case 0 => _done__ = true
              case 8 =>
                __ts = _input__.readUInt64()
                __requiredFields0 &= 0xfffffffffffffffeL
              case 18 =>
                __data = _input__.readString()
                __requiredFields0 &= 0xfffffffffffffffdL
              case tag => _input__.skipField(tag)
            }
          }
          if (__requiredFields0 != 0L) { throw new _root_.com.google.protobuf.InvalidProtocolBufferException("Message missing required fields.") } 
          JSONEntry(
              ts = __ts,
              data = __data
          )
        }
        def withTs(__v: _root_.scala.Long): JSONEntry = copy(ts = __v)
        def withData(__v: _root_.scala.Predef.String): JSONEntry = copy(data = __v)
        def getFieldByNumber(__fieldNumber: _root_.scala.Int): scala.Any = {
          (__fieldNumber: @_root_.scala.unchecked) match {
            case 1 => ts
            case 2 => data
          }
        }
        def getField(__field: _root_.scalapb.descriptors.FieldDescriptor): _root_.scalapb.descriptors.PValue = {
          require(__field.containingMessage eq companion.scalaDescriptor)
          (__field.number: @_root_.scala.unchecked) match {
            case 1 => _root_.scalapb.descriptors.PLong(ts)
            case 2 => _root_.scalapb.descriptors.PString(data)
          }
        }
        def toProtoString: _root_.scala.Predef.String = _root_.scalapb.TextFormat.printToUnicodeString(this)
        def companion = JSONEntry
    }
    
    object JSONEntry extends scalapb.GeneratedMessageCompanion[JSONEntry] {
      implicit def messageCompanion: scalapb.GeneratedMessageCompanion[JSONEntry] = this
      def fromFieldsMap(__fieldsMap: scala.collection.immutable.Map[_root_.com.google.protobuf.Descriptors.FieldDescriptor, scala.Any]): JSONEntry = {
        require(__fieldsMap.keys.forall(_.getContainingType() == javaDescriptor), "FieldDescriptor does not match message type.")
        val __fields = javaDescriptor.getFields
        JSONEntry(
          __fieldsMap(__fields.get(0)).asInstanceOf[_root_.scala.Long],
          __fieldsMap(__fields.get(1)).asInstanceOf[_root_.scala.Predef.String]
        )
      }
      implicit def messageReads: _root_.scalapb.descriptors.Reads[JSONEntry] = _root_.scalapb.descriptors.Reads{
        case _root_.scalapb.descriptors.PMessage(__fieldsMap) =>
          require(__fieldsMap.keys.forall(_.containingMessage == scalaDescriptor), "FieldDescriptor does not match message type.")
          JSONEntry(
            __fieldsMap.get(scalaDescriptor.findFieldByNumber(1).get).get.as[_root_.scala.Long],
            __fieldsMap.get(scalaDescriptor.findFieldByNumber(2).get).get.as[_root_.scala.Predef.String]
          )
        case _ => throw new RuntimeException("Expected PMessage")
      }
      def javaDescriptor: _root_.com.google.protobuf.Descriptors.Descriptor = DataProto.javaDescriptor.getMessageTypes.get(0)
      def scalaDescriptor: _root_.scalapb.descriptors.Descriptor = DataProto.scalaDescriptor.messages(0)
      def messageCompanionForFieldNumber(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[_] = throw new MatchError(__number)
      lazy val nestedMessagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_]] = Seq.empty
      def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[_] = throw new MatchError(__fieldNumber)
      lazy val defaultInstance = JSONEntry(
        ts = 0L,
        data = ""
      )
      implicit class JSONEntryLens[UpperPB](_l: _root_.scalapb.lenses.Lens[UpperPB, JSONEntry]) extends _root_.scalapb.lenses.ObjectLens[UpperPB, JSONEntry](_l) {
        def ts: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Long] = field(_.ts)((c_, f_) => c_.copy(ts = f_))
        def data: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Predef.String] = field(_.data)((c_, f_) => c_.copy(data = f_))
      }
      final val TS_FIELD_NUMBER = 1
      final val DATA_FIELD_NUMBER = 2
    }
    
    @SerialVersionUID(0L)
    final case class JSONOutput(
        metadata: scala.Option[_root_.scala.Predef.String] = None,
        entry: _root_.scala.collection.Seq[JSONEntry] = _root_.scala.collection.Seq.empty
        ) extends scalapb.GeneratedMessage with scalapb.Message[JSONOutput] with scalapb.lenses.Updatable[JSONOutput] {
        @transient
        private[this] var __serializedSizeCachedValue: _root_.scala.Int = 0
        private[this] def __computeSerializedValue(): _root_.scala.Int = {
          var __size = 0
          if (metadata.isDefined) { __size += _root_.com.google.protobuf.CodedOutputStream.computeStringSize(1, metadata.get) }
          entry.foreach(entry => __size += 1 + _root_.com.google.protobuf.CodedOutputStream.computeUInt32SizeNoTag(entry.serializedSize) + entry.serializedSize)
          __size
        }
        final override def serializedSize: _root_.scala.Int = {
          var read = __serializedSizeCachedValue
          if (read == 0) {
            read = __computeSerializedValue()
            __serializedSizeCachedValue = read
          }
          read
        }
        def writeTo(`_output__`: _root_.com.google.protobuf.CodedOutputStream): Unit = {
          metadata.foreach { __v =>
            _output__.writeString(1, __v)
          };
          entry.foreach { __v =>
            _output__.writeTag(2, 2)
            _output__.writeUInt32NoTag(__v.serializedSize)
            __v.writeTo(_output__)
          };
        }
        def mergeFrom(`_input__`: _root_.com.google.protobuf.CodedInputStream): JSONOutput = {
          var __metadata = this.metadata
          val __entry = (_root_.scala.collection.immutable.Vector.newBuilder[JSONEntry] ++= this.entry)
          var _done__ = false
          while (!_done__) {
            val _tag__ = _input__.readTag()
            _tag__ match {
              case 0 => _done__ = true
              case 10 =>
                __metadata = Option(_input__.readString())
              case 18 =>
                __entry += _root_.scalapb.LiteParser.readMessage(_input__, JSONEntry.defaultInstance)
              case tag => _input__.skipField(tag)
            }
          }
          JSONOutput(
              metadata = __metadata,
              entry = __entry.result()
          )
        }
        def getMetadata: _root_.scala.Predef.String = metadata.getOrElse("")
        def clearMetadata: JSONOutput = copy(metadata = None)
        def withMetadata(__v: _root_.scala.Predef.String): JSONOutput = copy(metadata = Option(__v))
        def clearEntry = copy(entry = _root_.scala.collection.Seq.empty)
        def addEntry(__vs: JSONEntry*): JSONOutput = addAllEntry(__vs)
        def addAllEntry(__vs: TraversableOnce[JSONEntry]): JSONOutput = copy(entry = entry ++ __vs)
        def withEntry(__v: _root_.scala.collection.Seq[JSONEntry]): JSONOutput = copy(entry = __v)
        def getFieldByNumber(__fieldNumber: _root_.scala.Int): scala.Any = {
          (__fieldNumber: @_root_.scala.unchecked) match {
            case 1 => metadata.orNull
            case 2 => entry
          }
        }
        def getField(__field: _root_.scalapb.descriptors.FieldDescriptor): _root_.scalapb.descriptors.PValue = {
          require(__field.containingMessage eq companion.scalaDescriptor)
          (__field.number: @_root_.scala.unchecked) match {
            case 1 => metadata.map(_root_.scalapb.descriptors.PString).getOrElse(_root_.scalapb.descriptors.PEmpty)
            case 2 => _root_.scalapb.descriptors.PRepeated(entry.map(_.toPMessage)(_root_.scala.collection.breakOut))
          }
        }
        def toProtoString: _root_.scala.Predef.String = _root_.scalapb.TextFormat.printToUnicodeString(this)
        def companion = JSONOutput
    }
    
    object JSONOutput extends scalapb.GeneratedMessageCompanion[JSONOutput] {
      implicit def messageCompanion: scalapb.GeneratedMessageCompanion[JSONOutput] = this
      def fromFieldsMap(__fieldsMap: scala.collection.immutable.Map[_root_.com.google.protobuf.Descriptors.FieldDescriptor, scala.Any]): JSONOutput = {
        require(__fieldsMap.keys.forall(_.getContainingType() == javaDescriptor), "FieldDescriptor does not match message type.")
        val __fields = javaDescriptor.getFields
        JSONOutput(
          __fieldsMap.get(__fields.get(0)).asInstanceOf[scala.Option[_root_.scala.Predef.String]],
          __fieldsMap.getOrElse(__fields.get(1), Nil).asInstanceOf[_root_.scala.collection.Seq[JSONEntry]]
        )
      }
      implicit def messageReads: _root_.scalapb.descriptors.Reads[JSONOutput] = _root_.scalapb.descriptors.Reads{
        case _root_.scalapb.descriptors.PMessage(__fieldsMap) =>
          require(__fieldsMap.keys.forall(_.containingMessage == scalaDescriptor), "FieldDescriptor does not match message type.")
          JSONOutput(
            __fieldsMap.get(scalaDescriptor.findFieldByNumber(1).get).flatMap(_.as[scala.Option[_root_.scala.Predef.String]]),
            __fieldsMap.get(scalaDescriptor.findFieldByNumber(2).get).map(_.as[_root_.scala.collection.Seq[JSONEntry]]).getOrElse(_root_.scala.collection.Seq.empty)
          )
        case _ => throw new RuntimeException("Expected PMessage")
      }
      def javaDescriptor: _root_.com.google.protobuf.Descriptors.Descriptor = DataProto.javaDescriptor.getMessageTypes.get(1)
      def scalaDescriptor: _root_.scalapb.descriptors.Descriptor = DataProto.scalaDescriptor.messages(1)
      def messageCompanionForFieldNumber(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[_] = {
        var __out: _root_.scalapb.GeneratedMessageCompanion[_] = null
        (__number: @_root_.scala.unchecked) match {
          case 2 => __out = JSONEntry
        }
        __out
      }
      lazy val nestedMessagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_]] = Seq.empty
      def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[_] = throw new MatchError(__fieldNumber)
      lazy val defaultInstance = JSONOutput(
      )
      implicit class JSONOutputLens[UpperPB](_l: _root_.scalapb.lenses.Lens[UpperPB, JSONOutput]) extends _root_.scalapb.lenses.ObjectLens[UpperPB, JSONOutput](_l) {
        def metadata: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Predef.String] = field(_.getMetadata)((c_, f_) => c_.copy(metadata = Option(f_)))
        def optionalMetadata: _root_.scalapb.lenses.Lens[UpperPB, scala.Option[_root_.scala.Predef.String]] = field(_.metadata)((c_, f_) => c_.copy(metadata = f_))
        def entry: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.collection.Seq[JSONEntry]] = field(_.entry)((c_, f_) => c_.copy(entry = f_))
      }
      final val METADATA_FIELD_NUMBER = 1
      final val ENTRY_FIELD_NUMBER = 2
    }
    
    object DataProto extends _root_.scalapb.GeneratedFileObject {
      lazy val dependencies: Seq[_root_.scalapb.GeneratedFileObject] = Seq(
        scalapb.options.ScalapbProto
      )
      lazy val messagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_]] = Seq(
        JSONEntry,
        JSONOutput
      )
      private lazy val ProtoBytes: Array[Byte] =
          scalapb.Encoding.fromBase64(scala.collection.Seq(
      """CgpkYXRhLnByb3RvGhVzY2FsYXBiL3NjYWxhcGIucHJvdG8iLwoJSlNPTkVudHJ5Eg4KAnRzGAEgAigEUgJ0cxISCgRkYXRhG
      AIgAigJUgRkYXRhIkoKCkpTT05PdXRwdXQSGgoIbWV0YWRhdGEYASABKAlSCG1ldGFkYXRhEiAKBWVudHJ5GAIgAygLMgouSlNPT
      kVudHJ5UgVlbnRyeUIH4j8EEAEoAQ=="""
          ).mkString)
      lazy val scalaDescriptor: _root_.scalapb.descriptors.FileDescriptor = {
        val scalaProto = com.google.protobuf.descriptor.FileDescriptorProto.parseFrom(ProtoBytes)
        _root_.scalapb.descriptors.FileDescriptor.buildFrom(scalaProto, dependencies.map(_.scalaDescriptor))
      }
      lazy val javaDescriptor: com.google.protobuf.Descriptors.FileDescriptor = {
        val javaProto = com.google.protobuf.DescriptorProtos.FileDescriptorProto.parseFrom(ProtoBytes)
        com.google.protobuf.Descriptors.FileDescriptor.buildFrom(javaProto, Array(
          scalapb.options.ScalapbProto.javaDescriptor
        ))
      }
      @deprecated("Use javaDescriptor instead. In a future version this will refer to scalaDescriptor.", "ScalaPB 0.5.47")
      def descriptor: com.google.protobuf.Descriptors.FileDescriptor = javaDescriptor
    }
    

    错误

    <console>:82: error: type mismatch;
     found   : JSONEntry.type
     required: scalapb.GeneratedMessageCompanion[_]
               def companion = JSONEntry
                               ^
    

1 个答案:

答案 0 :(得分:2)

我能够使用以下代码

成功编译您的proto文件

项目/ scalapb.sbt

addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.16")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.7.0"

build.sbt

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization := "com.example",
      scalaVersion := "2.12.4",
      version      := "0.1.0-SNAPSHOT"
    )),
    name := "protobuf",
    libraryDependencies ++= Seq(
      "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
     ),
    PB.targets in (Compile) := Seq(
      scalapb.gen() -> (sourceManaged in Compile).value
    )
 )

现在,如果您将照片文件复制并粘贴到src/main/protobuf hello.proto并执行sbt clean compile

我做的唯一不同的是我在照片文件中添加了一个包

syntax = "proto2";

import "scalapb/scalapb.proto";

option (scalapb.options) = {
  package_name: "com.abhi"
  flat_package: true
  single_file: true
};

message JSONEntry {
  required uint64 ts = 1;
  required string data = 2;
}

message JSONOutput {
  optional string metadata = 1;
  repeated JSONEntry entry = 2;
}

现在最终在您的应用中使用生成的代码

包示例 import com.abhi.JSONEntry import java.io ._

object Hello extends App {
  val jsonEntry = JSONEntry(10L, "foo")
  val target = new FileOutputStream(new File("foo.bin"))
  jsonEntry.writeTo(target)
  target.close()
}

代码编译正确且没有编译错误