title: Scala code generates type mismatch (ScalaPB)
body: <ol>
  <li>I have a protobuf file ...</li>
  <li>which I transform to a Scala file using ScalaPB. This way I can then ...</li>
  <li>use it inside my Juypter notebook* for transformation. Sadly, when I ...</li>
  <li>run the specific cell I get an type
  mismatch error and I don't know why?</li>
  </ol>
  
  <p>As the protobuf file is working with Python and the Scala code is generated, what is not right here? Could this be a bug?</p>
  
  <p>*The notebook uses <code>com.google.protobuf:protobuf-java:3.5.0,com.thesamet.scalapb:sparksql-scalapb_2.11:0.7.0</code> as imports</p>
  
  <h1>Sources &amp; Error</h1>
  
  <h2>protobuf file:</h2>
  
  <pre><code>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;
  }
  </code></pre>
  
  <h2>Scala (generated) code</h2>
  
  <pre><code>// 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 =&gt; _done__ = true
            case 8 =&gt;
              __ts = _input__.readUInt64()
              __requiredFields0 &amp;= 0xfffffffffffffffeL
            case 18 =&gt;
              __data = _input__.readString()
              __requiredFields0 &amp;= 0xfffffffffffffffdL
            case tag =&gt; _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 =&gt; ts
          case 2 =&gt; 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 =&gt; _root_.scalapb.descriptors.PLong(ts)
          case 2 =&gt; _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) =&gt;
        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 _ =&gt; 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_) =&gt; c_.copy(ts = f_))
      def data: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Predef.String] = field(_.data)((c_, f_) =&gt; 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 =&gt; __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 =&gt;
          _output__.writeString(1, __v)
        };
        entry.foreach { __v =&gt;
          _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 =&gt; _done__ = true
            case 10 =&gt;
              __metadata = Option(_input__.readString())
            case 18 =&gt;
              __entry += _root_.scalapb.LiteParser.readMessage(_input__, JSONEntry.defaultInstance)
            case tag =&gt; _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 =&gt; metadata.orNull
          case 2 =&gt; 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 =&gt; metadata.map(_root_.scalapb.descriptors.PString).getOrElse(_root_.scalapb.descriptors.PEmpty)
          case 2 =&gt; _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) =&gt;
        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 _ =&gt; 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 =&gt; __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_) =&gt; c_.copy(metadata = Option(f_)))
      def optionalMetadata: _root_.scalapb.lenses.Lens[UpperPB, scala.Option[_root_.scala.Predef.String]] = field(_.metadata)((c_, f_) =&gt; c_.copy(metadata = f_))
      def entry: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.collection.Seq[JSONEntry]] = field(_.entry)((c_, f_) =&gt; 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
  }
  </code></pre>
  
  <h2>Error</h2>
  
  <pre><code>&lt;console&gt;:82: error: type mismatch;
   found   : JSONEntry.type
   required: scalapb.GeneratedMessageCompanion[_]
             def companion = JSONEntry
                             ^
  </code></pre>