4 Commits

Author SHA1 Message Date
8d905117c8 Kryo codecs example 2018-06-14 13:34:18 +03:00
60661d5555 Codec composition example 2018-06-13 16:45:31 +03:00
e4ab5f147f Few more examples 2018-06-13 16:21:12 +03:00
f23f9dcfab Added PureCodec examples 2018-06-13 13:56:09 +03:00
15 changed files with 387 additions and 86 deletions

View File

@ -2,7 +2,7 @@ sudo: required
language: scala
scala:
- 2.12.8
- 2.12.5
jdk:
- oraclejdk8

View File

@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/fluencelabs/codec.svg?branch=master)](https://travis-ci.org/fluencelabs/codec)
[![Build Status](https://travis-ci.org/fluencelabs/codec.svg?branch=master)](https://travis-ci.org/fluencelabs/codec) [![Gitter](https://badges.gitter.im/fluencelabs/codec.svg)](https://gitter.im/fluencelabs/codec?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
# Codec
@ -44,7 +44,7 @@ In general, functional types conversion could be lazy or eager, be performed in
```scala
import cats.Id
val resEagerSync: Either[CodecError, Array[Byte]] = intToBytes.runF[Id](33)
val resEagerSync: Either[CodecError, Array[Byte]] = intToBytes.direct.runF[Id](33)
```
@ -64,7 +64,7 @@ In case of complex algorithms, it's worthy to share codebase between platforms.
// Bintray repo is used so far. Migration to Maven Central is planned
resolvers += Resolver.bintrayRepo("fluencelabs", "releases")
val codecV = "0.0.4"
val codecV = "0.0.1"
libraryDependencies ++= Seq(
"one.fluence" %%% "codec-core" % codecV, // basic types
@ -121,7 +121,8 @@ libraryDependencies ++= Seq(
// eyJpZCI6MjM0LCJuYW1lIjoiSGV5IEJvYiJ9
```
For more real-world examples, see [Fluence](https://github.com/fluencelabs/fluence).
For synthetic examples refer to the [examples directory](examples/).
For the real-world examples checkout [Fluence](https://github.com/fluencelabs/fluence) main repo.
## Roadmap

View File

@ -29,36 +29,18 @@ object BitsCodecs {
implicit val byteArrayToVector: PureCodec[Array[Byte], ByteVector] =
liftB(ByteVector.apply, _.toArray)
object Base64 {
// Notice the use of default Base64 alphabet
implicit val base64ToVector: PureCodec[String, ByteVector] =
alphabetToVector(Bases.Alphabets.Base64)
// Notice the use of default Base64 alphabet
implicit val base64ToVector: PureCodec[String, ByteVector] =
base64AlphabetToVector(Bases.Alphabets.Base64)
def alphabetToVector(alphabet: Bases.Base64Alphabet): PureCodec[String, ByteVector] =
liftEitherB(
str
ByteVector
.fromBase64Descriptive(str, alphabet)
.left
.map(CodecError(_)),
vec Right(vec.toBase64(alphabet))
)
}
object Base58 {
// Notice the use of default Base64 alphabet
implicit val base58ToVector: PureCodec[String, ByteVector] =
alphabetToVector(Bases.Alphabets.Base58)
def alphabetToVector(alphabet: Bases.Alphabet): PureCodec[String, ByteVector] =
liftEitherB(
str
ByteVector
.fromBase58Descriptive(str, alphabet)
.left
.map(CodecError(_)),
vec Right(vec.toBase58(alphabet))
)
}
def base64AlphabetToVector(alphabet: Bases.Base64Alphabet): PureCodec[String, ByteVector] =
liftEitherB(
str
ByteVector
.fromBase64Descriptive(str, alphabet)
.left
.map(CodecError(_)),
vec Right(vec.toBase64(alphabet))
)
}

View File

@ -22,7 +22,6 @@ import cats.syntax.compose._
import org.scalatest.prop.Checkers
import org.scalatest.{Matchers, WordSpec}
import scodec.bits.ByteVector
import BitsCodecs.Base64._
import BitsCodecs._
import fluence.codec.PureCodec
@ -33,7 +32,7 @@ class BitsCodecsSpec extends WordSpec with Matchers with Checkers {
val arrCodec = implicitly[PureCodec[Array[Byte], ByteVector]]
val b64Codec = implicitly[PureCodec[ByteVector, String]]
check { bytes: List[Byte]
check { (bytes: List[Byte])
(arrCodec andThen arrCodec.swap).direct.apply[Id](bytes.toArray).value.map(_.toList).contains(bytes) &&
(arrCodec andThen b64Codec andThen b64Codec.swap andThen arrCodec.swap).direct
.apply[Id](bytes.toArray)

View File

@ -1,6 +1,6 @@
import de.heikoseeberger.sbtheader.License
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import sbtcrossproject.CrossPlugin.autoImport.crossProject
import sbtcrossproject.crossProject
name := "codec"
@ -10,12 +10,11 @@ javaOptions in Test ++= Seq("-ea")
skip in publish := true // Skip root project
val scalaV = scalaVersion := "2.12.8"
val scalaV = scalaVersion := "2.12.5"
val commons = Seq(
scalaV,
//crossScalaVersions := Seq(scalaVersion.value, "2.13.0-RC1"),
version := "0.0.5",
version := "0.0.3",
fork in Test := true,
parallelExecution in Test := false,
organization := "one.fluence",
@ -31,19 +30,17 @@ val commons = Seq(
commons
val kindProjector = addCompilerPlugin("org.typelevel" % "kind-projector" % "0.10.0" cross CrossVersion.binary)
val kindProjector = addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.6")
val Cats1V = "1.6.0"
val ScodecBitsV = "1.1.10"
val CirceV = "0.11.1"
val Cats1V = "1.1.0"
val ScodecBitsV = "1.1.5"
val CirceV = "0.9.3"
val ShapelessV = "2.3.+"
val chill = "com.twitter" %% "chill" % "0.9.3"
val chill = "com.twitter" %% "chill" % "0.9.2"
val ScalatestV = "3.0.5"
// Note that cats-laws 1.5 are compiled against scalacheck 1.13, and scalacheck-shapeless should also not introduce the upgrade
val ScalacheckV = "1.13.5"
val ScalatestV = "3.0.+"
val ScalacheckV = "1.13.4"
val protobuf = Seq(
PB.targets in Compile := Seq(
@ -65,6 +62,7 @@ lazy val `codec-core` = crossProject(JVMPlatform, JSPlatform)
kindProjector,
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % Cats1V,
"org.typelevel" %%% "cats-laws" % Cats1V % Test,
"org.typelevel" %%% "cats-testkit" % Cats1V % Test,
"com.github.alexarchambault" %%% "scalacheck-shapeless_1.13" % "1.1.8" % Test,
"org.scalacheck" %%% "scalacheck" % ScalacheckV % Test,
@ -150,3 +148,14 @@ lazy val `codec-protobuf` = crossProject(JVMPlatform, JSPlatform)
lazy val `codec-protobuf-jvm` = `codec-protobuf`.jvm
lazy val `codec-protobuf-js` = `codec-protobuf`.js
lazy val `codec-examples` = project
.in(file("examples"))
.settings(
commons,
libraryDependencies ++= Seq(
"io.monix" %%% "monix" % "3.0.0-RC1"
)
)
.dependsOn(`codec-core-jvm`)
.dependsOn(`codec-kryo`)

View File

@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 Fluence Labs Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fluence.codec
import cats.data.Kleisli
import cats.{Applicative, FlatMap, Traverse}
import cats.syntax.applicative._
import scala.language.{higherKinds, implicitConversions}
/**
* Base trait for serialize/deserialize objects.
*
* @tparam A The type of plain object representation
* @tparam B The type of binary representation
* @tparam F Encoding/decoding effect
*/
@deprecated(
"Codec is planned for removing soon, as it's impure and not properly tested. Use PureCodec instead.",
"6.4.2018"
)
final case class Codec[F[_], A, B](encode: A F[B], decode: B F[A]) {
self
implicit val direct: Kleisli[F, A, B] = Kleisli(encode)
implicit val inverse: Kleisli[F, B, A] = Kleisli(decode)
def andThen[C](other: Codec[F, B, C])(implicit F: FlatMap[F]): Codec[F, A, C] =
Codec((self.direct andThen other.direct).run, (other.inverse andThen self.inverse).run)
def compose[C](other: Codec[F, C, A])(implicit F: FlatMap[F]): Codec[F, C, B] =
Codec((other.direct andThen self.direct).run, (self.inverse andThen other.inverse).run)
def swap: Codec[F, B, A] = Codec(decode, encode)
}
@deprecated(
"Codec is planned for removing soon, as it's impure and not properly tested. Use PureCodec instead.",
"6.4.2018"
)
object Codec {
implicit def identityCodec[F[_]: Applicative, T]: Codec[F, T, T] =
Codec(_.pure[F], _.pure[F])
implicit def traverseCodec[F[_]: Applicative, G[_]: Traverse, O, B](
implicit codec: Codec[F, O, B]
): Codec[F, G[O], G[B]] =
Codec[F, G[O], G[B]](Traverse[G].traverse[F, O, B](_)(codec.encode), Traverse[G].traverse[F, B, O](_)(codec.decode))
implicit def toDirect[F[_], A, B](implicit cod: Codec[F, A, B]): Kleisli[F, A, B] =
cod.direct
implicit def toInverse[F[_], A, B](implicit cod: Codec[F, A, B]): Kleisli[F, B, A] =
cod.inverse
implicit def swap[F[_], A, B](implicit cod: Codec[F, A, B]): Codec[F, B, A] =
Codec[F, B, A](cod.decode, cod.encode)
@deprecated(
"Codec is planned for removing soon, as it's impure and not properly tested. Use PureCodec instead.",
"6.4.2018"
)
def codec[F[_], O, B](implicit codec: Codec[F, O, B]): Codec[F, O, B] = codec
/**
* Constructs a Codec from pure encode/decode functions and an Applicative
*
* @param encodeFn Encode function that never fail
* @param decodeFn Decode function that never fail
* @tparam F Applicative effect
* @tparam O Raw type
* @tparam B Encoded type
* @return New codec for O and B
*/
@deprecated(
"Codec is planned for removing soon, as it's impure and not properly tested. Use PureCodec instead.",
"6.4.2018"
)
def pure[F[_]: Applicative, O, B](encodeFn: O B, decodeFn: B O): Codec[F, O, B] =
Codec(encodeFn(_).pure[F], decodeFn(_).pure[F])
}

View File

@ -121,6 +121,13 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
*/
lazy val swap: Bijection[B, A] = Bijection(inverse, direct)
@deprecated(
"You should keep codec Pure until running direct or inverse on it: there's no reason to bind effect into Codec",
"6.4.2018"
)
def toCodec[F[_]](implicit F: MonadError[F, Throwable]): Codec[F, A, B] =
Codec(direct.runF[F], inverse.runF[F])
/**
* Splits the input and puts it to either bijection, then merges output.
* It could have been achieved with `Strong` typeclass in case it doesn't extend `Profunctor`; but it does.
@ -201,7 +208,7 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
* @param f Function to lift
*/
def liftFuncPoint[A, B](f: A Point[B]): Func[A,B] =
new Func[A,B]{
new Func[A, B]{
override def apply[F[_] : Monad](input: A): EitherT[F, E, B] =
f(input).apply[F](())
}

View File

@ -17,14 +17,11 @@
package fluence.codec
import cats.{Applicative, Eq, Invariant}
import cats.{Eq, Invariant}
import cats.data.EitherT
import cats.syntax.functor._
import cats.laws.discipline.{MonadErrorTests, SemigroupalTests}
import cats.tests.CatsSuite
import fluence.codec
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary._
import org.scalacheck.ScalacheckShapeless._
class PureCodecPointLawsSpec extends CatsSuite {
@ -46,7 +43,6 @@ class PureCodecPointLawsSpec extends CatsSuite {
checkAll(
"PureCodec.Point.MonadErrorLaws",
MonadErrorTests[PureCodec.Point, CodecError]
.monadError[Int, String, Double]
MonadErrorTests[PureCodec.Point, CodecError].monadError[Int, String, Double]
)
}

View File

@ -0,0 +1,65 @@
package fluence.codec.examples
import cats.Id
import fluence.codec.kryo.KryoCodecs
import fluence.codec.{CodecError, PureCodec}
import monix.eval.Task
import shapeless.{::, HNil}
object KryoCodecExample {
case class Aircraft(manufacturer: String, model: String, tailNumber: String)
case class Fuel(amount: Double) extends AnyVal
case class UnknownClass(x: String)
def main(args: Array[String]): Unit = {
// This way we can define a typed collection of codecs using kryo for the underlying serialization.
//
// These codecs can be used only to transform the corresponding type: i.e. it won't be possible to
// use an aircraft codec to serialize fuel (which is essentially a typed wrapper over double value).
//
// It won't be possible to obtain from this collection a codec for previously not registered class.
// Type safety FTW!
//
// Note that different methods are used to register Aircraft and Fuel that's because one is a reference,
// and another is a value type.
val codecs: KryoCodecs[Task, ::[Fuel, ::[Aircraft, ::[Array[Byte], ::[Long, ::[String, HNil]]]]]] = KryoCodecs()
.addCase(classOf[Aircraft])
.add[Fuel]
.build[Task]()
val skyhawk61942 = Aircraft("Cessna", "172S G1000", "N61942")
val tabsFuel = Fuel(53)
val aircraftCodec: PureCodec[Aircraft, Array[Byte]] = codecs.pureCodec[Aircraft]
val fuelCodec: PureCodec[Fuel, Array[Byte]] = codecs.pureCodec[Fuel]
// This will cause a compilation error, because the class was never registered with the codecs.
// "You requested an element of type (...).UnknownClass, but there is none in the HList"
//
// val unknownCodec = codecs.pureCodec[UnknownClass]
// Here all the standard machinery of codecs applies (for more examples, consider checking out PureCodecExample.
// We can serialize and deserialize the object and unsurprisingly the original and restored values match.
//
// Let's serialize an aircraft instance.
{
val ser: Id[Either[CodecError, Array[Byte]]] = aircraftCodec.direct[Id](skyhawk61942).value
val deser: Id[Either[CodecError, Aircraft]] = aircraftCodec.inverse[Id](ser.right.get).value
println(ser.right.map(x => s"$skyhawk61942 => serialized size: ${x.length}"))
assert(deser == Right(skyhawk61942))
}
// Same thing for the fuel instance (which is AnyVal fwiw).
{
val ser: Id[Either[CodecError, Array[Byte]]] = fuelCodec.direct[Id](tabsFuel).value
val deser: Id[Either[CodecError, Fuel]] = fuelCodec.inverse[Id](ser.right.get).value
println(ser.right.map(x => s"$tabsFuel => serialized size: ${x.length}"))
assert(deser == Right(tabsFuel))
}
}
}

View File

@ -0,0 +1,138 @@
package fluence.codec.examples
import cats.Id
import cats.data.EitherT
import cats.implicits._
import fluence.codec.PureCodec.{Bijection, Point}
import fluence.codec.{CodecError, PureCodec}
import scala.util.Try
object PureCodecExample {
def main(args: Array[String]): Unit = {
// Here we are defining a simple codec transforming a string to integer and back.
//
// It's not really a bijection: even not taking into account unparseable strings like "test", there are
// different string values (e.g., "+20" and "20") producing the same integer value. It's good enough for
// demonstration purposes though, so we keep using it.
val str2intCodec: Bijection[String, Int] = PureCodec.build[String, Int](
(x: String) => x.toInt,
(x: Int) => x.toString
)
// Using an identity monad, we can parse a valid string into integer (which produces EitherT) and then map
// the result. Now, we can use EitherT[F, E, B] or convert it into F[Either[E, B]] representation.
{
val res: EitherT[Id, CodecError, Int] = str2intCodec.direct[Id]("31330").map(_ + 7)
val resMonad: Id[Either[CodecError, Int]] = res.value
assert(res.toString == "EitherT(Right(31337))")
assert(resMonad.toString == "Right(31337)")
}
// We can also supply a different type class (Monad[F[_]]) in this case the result will be wrapped into
// the corresponding type F[_] using the `F.pure(_)` method.
{
val res = str2intCodec.direct[Option]("42")
val resMonad = res.value
assert(res.toString == "EitherT(Some(Right(42)))")
assert(resMonad.toString == "Some(Right(42))")
}
// Here we attempt to pass an unparseable string value. Note that PureCodec won't catch a thrown exception
// automatically despite that return type is EitherT (this might be a bit confusing). Instead, the exception
// will come all the way up to the caller, which will have to handle it manually.
{
val resWrapped = Try {
val res: EitherT[Id, CodecError, Int] = str2intCodec.direct[Id]("foo")
res
}
assert(resWrapped.toString == "Failure(java.lang.NumberFormatException: For input string: \"foo\")")
}
// To handle exceptions automatically, we can use Try monad. Note that we get `EitherT(Failure(...))`, not
// `EitherT(Failure(Right(...)))` as one might expect by analogy with previous examples. It's not
// `EitherT(Left(...))` too which could have been more convenient potentially.
{
val res = str2intCodec.direct[Try]("foo")
val resMonad: Try[Either[CodecError, Int]] = res.value
assert(res.toString == "EitherT(Failure(java.lang.NumberFormatException: For input string: \"foo\"))")
assert(resMonad.toString == "Failure(java.lang.NumberFormatException: For input string: \"foo\")")
}
// If we really want to receive Left with the exception info when the string argument can't be parsed, a little
// more effort is needed. The problem we had before was that the supplied function `(x: String) => x.toInt`
// could throw parse exceptions and therefore was not really pure.
//
// However, we can catch exceptions in this function and return an Either, which will make it pure. Now, all we
// need to do is to lift this function into the Func context.
val str2intEitherCodec: Bijection[String, Int] = PureCodec.build(
PureCodec.liftFuncEither((x: String) => Either.catchNonFatal(x.toInt).left.map(e => CodecError(e.getMessage))),
PureCodec.liftFuncEither((x: Int) => Either.catchNonFatal(x.toString).left.map(e => CodecError(e.getMessage)))
)
// For lawful strings those which can be parsed into an integer the behavior hasn't really changed.
// Note that we receive Right(...) wrapped in the supplied monadic type.
{
val res: EitherT[Option, CodecError, Int] = str2intEitherCodec.direct[Option]("1024")
val resMonad = res.value
assert(res.toString == "EitherT(Some(Right(1024)))")
assert(resMonad.toString == "Some(Right(1024))")
}
// However, for strings that can't be parsed, we will receive Left(...) which is a desired behavior!
{
val res: EitherT[Option, CodecError, Int] = str2intEitherCodec.direct[Option]("bar")
val resMonad = res.value
assert(res.toString == "EitherT(Some(Left(fluence.codec.CodecError: For input string: \"bar\")))")
assert(resMonad.toString == "Some(Left(fluence.codec.CodecError: For input string: \"bar\"))")
}
// It's also totally possible to perform an inverse transformation: after all, a codec is a bijection.
{
val res: EitherT[Id, CodecError, String] = str2intCodec.inverse[Id](720)
val resMonad: Id[Either[CodecError, String]] = res.value
assert(res.toString == "EitherT(Right(720))")
assert(resMonad.toString == "Right(720)")
}
// It's also possible to pass the to-be-converted value first, but perform the actual transformation only
// later on (using different enclosing monads if desired). To achieve this, `pointAt` method which returns a
// lazily evaluated function can be used.
{
val point: Point[Int] = str2intCodec.direct.pointAt("333")
val resId: EitherT[Id, CodecError, Int] = point[Id]()
val resOption: EitherT[Option, CodecError, Int] = point[Option]()
assert(resId.toString == "EitherT(Right(333))")
assert(resOption.toString == "EitherT(Some(Right(333)))")
}
// Sometimes, we might want to be able to compose two codecs together. Here we define an integer to boolean
// codec and compose it with one of the previously defined codecs. Yes, the int-to-bool codec is not really
// a bijection but we can put up with that for the sake of example.
val int2boolCodec: Bijection[Int, Boolean] = PureCodec.build[Int, Boolean](
(x: Int) => x != 0,
(x: Boolean) => if (x) 1 else 0
)
val str2boolCodec: Bijection[String, Boolean] = str2intCodec andThen int2boolCodec
{
val resA: EitherT[Id, CodecError, Boolean] = str2boolCodec.direct[Id]("100")
val resB = str2boolCodec.inverse[Option](true)
assert(resA.toString == "EitherT(Right(true))")
assert(resB.toString == "EitherT(Some(Right(1)))")
}
// TODO: describe `runF` and `toKleisli`
}
}

View File

@ -17,29 +17,36 @@
package fluence.codec.kryo
import cats.MonadError
import com.twitter.chill.KryoPool
import fluence.codec.{CodecError, PureCodec}
import fluence.codec.{Codec, CodecError, PureCodec}
import shapeless._
import scala.language.higherKinds
import scala.reflect.ClassTag
import scala.util.Try
import scala.util.control.NonFatal
/**
* Wrapper for a KryoPool with a list of registered classes
*
* @param pool Pre-configured KryoPool
* @param F Applicative error
* @tparam L List of classes registered with kryo
* @tparam F Effect
*/
class KryoCodecs[L <: HList] private (pool: KryoPool) {
class KryoCodecs[F[_], L <: HList] private (pool: KryoPool)(implicit F: MonadError[F, Throwable]) {
/**
* Returns a codec for any registered type
*
* @param sel Shows the presence of type T within list L
* @tparam T Object type
* @return Freshly created PureCodec with Kryo inside
* @return Freshly created Codec with Kryo inside
*/
implicit def codec[T](implicit sel: ops.hlist.Selector[L, T]): Codec[F, T, Array[Byte]] =
pureCodec[T].toCodec[F]
implicit def pureCodec[T](implicit sel: ops.hlist.Selector[L, T]): PureCodec[T, Array[Byte]] =
PureCodec.Bijection(
PureCodec.liftFuncEither { input
@ -105,10 +112,10 @@ object KryoCodecs {
* @tparam F Effect type
* @return Configured instance of KryoCodecs
*/
def build(
def build[F[_]](
poolSize: Int = Runtime.getRuntime.availableProcessors
): KryoCodecs[L] =
new KryoCodecs[L](
)(implicit F: MonadError[F, Throwable]): KryoCodecs[F, L] =
new KryoCodecs[F, L](
KryoPool.withByteArrayOutputStream(
poolSize,
KryoFactory(klasses, registrationRequired = true) // registrationRequired should never be needed, as codec derivation is typesafe

View File

@ -21,8 +21,8 @@ import com.twitter.chill.{AllScalaRegistrar, KryoBase, KryoInstantiator}
import org.objenesis.strategy.StdInstantiatorStrategy
/**
* This Instantiator enable compulsory class registration, registers all java and scala main classes.
* This class required for [[com.twitter.chill.KryoPool]].
* This Instantiator enables compulsory class registration and registers all java and scala main classes.
* This class is required for [[com.twitter.chill.KryoPool]].
* @param classesToReg additional classes for registration
* @param registrationRequired if true, an exception is thrown when an unregistered class is encountered.
*/

View File

@ -18,7 +18,6 @@
package fluence.codec.kryo
import cats.instances.try_._
import cats.syntax.profunctor._
import org.scalatest.{Matchers, WordSpec}
import scala.util.Try
@ -32,15 +31,15 @@ class KryoCodecsSpec extends WordSpec with Matchers {
KryoCodecs()
.add[Array[Array[Byte]]]
.addCase(classOf[TestClass])
.build()
.build[Try]()
"encode and decode" should {
"be inverse functions" when {
"object defined" in {
val codec = testCodecs.pureCodec[TestClass]
val codec = testCodecs.codec[TestClass]
val result = codec.inverse.unsafe(codec.direct.unsafe(testClass))
val result = codec.encode(testClass).flatMap(codec.decode).get
result.str shouldBe "one"
result.num shouldBe 2
@ -48,8 +47,8 @@ class KryoCodecsSpec extends WordSpec with Matchers {
}
"object is null" in {
val codec = testCodecs.pureCodec[TestClass]
val result = codec.direct.runF[Try](null).flatMap(codec.inverse.runF[Try])
val codec = testCodecs.codec[TestClass]
val result = codec.encode(null).flatMap(codec.decode)
result.isFailure shouldBe true
}
}
@ -58,8 +57,9 @@ class KryoCodecsSpec extends WordSpec with Matchers {
"encode" should {
"not write full class name to binary representation" when {
"class registered" in {
val codec = testCodecs.pureCodec[TestClass]
val encoded = codec.direct.rmap(new String(_)).unsafe(testClass)
//val codec = KryoCodec[TestClass](Seq(classOf[TestClass], classOf[Array[Byte]], classOf[Array[Array[Byte]]]), registerRequired = true)
val codec = testCodecs.codec[TestClass]
val encoded = codec.encode(testClass).map(new String(_)).get
val reasonableMaxSize = 20 // bytes
encoded should not contain "TestClass"
encoded.length should be < reasonableMaxSize

View File

@ -1 +1 @@
sbt.version = 1.2.8
sbt.version = 1.1.4

View File

@ -1,21 +1,21 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0")
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.4.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.20")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.2")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.2.0")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "4.1.0")
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.20")
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.18")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.27")
addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "0.6.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.22")
addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "0.3.1")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.3.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.14.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.10.0")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.0")
addSbtPlugin("com.lihaoyi" % "workbench" % "0.4.1")
addSbtPlugin("com.lihaoyi" % "workbench" % "0.4.0")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.8.4"
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.7.1"
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")