mirror of
https://github.com/fluencelabs/codec
synced 2025-04-25 06:42:14 +00:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
cf36fdb00e | ||
|
70dae6edca | ||
|
3c4a7fc722 |
@ -29,18 +29,36 @@ object BitsCodecs {
|
|||||||
implicit val byteArrayToVector: PureCodec[Array[Byte], ByteVector] =
|
implicit val byteArrayToVector: PureCodec[Array[Byte], ByteVector] =
|
||||||
liftB(ByteVector.apply, _.toArray)
|
liftB(ByteVector.apply, _.toArray)
|
||||||
|
|
||||||
// Notice the use of default Base64 alphabet
|
object Base64 {
|
||||||
implicit val base64ToVector: PureCodec[String, ByteVector] =
|
// Notice the use of default Base64 alphabet
|
||||||
base64AlphabetToVector(Bases.Alphabets.Base64)
|
implicit val base64ToVector: PureCodec[String, ByteVector] =
|
||||||
|
alphabetToVector(Bases.Alphabets.Base64)
|
||||||
|
|
||||||
def base64AlphabetToVector(alphabet: Bases.Base64Alphabet): PureCodec[String, ByteVector] =
|
def alphabetToVector(alphabet: Bases.Base64Alphabet): PureCodec[String, ByteVector] =
|
||||||
liftEitherB(
|
liftEitherB(
|
||||||
str ⇒
|
str ⇒
|
||||||
ByteVector
|
ByteVector
|
||||||
.fromBase64Descriptive(str, alphabet)
|
.fromBase64Descriptive(str, alphabet)
|
||||||
.left
|
.left
|
||||||
.map(CodecError(_)),
|
.map(CodecError(_)),
|
||||||
vec ⇒ Right(vec.toBase64(alphabet))
|
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))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import cats.syntax.compose._
|
|||||||
import org.scalatest.prop.Checkers
|
import org.scalatest.prop.Checkers
|
||||||
import org.scalatest.{Matchers, WordSpec}
|
import org.scalatest.{Matchers, WordSpec}
|
||||||
import scodec.bits.ByteVector
|
import scodec.bits.ByteVector
|
||||||
|
import BitsCodecs.Base64._
|
||||||
import BitsCodecs._
|
import BitsCodecs._
|
||||||
import fluence.codec.PureCodec
|
import fluence.codec.PureCodec
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ class BitsCodecsSpec extends WordSpec with Matchers with Checkers {
|
|||||||
val arrCodec = implicitly[PureCodec[Array[Byte], ByteVector]]
|
val arrCodec = implicitly[PureCodec[Array[Byte], ByteVector]]
|
||||||
val b64Codec = implicitly[PureCodec[ByteVector, String]]
|
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 arrCodec.swap).direct.apply[Id](bytes.toArray).value.map(_.toList).contains(bytes) &&
|
||||||
(arrCodec andThen b64Codec andThen b64Codec.swap andThen arrCodec.swap).direct
|
(arrCodec andThen b64Codec andThen b64Codec.swap andThen arrCodec.swap).direct
|
||||||
.apply[Id](bytes.toArray)
|
.apply[Id](bytes.toArray)
|
||||||
|
@ -14,7 +14,8 @@ val scalaV = scalaVersion := "2.12.8"
|
|||||||
|
|
||||||
val commons = Seq(
|
val commons = Seq(
|
||||||
scalaV,
|
scalaV,
|
||||||
version := "0.0.4",
|
//crossScalaVersions := Seq(scalaVersion.value, "2.13.0-RC1"),
|
||||||
|
version := "0.0.5",
|
||||||
fork in Test := true,
|
fork in Test := true,
|
||||||
parallelExecution in Test := false,
|
parallelExecution in Test := false,
|
||||||
organization := "one.fluence",
|
organization := "one.fluence",
|
||||||
@ -30,10 +31,10 @@ val commons = Seq(
|
|||||||
|
|
||||||
commons
|
commons
|
||||||
|
|
||||||
val kindProjector = addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.9")
|
val kindProjector = addCompilerPlugin("org.typelevel" % "kind-projector" % "0.10.0" cross CrossVersion.binary)
|
||||||
|
|
||||||
val Cats1V = "1.5.0"
|
val Cats1V = "1.6.0"
|
||||||
val ScodecBitsV = "1.1.9"
|
val ScodecBitsV = "1.1.10"
|
||||||
val CirceV = "0.11.1"
|
val CirceV = "0.11.1"
|
||||||
val ShapelessV = "2.3.+"
|
val ShapelessV = "2.3.+"
|
||||||
|
|
||||||
|
@ -49,6 +49,7 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
* @tparam B Successful result type
|
* @tparam B Successful result type
|
||||||
*/
|
*/
|
||||||
abstract class Func[A, B] {
|
abstract class Func[A, B] {
|
||||||
|
f ⇒
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the func on input, using the given monad.
|
* Run the func on input, using the given monad.
|
||||||
@ -63,9 +64,9 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
*
|
*
|
||||||
* @param input Input
|
* @param input Input
|
||||||
* @param F All internal maps and composes, as well as errors, are to be executed with this MonadError.
|
* @param F All internal maps and composes, as well as errors, are to be executed with this MonadError.
|
||||||
* Error type should be a supertype for this arrow's error E.
|
* Error type should be a supertype for this arrow's error E.
|
||||||
*/
|
*/
|
||||||
def runF[F[_]](input: A)(implicit F: MonadError[F, EE] forSome {type EE >: E}): F[B] =
|
def runF[F[_]](input: A)(implicit F: MonadError[F, EE] forSome { type EE >: E }): F[B] =
|
||||||
runEither(input).flatMap(F.fromEither)
|
runEither(input).flatMap(F.fromEither)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,13 +77,37 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
*/
|
*/
|
||||||
def apply[F[_]: Monad](input: A): EitherT[F, E, B]
|
def apply[F[_]: Monad](input: A): EitherT[F, E, B]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for function composition
|
||||||
|
*
|
||||||
|
* @param other Other function to run after
|
||||||
|
* @tparam C Resulting input type
|
||||||
|
* @return Composed function
|
||||||
|
*/
|
||||||
|
def on[C](other: Func[C, A]): Func[C, B] =
|
||||||
|
catsMonadicalEitherArrowChoice.compose(this, other)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert this Func into another one, lifting the error
|
||||||
|
*
|
||||||
|
* @param m Another instance of MonadicalEitherArrow
|
||||||
|
* @param convertE Convert error
|
||||||
|
* @tparam EE Error type
|
||||||
|
* @return Converted function
|
||||||
|
*/
|
||||||
|
def to[EE <: Throwable](m: MonadicalEitherArrow[EE])(implicit convertE: E ⇒ EE): m.Func[A, B] =
|
||||||
|
new m.Func[A, B] {
|
||||||
|
override def apply[F[_]: Monad](input: A): EitherT[F, EE, B] =
|
||||||
|
f[F](input).leftMap(convertE)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts this Func to Kleisli, using MonadError to execute upon and to lift errors into.
|
* Converts this Func to Kleisli, using MonadError to execute upon and to lift errors into.
|
||||||
*
|
*
|
||||||
* @param F All internal maps and composes, as well as errors, are to be executed with this MonadError.
|
* @param F All internal maps and composes, as well as errors, are to be executed with this MonadError.
|
||||||
* Error type should be a supertype for this arrow's error E.
|
* Error type should be a supertype for this arrow's error E.
|
||||||
*/
|
*/
|
||||||
def toKleisli[F[_]](implicit F: MonadError[F, EE] forSome {type EE >: E}): Kleisli[F, A, B] =
|
def toKleisli[F[_]](implicit F: MonadError[F, EE] forSome { type EE >: E }): Kleisli[F, A, B] =
|
||||||
Kleisli(input ⇒ runF[F](input))
|
Kleisli(input ⇒ runF[F](input))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,11 +122,11 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Picks a point from the arrow, using the initial element (Unit) on the left.
|
* Picks a point from the arrow, using the initial element (Unit) on the left.
|
||||||
*
|
*
|
||||||
* @param input Point to pick
|
* @param input Point to pick
|
||||||
* @return Picked point
|
* @return Picked point
|
||||||
*/
|
*/
|
||||||
def pointAt(input: A): Point[B] =
|
def pointAt(input: A): Point[B] =
|
||||||
catsMonadicalEitherArrowChoice.lmap(this)(_ ⇒ input)
|
catsMonadicalEitherArrowChoice.lmap(this)(_ ⇒ input)
|
||||||
}
|
}
|
||||||
@ -187,22 +212,22 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check a condition, lifted with a Func.
|
* Check a condition, lifted with a Func.
|
||||||
*
|
*
|
||||||
* @param error Error to produce when condition is not met
|
* @param error Error to produce when condition is not met
|
||||||
* @return Func that takes boolean, checks it, and returns Unit or fails with given error
|
* @return Func that takes boolean, checks it, and returns Unit or fails with given error
|
||||||
*/
|
*/
|
||||||
def cond(error: ⇒ E): Func[Boolean, Unit] =
|
def cond(error: ⇒ E): Func[Boolean, Unit] =
|
||||||
liftFuncEither(Either.cond(_, (), error))
|
liftFuncEither(Either.cond(_, (), error))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lift a function which returns a Func arrow with Unit on the left side.
|
* Lift a function which returns a Func arrow with Unit on the left side.
|
||||||
*
|
*
|
||||||
* @param f Function to lift
|
* @param f Function to lift
|
||||||
*/
|
*/
|
||||||
def liftFuncPoint[A, B](f: A ⇒ Point[B]): Func[A,B] =
|
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] =
|
override def apply[F[_]: Monad](input: A): EitherT[F, E, B] =
|
||||||
f(input).apply[F](())
|
f(input).apply[F](())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,25 +265,25 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Point type maps from Unit to a particular value of A, so it's just a lazy Func.
|
* Point type maps from Unit to a particular value of A, so it's just a lazy Func.
|
||||||
*
|
*
|
||||||
* @tparam A Output value type
|
* @tparam A Output value type
|
||||||
*/
|
*/
|
||||||
type Point[A] = Func[Unit, A]
|
type Point[A] = Func[Unit, A]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Point must obey MonadErrorLaws
|
* Point must obey MonadErrorLaws
|
||||||
*/
|
*/
|
||||||
implicit object catsMonadicalEitherPointMonad extends MonadError[Point, E] {
|
implicit object catsMonadicalEitherPointMonad extends MonadError[Point, E] {
|
||||||
override def flatMap[A, B](fa: Point[A])(f: A ⇒ Point[B]): Point[B] =
|
override def flatMap[A, B](fa: Point[A])(f: A ⇒ Point[B]): Point[B] =
|
||||||
new Func[Unit, B]{
|
new Func[Unit, B] {
|
||||||
override def apply[F[_] : Monad](input: Unit): EitherT[F, E, B] =
|
override def apply[F[_]: Monad](input: Unit): EitherT[F, E, B] =
|
||||||
fa[F](()).flatMap(f(_).apply[F](()))
|
fa[F](()).flatMap(f(_).apply[F](()))
|
||||||
}
|
}
|
||||||
|
|
||||||
override def tailRecM[A, B](a: A)(f: A ⇒ Point[Either[A, B]]): Point[B] =
|
override def tailRecM[A, B](a: A)(f: A ⇒ Point[Either[A, B]]): Point[B] =
|
||||||
new Func[Unit, B]{
|
new Func[Unit, B] {
|
||||||
override def apply[F[_] : Monad](input: Unit): EitherT[F, E, B] =
|
override def apply[F[_]: Monad](input: Unit): EitherT[F, E, B] =
|
||||||
Monad[EitherT[F, E, ?]].tailRecM(a)(f(_).apply[F](()))
|
Monad[EitherT[F, E, ?]].tailRecM(a)(f(_).apply[F](()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,8 +291,8 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
liftFuncEither(_ ⇒ Left(e))
|
liftFuncEither(_ ⇒ Left(e))
|
||||||
|
|
||||||
override def handleErrorWith[A](fa: Point[A])(f: E ⇒ Point[A]): Point[A] =
|
override def handleErrorWith[A](fa: Point[A])(f: E ⇒ Point[A]): Point[A] =
|
||||||
new Func[Unit, A]{
|
new Func[Unit, A] {
|
||||||
override def apply[F[_] : Monad](input: Unit): EitherT[F, E, A] =
|
override def apply[F[_]: Monad](input: Unit): EitherT[F, E, A] =
|
||||||
fa[F](()).leftFlatMap(e ⇒ f(e).apply[F](()))
|
fa[F](()).leftFlatMap(e ⇒ f(e).apply[F](()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,9 +323,9 @@ abstract class MonadicalEitherArrow[E <: Throwable] {
|
|||||||
Bijection(liftFuncEither(direct), liftFuncEither(inverse))
|
Bijection(liftFuncEither(direct), liftFuncEither(inverse))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lifts point functions into Bijection.
|
* Lifts point functions into Bijection.
|
||||||
*/
|
*/
|
||||||
def liftPointB[A,B](direct: A ⇒ Point[B], inverse: B ⇒ Point[A]): Bijection[A,B] =
|
def liftPointB[A, B](direct: A ⇒ Point[B], inverse: B ⇒ Point[A]): Bijection[A, B] =
|
||||||
Bijection(liftFuncPoint(direct), liftFuncPoint(inverse))
|
Bijection(liftFuncPoint(direct), liftFuncPoint(inverse))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,6 +24,7 @@ import cats.laws.discipline.{MonadErrorTests, SemigroupalTests}
|
|||||||
import cats.tests.CatsSuite
|
import cats.tests.CatsSuite
|
||||||
import fluence.codec
|
import fluence.codec
|
||||||
import org.scalacheck.Arbitrary
|
import org.scalacheck.Arbitrary
|
||||||
|
import org.scalacheck.Arbitrary._
|
||||||
import org.scalacheck.ScalacheckShapeless._
|
import org.scalacheck.ScalacheckShapeless._
|
||||||
|
|
||||||
class PureCodecPointLawsSpec extends CatsSuite {
|
class PureCodecPointLawsSpec extends CatsSuite {
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.5.1")
|
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0")
|
||||||
|
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.15")
|
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.20")
|
||||||
|
|
||||||
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.0.0")
|
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.2.0")
|
||||||
|
|
||||||
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.19")
|
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.20")
|
||||||
|
|
||||||
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.26")
|
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.27")
|
||||||
addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "0.6.0")
|
addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "0.6.0")
|
||||||
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0")
|
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0")
|
||||||
|
|
||||||
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.1")
|
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.14.0")
|
||||||
|
|
||||||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
|
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user