Last active
January 3, 2018 15:35
-
-
Save mrdziuban/d11bcd259faae676c17e3a4c7a27d388 to your computer and use it in GitHub Desktop.
shapeless implicit LeftFolder resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import $ivy.`com.chuusai::shapeless:2.3.3` | |
// import $ivy.`com.chuusai::shapeless:2.3.4-SNAPSHOT` | |
import $ivy.`org.scalaz::scalaz-core:7.2.17` | |
interp.repositories() ++= Seq(coursier.MavenRepository("https://dl.bintray.com/bondlink/Typify")) | |
import $ivy.`typify::typify:2.4.1` | |
import shapeless.{::, HNil} | |
import shapeless.syntax.singleton._ | |
import typify.{Typify, Parsed, ParseError} | |
import typify.convert._ | |
import typify.convert.syntax._ | |
import typify.parsedany._ | |
import scalaz.NonEmptyList | |
import scalaz.syntax.either._ | |
import scalaz.syntax.nel._ | |
import scalaz.syntax.validation._ | |
case class Fail(reason: String) | |
val tp = new Typify[Fail, Parsed[Any]] | |
implicit val parse2Error = (p: Parsed[Any], pe: ParseError) => Fail(s"${pe.key} - ${pe.error}") | |
val checkEmail = Typify.validate((s: String) => | |
s.right[NonEmptyList[Fail]] | |
.ensure(Fail("Email is invalid").wrapNel)(_.contains("@")) | |
.validation) | |
val checkAge = Typify.validate((i: Int) => | |
i.right[NonEmptyList[Fail]] | |
.ensure(Fail("Too young").wrapNel)(_ > 21) | |
.validation) | |
val checkSessId = Typify.optional((i: Int) => | |
i.right[NonEmptyList[Fail]] | |
.ensure(Fail("Invalid session id").wrapNel)(_ > 3000) | |
.validation) | |
val checkPerson = 'email ->> checkEmail :: 'age ->> checkAge :: 'session ->> checkSessId :: HNil | |
import tp.syntax._ | |
val passes: Any = Map("email" -> "foo@bar", "age" -> 22, "session" -> 77777, 3 -> "junk") | |
// Fails on 2.3.3 but passes with #686 reverted | |
val passed = Parsed(passes).parse(checkPerson) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment