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 support of fieldName annotations #773

Merged

Conversation

plokhotnyuk
Copy link
Contributor

  • Fix using of fieldName annotations to rename fields (not aliasing)

  • Fix ignoring non-first fieldName annotations at the field declarations

@plokhotnyuk plokhotnyuk requested a review from a team as a code owner January 9, 2025 11:11
Fix ignoring non-first fieldName annotations at the field declarations
@@ -256,7 +256,7 @@ private case class DeriveSchema()(using val ctx: Quotes) {
tpe.asType
}
val annotations = paramAnns.getOrElse(label, List.empty)
val nameExpr = annotations.collectFirst {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think more the one annotation of the same kind is using the lib wrong. I would keep the old impl, since it is less complicated. You annotate twice? Your problem. Just don't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed solution is a workaround for reverse order of annotations in Scala 3 macros.

The better solution would be throwing a compile-time error during derivation or/and runtime error when calculating fieldName value in Schema.Field instances

val fieldName: String = annotations.collectFirst {
case f: fieldName => f.name
}.getOrElse(name)

val nameAndAliases: scala.collection.immutable.Set[String] =
annotations.foldLeft(scala.collection.immutable.Set(fieldName)) { (acc, annotation) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a difference besides making it a fold?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It avoids reusing of an old (renamed) field name as an alias

@987Nabil 987Nabil merged commit d24ae5f into zio:main Jan 9, 2025
26 checks passed
@plokhotnyuk plokhotnyuk deleted the fix-support-of-fieldName-annotations branch January 9, 2025 13:01
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

Successfully merging this pull request may close these issues.

2 participants