add protobuf codecs, change version

This commit is contained in:
dmitry.shakhtarin 2018-07-04 17:25:47 +03:00
parent 6efaac148b
commit 0caafa60b7
3 changed files with 20 additions and 2 deletions

View File

@ -14,7 +14,7 @@ val scalaV = scalaVersion := "2.12.5"
val commons = Seq(
scalaV,
version := "0.0.3",
version := "0.0.4",
fork in Test := true,
parallelExecution in Test := false,
organization := "one.fluence",

View File

@ -18,4 +18,6 @@ addSbtPlugin("com.lihaoyi" % "workbench" % "0.4.0")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.7.1"
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.0.0-M11")

View File

@ -19,6 +19,7 @@ package fluence.codec.pb
import com.google.protobuf.ByteString
import fluence.codec.PureCodec
import scalapb.{GeneratedMessage, GeneratedMessageCompanion, Message}
import scala.language.higherKinds
@ -30,4 +31,19 @@ object ProtobufCodecs {
arr ByteString.copyFrom(arr)
)
/**
* Codec for converting byte array to protobuf class.
*
* @param gen Protobuf class's companion.
* @return New codec for converting byte array to a specific protobuf class.
*/
def protobufDynamicCodec[A <: GeneratedMessage with Message[A]](
gen: GeneratedMessageCompanion[A]
): PureCodec.Func[Array[Byte], A] = PureCodec.liftFunc[Array[Byte], A](gen.parseFrom)
/**
* Codec for converting protobuf class to a byte array.
*/
val generatedMessageCodec: PureCodec.Func[GeneratedMessage, Array[Byte]] =
PureCodec.liftFunc[GeneratedMessage, Array[Byte]](_.toByteArray)
}