Skip to content

Commit

Permalink
Issue davegurnell#1: Context switching working. More tests needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Aug 17, 2018
1 parent 702c203 commit 1d1753c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/checklist/CanLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.language.higherKinds


trait NaturalTransformationLowPriorityImplicits {
implicit def applicativeTransform[C[_[_]], F[_]](implicit evF: C[F], evCF: C[F] <:< Applicative[F]): Id ~> F = new (Id ~> F) {
implicit def applicativeTransform[F[_]](implicit evF: Applicative[F]): Id ~> F = new (Id ~> F) {
override def apply[A](a: Id[A]): F[A] = evF.pure(a)
}
}
Expand Down
34 changes: 1 addition & 33 deletions core/src/main/scala/checklist/Rule1Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,19 @@ trait Rule1Syntax {
}

implicit class Rule1Ops[F[_] : Applicative, A](self: Check[F, A]) {
// private def andSame(that: Rule[F, A, A]): Rule[F, A, A] =
// (self, that).mapN((x, _) => x)

def and[G[_] : Applicative, R[_]](that: Check[G, A])(implicit canLift: CanLift[Applicative, F, G, R]): Rule[R, A, A] = {
implicit val evApplicativeR: Applicative[R] = canLift.evR

(self.liftWith(canLift.liftF), that.liftWith(canLift.liftG)).mapN((x, _) => x)
}

/*
def field[B, G[_] : Applicative, R[_]](field: String, accessor: A => B)(validator: Validator[B, G])(implicit canLift: CanLift[F, G, R]): Validator[A, R] =
this and (validator contramap accessor prefix field)
def field[B, G[_] : Applicative, R[_]](accessor: A => B)(validator: Validator[B, G])(implicit canLift: CanLift[F, G, R]): Validator[A, R] =
macro ValidationMacros.field[A, B, F, G, R]
def fieldImplicit[B, G[_] : Applicative, R[_]](field: String, accessor: A => B)(implicit validator: Validator[B, G], canLift: CanLift[F, G, R]): Validator[A, R] =
this.field(field, accessor)(validator)
def fieldImplicit[B, G[_] : Applicative, R[_]](accessor: A => B)(implicit validator: Validator[B, G], canLift: CanLift[F, G, R]): Validator[A, R] =
macro ValidationMacros.fieldImplicit[A, B, F, G, R]
def fieldWith[B, G[_] : Applicative, R[_]](field: String, accessor: A => B)(validatorBuilder: A => Validator[B, G])(implicit canLift: CanLift[F, G, R]): Validator[A, R] =
this and Validator[A, G] { value =>
val validator = validatorBuilder(value) contramap accessor prefix field
validator(value)
}
def fieldWith[B, G[_] : Applicative, R[_]](accessor: A => B)(validatorBuilder: A => Validator[B, G])(implicit canLift: CanLift[F, G, R]): Validator[A, R] =
macro ValidationMacros.fieldWith[A, B, F, G, R]
def fieldWithImplicit[B, G[_] : Applicative, R[_]](field: String, accessor: A => B)(implicit validatorBuilder: A => Validator[B, G], canLift: CanLift[F, G, R]): Validator[A, R] =
this.fieldWith(field, accessor)(validatorBuilder)
def fieldWithImplicit[B, G[_] : Applicative, R[_]](accessor: A => B)(implicit validatorBuilder: A => Validator[B, G], canLift: CanLift[F, G, R]): Validator[A, R] =
macro ValidationMacros.fieldWithImplicit[A, B, F, G, R]
*/

def field[G[_] : Applicative, R[_], B](path: Path, lens: Lens[A, B])(rule: Rule[G, B, B])(implicit canLift: CanLift[Applicative, F, G, R]): Rule[R, A, A] =
self and rule.at(path, lens)

def field[G[_] : Applicative, R[_], B](accessor: A => B)(rule: Rule[G, B, B])(implicit canLift: CanLift[Applicative, F, G, R]): Rule[R, A, A] =
macro RuleMacros.field[F, G, R, A, B]

// This is not really working, the compiler can't unify. We should try with embedded class with apply().
def fieldImplicit[G[_] : Applicative, R[_], B](path: Path, lens: Lens[A, B])(implicit rule: Rule[G, B, B], canLift: CanLift[Applicative, F, G, R]): Rule[R, A, A] =
field(path, lens)(rule)

Expand Down
22 changes: 22 additions & 0 deletions core/src/test/scala/checklist/ContextSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checklist

import cats._
import cats.data.Ior
import cats.implicits._
import org.scalatest.{FreeSpec, Matchers}

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class ContextSpec extends FreeSpec with Matchers {

"pass" in {
val rule1 = Rule.pass[Int]
val rule2 = rule1.liftTo[Future]
val combined1 = rule1 and rule2
val combined2 = rule2 and rule1
combined1(+1) map (_ should be(Ior.right(+1)))
combined2(+1) map (_ should be(Ior.right(+1)))
}

}

0 comments on commit 1d1753c

Please sign in to comment.