Skip to content

Commit

Permalink
Fix generic record construction for fields with fieldName annotation (z…
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Jun 28, 2024
1 parent e3ede68 commit 37d5954
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,11 @@ object DeriveSchema {
}
}
val fromMap = {
val casts = fieldTypes.map { termSymbol =>
q"""
try m.apply(${termSymbol.name.toString.trim}).asInstanceOf[${termSymbol.typeSignature}]
val casts = fieldTypes.zip(fieldAnnotations).map {
case (termSymbol, annotations) =>
val newName = getFieldName(annotations).getOrElse(termSymbol.name.toString.trim)
q"""
try m.apply(${newName}).asInstanceOf[${termSymbol.typeSignature}]
catch {
case _: ClassCastException => throw new RuntimeException("Field " + ${termSymbol.name.toString.trim} + " has invalid type")
case _: Throwable => throw new RuntimeException("Field " + ${termSymbol.name.toString.trim} + " is missing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,9 @@ object JsonCodec {
if (Lexer.firstField(trace, in)) {
while ({
val field = Lexer.string(trace, in).toString
structure.find(_.name == field) match {
structure.find(
f => f.name == field || f.annotations.collectFirst { case fieldName(name) => name }.contains(field)
) match {
case Some(Schema.Field(label, schema, _, _, _, _)) =>
val trace_ = JsonError.ObjectAccess(label) :: trace
Lexer.char(trace_, in, ':')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
f20: Option[String] = None,
f21: Option[String] = None,
f22: Option[String] = None,
f23: Option[String] = None
@fieldName("$f23") f23: Option[String] = None
)

object RecordExample {
Expand Down

0 comments on commit 37d5954

Please sign in to comment.