Skip to content

Commit

Permalink
Fix traces of decoding error for tuples (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Jan 19, 2025
1 parent 75ec28b commit 953f7ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ lazy val zioJson = crossProject(JSPlatform, JVMPlatform, NativePlatform)
val tparams = (1 to i).map(p => s"A$p").mkString(", ")
val implicits = (1 to i).map(p => s"A$p: JsonDecoder[A$p]").mkString(", ")
val work = (1 to i)
.map(p => s"val a$p = A$p.unsafeDecode(trace :+ traces($p), in)")
.map(p => s"val a$p = A$p.unsafeDecode(traces(${p - 1}) :: trace, in)")
.mkString("\n Lexer.char(trace, in, ',')\n ")
val returns = (1 to i).map(p => s"a$p").mkString(", ")

s"""implicit def tuple$i[$tparams](implicit $implicits): JsonDecoder[Tuple$i[$tparams]] =
| new JsonDecoder[Tuple$i[$tparams]] {
| val traces: Array[JsonError] = (0 to $i).map(JsonError.ArrayAccess(_)).toArray
| private[this] val traces: Array[JsonError] = (0 to ${i - 1}).map(JsonError.ArrayAccess(_)).toArray
| def unsafeDecode(trace: List[JsonError], in: RetractReader): Tuple$i[$tparams] = {
| Lexer.char(trace, in, '[')
| $work
| Lexer.char(trace, in, ']')
| Tuple$i($returns)
| new Tuple$i($returns)
| }
| }""".stripMargin
}
Expand Down
7 changes: 7 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ object DecoderSpec extends ZIOSpecDefault {
forall(isRight(isRight(equalTo(2))))
)
},
test("tuples") {
assert("""["a",3]""".fromJson[(String, Int)])(isRight(equalTo(("a", 3))))
assert("""["a","b"]""".fromJson[(String, Int)])(isLeft(equalTo("[1](expected a number, got 'b')")))
assert("""[[0.1,0.2],[0.3,0.4],[-0.3,-]]""".fromJson[Seq[(Double, Double)]])(
isLeft(equalTo("[2][1](expected a Double)"))
)
},
test("parameterless products") {
import exampleproducts._

Expand Down

0 comments on commit 953f7ed

Please sign in to comment.