Skip to content

Commit

Permalink
Use two list parameter lists.
Browse files Browse the repository at this point in the history
Thus allowing embedding the error generation function in a block and making the overload seamlessly work with Scala 2.11
  • Loading branch information
pfcoperez committed Nov 23, 2018
1 parent a152532 commit 98588d0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
4 changes: 2 additions & 2 deletions modules/core/shared/src/main/scala/io/circe/Decoder.scala
Expand Up @@ -163,7 +163,7 @@ trait Decoder[A] extends Serializable { self =>
* (instead only the error of the last failing `validate` in the chain will be
* returned).
*/
final def validate(pred: HCursor => Boolean, message: HCursor => String): Decoder[A] = new Decoder[A] {
final def validate(pred: HCursor => Boolean)(message: HCursor => String): Decoder[A] = new Decoder[A] {
final def apply(c: HCursor): Decoder.Result[A] =
if (pred(c)) self(c) else Left(DecodingFailure(message(c), c.history))

Expand All @@ -182,7 +182,7 @@ trait Decoder[A] extends Serializable { self =>
* returned).
*/
final def validate(pred: HCursor => Boolean, message: => String): Decoder[A] =
validate(pred, _ => message)
validate(pred)(_ => message)

/**
* Convert to a Kleisli arrow.
Expand Down
Expand Up @@ -407,16 +407,14 @@ class DecoderSuite extends CirceSuite with LargeNumberDecoderTests with TableDri
it should "generate error messages from HCursor when a function is passed" in {
case class Foo(x: Int, y: String)

val errorFunction: HCursor => String = { c =>
val decoder: Decoder[Foo] = Decoder.const(Foo(42, "meaning")).validate(_ => false) { c =>
val maybeFieldsStr = for {
json <- c.focus
jsonKeys <- json.hcursor.keys
} yield jsonKeys.mkString(",")
maybeFieldsStr.getOrElse("")
}

val decoder: Decoder[Foo] = Decoder.const(Foo(42, "meaning")).validate(_ => false, errorFunction)

val Right(fooJson) = parse("""{"x":42, "y": "meaning"}""")

assert(decoder.decodeJson(fooJson).left.get.message == "x,y")
Expand Down

0 comments on commit 98588d0

Please sign in to comment.