Skip to content

Commit

Permalink
Merge pull request #329 from goshacodes/fix_message
Browse files Browse the repository at this point in the history
fix reader message on missing fields
  • Loading branch information
dos65 authored Nov 11, 2024
2 parents 0c04dc9 + 94ee3bb commit fd6f7d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private[derivation] class DerivationMacro(val quotes: Quotes)
}
ReaderError.wrongJson(
"Can not extract fields from json: " + uninitializedFields
.mkString(", ")
.mkString("'", "', '", "'")
)(${ fieldName })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class DerivationSpec extends AnyFlatSpec with Matchers {
res
}

it should "build message correctly" in {
case class Foo(bar: Int, baz: String, faz: Boolean) derives JsonReader

util.Try(read[Foo](obj("bar" -> 1))) should matchPattern {
case util.Failure(ex)
if ex.getMessage == """Illegal json at '[ROOT]': Can not extract fields from json: 'baz', 'faz'""" =>
}
}

it should "compile and correctly write and read product" in {
case class Person(
id: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ trait ReaderDerivation
if($predicate) {
val $uninitializedFields = new scala.collection.mutable.ArrayBuffer[String](${xs.size})
..$fields
$readerErrorCompanion.wrongJson("Can not extract fields from json" + $uninitializedFields.mkString("'", "', '", "'"))
$readerErrorCompanion.wrongJson("Can not extract fields from json: " + $uninitializedFields.mkString("'", "', '", "'"))
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class SemiautoReaderDerivationTest extends AnyFlatSpec with Matchers {


behavior of "semiauto derivation"

it should "build message correctly" in {
case class Foo(bar: Int, baz: String, faz: Boolean)

implicit val reader: JsonReader[Foo] = tethys.derivation.semiauto.jsonReader

util.Try(read[Foo](obj("bar" -> 1))) should matchPattern {
case util.Failure(ex) if ex.getMessage == """Illegal json at '[ROOT]': Can not extract fields from json: 'baz', 'faz'""" =>
}
}

it should "derive readers for simple case class hierarchy" in {
implicit val dReader: JsonReader[D] = jsonReader[D]
implicit val cReader: JsonReader[C] = jsonReader[C]
Expand Down

0 comments on commit fd6f7d7

Please sign in to comment.