mirror of
https://github.com/fluencelabs/codec
synced 2025-04-25 14:52:14 +00:00
scalafmt reformat (#91)
* scalafmt config * scalafmt reformat * revert scaladoc to javadoc * danglingParentheses = true * delete scalariform prefs * delete scalariform imports * disabling test
This commit is contained in:
parent
250e4f1371
commit
f0fa6a16e1
@ -18,11 +18,11 @@
|
||||
package fluence.codec
|
||||
|
||||
import cats.data.Kleisli
|
||||
import cats.{ Applicative, ApplicativeError, FlatMap, Traverse }
|
||||
import cats.{Applicative, ApplicativeError, FlatMap, Traverse}
|
||||
import cats.syntax.applicative._
|
||||
import scodec.bits.ByteVector
|
||||
|
||||
import scala.language.{ higherKinds, implicitConversions }
|
||||
import scala.language.{higherKinds, implicitConversions}
|
||||
|
||||
/**
|
||||
* Base trait for serialize/deserialize objects.
|
||||
@ -48,10 +48,12 @@ final case class Codec[F[_], A, B](encode: A ⇒ F[B], decode: B ⇒ F[A]) {
|
||||
}
|
||||
|
||||
object Codec {
|
||||
implicit def identityCodec[F[_] : Applicative, T]: Codec[F, T, T] =
|
||||
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]] =
|
||||
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] =
|
||||
@ -63,17 +65,18 @@ object Codec {
|
||||
implicit def swap[F[_], A, B](implicit cod: Codec[F, A, B]): Codec[F, B, A] =
|
||||
Codec[F, B, A](cod.decode, cod.encode)
|
||||
|
||||
implicit def byteVectorArray[F[_] : Applicative]: Codec[F, Array[Byte], ByteVector] =
|
||||
implicit def byteVectorArray[F[_]: Applicative]: Codec[F, Array[Byte], ByteVector] =
|
||||
pure(ByteVector.apply, _.toArray)
|
||||
|
||||
// TODO: descriptive error
|
||||
implicit def byteVectorB64[F[_]](implicit F: ApplicativeError[F, Throwable]): Codec[F, String, ByteVector] =
|
||||
Codec(
|
||||
str ⇒
|
||||
ByteVector.fromBase64(str).fold[F[ByteVector]](
|
||||
F.raiseError(new IllegalArgumentException(s"Given string is not valid b64: $str"))
|
||||
)(_.pure[F]),
|
||||
|
||||
ByteVector
|
||||
.fromBase64(str)
|
||||
.fold[F[ByteVector]](
|
||||
F.raiseError(new IllegalArgumentException(s"Given string is not valid b64: $str"))
|
||||
)(_.pure[F]),
|
||||
_.toBase64.pure[F]
|
||||
)
|
||||
|
||||
@ -89,6 +92,6 @@ object Codec {
|
||||
* @tparam B Encoded type
|
||||
* @return New codec for O and B
|
||||
*/
|
||||
def pure[F[_] : Applicative, O, B](encodeFn: O ⇒ B, decodeFn: B ⇒ O): Codec[F, O, B] =
|
||||
def pure[F[_]: Applicative, O, B](encodeFn: O ⇒ B, decodeFn: B ⇒ O): Codec[F, O, B] =
|
||||
Codec(encodeFn(_).pure[F], decodeFn(_).pure[F])
|
||||
}
|
||||
|
@ -43,15 +43,18 @@ class KryoCodecs[F[_], L <: HList] private (pool: KryoPool)(implicit F: MonadErr
|
||||
*/
|
||||
implicit def codec[T](implicit sel: ops.hlist.Selector[L, T]): Codec[F, T, Array[Byte]] =
|
||||
Codec(
|
||||
obj ⇒ Option(obj) match {
|
||||
case Some(o) ⇒
|
||||
F.catchNonFatal(Option(pool.toBytesWithClass(o))).flatMap {
|
||||
case Some(v) ⇒ F.pure(v)
|
||||
case None ⇒ F.raiseError(new NullPointerException("Obj is encoded into null"))
|
||||
}
|
||||
case None ⇒
|
||||
F.raiseError[Array[Byte]](new NullPointerException("Obj is null, encoding is impossible"))
|
||||
}, binary ⇒ F.catchNonFatal(pool.fromBytes(binary).asInstanceOf[T]))
|
||||
obj ⇒
|
||||
Option(obj) match {
|
||||
case Some(o) ⇒
|
||||
F.catchNonFatal(Option(pool.toBytesWithClass(o))).flatMap {
|
||||
case Some(v) ⇒ F.pure(v)
|
||||
case None ⇒ F.raiseError(new NullPointerException("Obj is encoded into null"))
|
||||
}
|
||||
case None ⇒
|
||||
F.raiseError[Array[Byte]](new NullPointerException("Obj is null, encoding is impossible"))
|
||||
},
|
||||
binary ⇒ F.catchNonFatal(pool.fromBytes(binary).asInstanceOf[T])
|
||||
)
|
||||
}
|
||||
|
||||
object KryoCodecs {
|
||||
@ -62,6 +65,7 @@ object KryoCodecs {
|
||||
* @tparam L List of registered classes
|
||||
*/
|
||||
class Builder[L <: HList] private[KryoCodecs] (klasses: Seq[Class[_]]) {
|
||||
|
||||
/**
|
||||
* Register a new case class T to Kryo
|
||||
* @tparam T Type to add
|
||||
@ -70,7 +74,9 @@ object KryoCodecs {
|
||||
* @param sa Presence of all types of S inside L
|
||||
* @return Extended builder
|
||||
*/
|
||||
def addCase[T, S <: HList](klass: Class[T])(implicit gen: Generic.Aux[T, S], sa: ops.hlist.SelectAll[L, S]): Builder[T :: L] =
|
||||
def addCase[T, S <: HList](
|
||||
klass: Class[T]
|
||||
)(implicit gen: Generic.Aux[T, S], sa: ops.hlist.SelectAll[L, S]): Builder[T :: L] =
|
||||
new Builder[T :: L](klasses :+ klass)
|
||||
|
||||
/**
|
||||
@ -78,7 +84,7 @@ object KryoCodecs {
|
||||
* @tparam T Type to add
|
||||
* @return Extended builder
|
||||
*/
|
||||
def add[T : ClassTag]: Builder[T :: L] =
|
||||
def add[T: ClassTag]: Builder[T :: L] =
|
||||
new Builder[T :: L](klasses :+ implicitly[ClassTag[T]].runtimeClass)
|
||||
|
||||
/**
|
||||
@ -88,7 +94,9 @@ object KryoCodecs {
|
||||
* @tparam F Effect type
|
||||
* @return Configured instance of KryoCodecs
|
||||
*/
|
||||
def build[F[_]](poolSize: Int = Runtime.getRuntime.availableProcessors)(implicit F: MonadError[F, Throwable]): KryoCodecs[F, L] =
|
||||
def build[F[_]](
|
||||
poolSize: Int = Runtime.getRuntime.availableProcessors
|
||||
)(implicit F: MonadError[F, Throwable]): KryoCodecs[F, L] =
|
||||
new KryoCodecs[F, L](
|
||||
KryoPool.withByteArrayOutputStream(
|
||||
poolSize,
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
package fluence.codec.kryo
|
||||
|
||||
import com.twitter.chill.{ AllScalaRegistrar, KryoBase, KryoInstantiator }
|
||||
import com.twitter.chill.{AllScalaRegistrar, KryoBase, KryoInstantiator}
|
||||
import org.objenesis.strategy.StdInstantiatorStrategy
|
||||
|
||||
/**
|
||||
@ -26,7 +26,8 @@ import org.objenesis.strategy.StdInstantiatorStrategy
|
||||
* @param classesToReg additional classes for registration
|
||||
* @param registrationRequired if true, an exception is thrown when an unregistered class is encountered.
|
||||
*/
|
||||
private[kryo] case class KryoFactory(classesToReg: Seq[Class[_]], registrationRequired: Boolean) extends KryoInstantiator {
|
||||
private[kryo] case class KryoFactory(classesToReg: Seq[Class[_]], registrationRequired: Boolean)
|
||||
extends KryoInstantiator {
|
||||
|
||||
override def newKryo(): KryoBase = {
|
||||
val kryo = new KryoBase()
|
||||
|
@ -18,7 +18,7 @@
|
||||
package fluence.codec.kryo
|
||||
|
||||
import cats.instances.try_._
|
||||
import org.scalatest.{ Matchers, WordSpec }
|
||||
import org.scalatest.{Matchers, WordSpec}
|
||||
|
||||
import scala.util.Try
|
||||
|
||||
@ -30,7 +30,8 @@ class KryoCodecsSpec extends WordSpec with Matchers {
|
||||
private val testCodecs =
|
||||
KryoCodecs()
|
||||
.add[Array[Array[Byte]]]
|
||||
.addCase(classOf[TestClass]).build[Try]()
|
||||
.addCase(classOf[TestClass])
|
||||
.build[Try]()
|
||||
|
||||
"encode and decode" should {
|
||||
"be inverse functions" when {
|
||||
@ -69,4 +70,3 @@ class KryoCodecsSpec extends WordSpec with Matchers {
|
||||
}
|
||||
|
||||
case class TestClass(str: String, num: Long, blob: Array[Array[Byte]])
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
package fluence.codec.pb
|
||||
|
||||
import cats.{ Applicative, MonadError }
|
||||
import cats.{Applicative, MonadError}
|
||||
import com.google.protobuf.ByteString
|
||||
import fluence.codec.Codec
|
||||
import fluence.kad.protocol.Key
|
||||
@ -27,7 +27,7 @@ import scala.language.higherKinds
|
||||
|
||||
object ProtobufCodecs {
|
||||
|
||||
implicit def byteVectorByteString[F[_] : Applicative]: Codec[F, ByteString, ByteVector] =
|
||||
implicit def byteVectorByteString[F[_]: Applicative]: Codec[F, ByteString, ByteVector] =
|
||||
Codec.pure(
|
||||
str ⇒ ByteVector(str.toByteArray),
|
||||
vec ⇒ ByteString.copyFrom(vec.toArray)
|
||||
|
Loading…
x
Reference in New Issue
Block a user