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

Fix generic record construction for fields with fieldName annotation #698

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading