All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.trueaccord.scalapb.GeneratedMessageCompanion.scala Maven / Gradle / Ivy

package com.trueaccord.scalapb

import java.io.{InputStream, OutputStream}

import com.google.protobuf.{ByteString, CodedInputStream, CodedOutputStream}
import com.google.protobuf.Descriptors.{EnumDescriptor, EnumValueDescriptor, FieldDescriptor}

import scala.collection.JavaConversions._
import scala.util.{Failure, Try}

trait GeneratedEnum extends Product with Serializable {
  type EnumType <: GeneratedEnum

  def value: Int

  def index: Int

  def name: String

  override def toString = name

  def companion: GeneratedEnumCompanion[EnumType]

  def valueDescriptor: EnumValueDescriptor = companion.descriptor.getValues.get(index)
}

trait GeneratedEnumCompanion[A <: GeneratedEnum] {
  type ValueType = A
  def fromValue(value: Int): A
  def values: Seq[A]
  def descriptor: EnumDescriptor
}

trait GeneratedOneof extends Product with Serializable {
  def number: Int
  def isDefined: Boolean
  def isEmpty: Boolean
}

trait GeneratedOneofCompanion

trait GeneratedMessage extends Serializable {
  def writeTo(output: CodedOutputStream): Unit

  def writeTo(output: OutputStream): Unit = {
    val bufferSize =
        LiteParser.preferredCodedOutputStreamBufferSize(serializedSize)
    val codedOutput: CodedOutputStream =
        CodedOutputStream.newInstance(output, bufferSize)
    writeTo(codedOutput)
    codedOutput.flush()
  }

  def writeDelimitedTo(output: OutputStream): Unit = {
    val serialized: Int = serializedSize
    val bufferSize: Int = LiteParser.preferredCodedOutputStreamBufferSize(
        CodedOutputStream.computeUInt32SizeNoTag(serialized) + serialized)
    val codedOutput: CodedOutputStream =
        CodedOutputStream.newInstance(output, bufferSize)
    codedOutput.writeUInt32NoTag(serialized)
    writeTo(codedOutput)
    codedOutput.flush()
  }

  def getField(field: FieldDescriptor): Any

  def companion: GeneratedMessageCompanion[_]

  def getAllFields: Map[FieldDescriptor, Any] =
    companion.descriptor.getFields.filter(_.getType != FieldDescriptor.Type.GROUP).flatMap({
      f =>
        getField(f) match {
          case null => None
          case bs: ByteString if bs.isEmpty => Some(f -> bs)
          case Nil => None
          case v => Some(f -> v)
        }
    })(collection.breakOut)

  def toByteArray: Array[Byte] = {
    val a = new Array[Byte](serializedSize)
    val outputStream = CodedOutputStream.newInstance(a)
    writeTo(outputStream)
    outputStream.checkNoSpaceLeft()
    a
  }

  def serializedSize: Int
}

trait Message[A] {
  def mergeFrom(input: CodedInputStream): A
}

trait JavaProtoSupport[ScalaPB, JavaPB] {
  def fromJavaProto(javaProto: JavaPB): ScalaPB

  def toJavaProto(scalaProto: ScalaPB): JavaPB
}

trait GeneratedMessageCompanion[A <: GeneratedMessage with Message[A]] {
  def parseFrom(input: CodedInputStream): A = LiteParser.parseFrom(this, input)

  def parseFrom(input: InputStream): A = parseFrom(CodedInputStream.newInstance(input))

  def parseDelimitedFrom(input: CodedInputStream): Option[A] =
    LiteParser.parseDelimitedFrom(this, input)

  def parseDelimitedFrom(input: InputStream): Option[A] =
    LiteParser.parseDelimitedFrom(this, input)

  // Creates a stream that parses one message at a time from the delimited input stream.
  def streamFromDelimitedInput(input: InputStream): Stream[A] = {
    val codedInputStream = CodedInputStream.newInstance(input)
    Stream.continually(parseDelimitedFrom(codedInputStream)).takeWhile(_.isDefined).map(_.get)
  }

  def parseFrom(s: Array[Byte]): A = parseFrom(CodedInputStream.newInstance(s))

  def validate(s: Array[Byte]): Try[A] = Try(parseFrom(s))

  def toByteArray(a: A): Array[Byte] = a.toByteArray

  def fromFieldsMap(fields: Map[FieldDescriptor, Any]): A

  def descriptor: com.google.protobuf.Descriptors.Descriptor

  def messageCompanionForField(field: FieldDescriptor): GeneratedMessageCompanion[_]

  def enumCompanionForField(field: FieldDescriptor): GeneratedEnumCompanion[_]

  // The ASCII representation is the representation returned by toString. The following
  // functions allow you to convert the ASCII format back to a protocol buffer.  These
  // functions are compatible with the Java implementation in the sense that anything that
  // was generated by TextFormat.printToString can be parsed by these functions.
  def validateAscii(s: String): Either[TextFormatError, A] = TextFormat.fromAscii(this, s)

  def fromAscii(s: String): A = validateAscii(s).fold(
    t => throw new TextFormatException(t.msg), identity[A])

  def defaultInstance: A
}

case class KeyValue[K, V](key: K, value: V) {

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy