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

scalapb.AnyMethods.scala Maven / Gradle / Ivy

package scalapb

import com.google.protobuf.ByteString

trait AnyMethods {
  def typeUrl: String
  def value: ByteString

  def is[A <: GeneratedMessage](implicit cmp: GeneratedMessageCompanion[A]) = {
    AnyMethods.typeNameFromTypeUrl(typeUrl) == cmp.scalaDescriptor.fullName
  }

  def unpack[A <: GeneratedMessage](implicit cmp: GeneratedMessageCompanion[A]) = {
    require(is[A], s"Type of the Any message does not match the given class.")
    cmp.parseFrom(value.newCodedInput())
  }
}

object AnyMethods {
  private def typeNameFromTypeUrl(typeUrl: String): String = {
    typeUrl.split("/").lastOption.getOrElse(typeUrl)
  }
}

trait AnyCompanionMethods {
  def pack[A <: GeneratedMessage](
      generatedMessage: A
  ): com.google.protobuf.any.Any =
    pack(generatedMessage, "type.googleapis.com/")

  def pack[A <: GeneratedMessage](
      generatedMessage: A,
      urlPrefix: String
  ): com.google.protobuf.any.Any =
    com.google.protobuf.any.Any(
      typeUrl =
        if (urlPrefix.endsWith("/"))
          urlPrefix + generatedMessage.companion.scalaDescriptor.fullName
        else
          urlPrefix + "/" + generatedMessage.companion.scalaDescriptor.fullName,
      value = generatedMessage.toByteString
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy