Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoder.decodeAccumulating discards errors in ADTs #2062

Open
morgen-peschke opened this issue Nov 22, 2022 · 0 comments
Open

Decoder.decodeAccumulating discards errors in ADTs #2062

morgen-peschke opened this issue Nov 22, 2022 · 0 comments

Comments

@morgen-peschke
Copy link

morgen-peschke commented Nov 22, 2022

The microsite shows the standard way to write a Decoder for an ADT is to merge a list of Decoders using Decoder#or:

implicit val decodeEvent: Decoder[Event] =
  List[Decoder[Event]](
    Decoder[Foo].widen,
    Decoder[Bar].widen,
    Decoder[Baz].widen,
    Decoder[Qux].widen
  ).reduceLeft(_ or _)

This works when the JSON is correct, and produces misleading errors when the JSON structure is incorrect:

println(decode[Event]("""{ "i": null }""").show)
// Left(DecodingFailure at .values: Missing required field)

Which makes sense, as decode returns as single error. What makes less sense is that decodeAccumulating returns almost exactly the same thing:

println(decodeAccumulating[Event]("""{ "i": null }""").show)
// Invalid(NonEmptyList(DecodingFailure at .values: Missing required field))

A more useful error from decodeAccumulating could look like this:

Invalid(NonEmptyList(DecodingFailure at .i: Int, DecodingFailure at .s: Missing required field, DecodingFailure at .c: Missing required field, DecodingFailure at .values: Missing required field))

It looks like this could be accomplished with a change to Decoder#or (and likely a similar change to Decoder#either, for consistency):

// Existing
override def tryDecodeAccumulating(c: ACursor): Decoder.AccumulatingResult[AA] =
  self.tryDecodeAccumulating(c) match {
    case r @ Valid(_) => r
    case Invalid(_)   => d.tryDecodeAccumulating(c)
  }
// Proposed
override def tryDecodeAccumulating(c: ACursor): Decoder.AccumulatingResult[AA] =
  self.tryDecodeAccumulating(c) match {
    case r @ Valid(_) => r
    case Invalid(i)   => d.tryDecodeAccumulating(c).leftMap(i.concatNel)
  }

Scastie with the above implemented as a Semigroup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant