-
Notifications
You must be signed in to change notification settings - Fork 115
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
Feat/support all annotations in scala 3 #1471
Open
ThijsBroersen
wants to merge
3
commits into
series/4.x
Choose a base branch
from
feat/support-all-annotations-in-scala-3
base: series/4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
magnolia/shared/src/main/scala-dotty/zio/config/magnolia/AnnotationMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package zio.config.magnolia | ||
|
||
import scala.quoted.* | ||
import zio.config.derivation._ | ||
|
||
private[magnolia] object AnnotationMacros: | ||
inline def nameOf[T]: List[name] = ${ filterAnnotations[T, name] } | ||
inline def discriminatorOf[T]: List[discriminator] = ${ filterAnnotations[T, discriminator] } | ||
inline def descriptionOf[T]: List[describe] = ${ filterAnnotations[T, describe] } | ||
inline def caseModifier[T]: List[kebabCase | snakeCase] = ${ filterAnnotations[T, kebabCase | snakeCase] } | ||
inline def kebabCaseOf[T]: List[kebabCase] = ${ filterAnnotations[T, kebabCase] } | ||
inline def snakeCaseOf[T]: List[snakeCase] = ${ filterAnnotations[T, snakeCase] } | ||
inline def keyModifiers[T]: List[prefix | postfix | suffix] = ${ filterAnnotations[T, prefix | postfix | suffix] } | ||
inline def prefixOf[T]: List[prefix] = ${ filterAnnotations[T, prefix] } | ||
inline def postfixOf[T]: List[postfix] = ${ filterAnnotations[T, postfix] } | ||
inline def suffixOf[T]: List[suffix] = ${ filterAnnotations[T, suffix] } | ||
inline def fieldNamesOf[T]: List[(String, List[name])] = ${ filterFieldAnnotations[T, name] } | ||
inline def fieldDescriptionsOf[T]: List[(String, List[describe])] = ${ | ||
filterFieldAnnotations[T, describe] | ||
} | ||
|
||
private def filterAnnotations[T: Type, A: Type](using Quotes): Expr[List[A]] = { | ||
import quotes.reflect.* | ||
|
||
val annotationTpe = TypeRepr.of[A] | ||
|
||
val annotations = TypeRepr | ||
.of[T] | ||
.typeSymbol | ||
.annotations | ||
.collect: | ||
case term if term.tpe <:< annotationTpe => term | ||
|
||
Expr.ofList(annotations.reverse.map(_.asExprOf[A])) | ||
} | ||
|
||
private def filterFieldAnnotations[T: Type, A: Type](using Quotes): Expr[List[(String, List[A])]] = | ||
import quotes.reflect.* | ||
|
||
val annotationTpe = TypeRepr.of[A] | ||
|
||
val namedAnnotations = TypeRepr | ||
.of[T] | ||
.typeSymbol | ||
.primaryConstructor | ||
.paramSymss | ||
.flatten | ||
.map(field => field.name -> field.annotations) | ||
|
||
Expr | ||
.ofList( | ||
namedAnnotations | ||
.map: | ||
case (name, terms) => | ||
name -> terms.collect: | ||
case term if term.tpe <:< annotationTpe => term | ||
.map: | ||
case (name, terms) => Expr(name) -> terms.reverse.map(_.asExprOf[A]) | ||
.map((name, annotations) => Expr.ofTuple((name, Expr.ofList(annotations)))) | ||
) | ||
end AnnotationMacros |
30 changes: 30 additions & 0 deletions
30
magnolia/shared/src/main/scala-dotty/zio/config/magnolia/DefaultValueMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package zio.config.magnolia | ||
|
||
import scala.quoted.* | ||
import zio.config.derivation._ | ||
|
||
private[magnolia] object DefaultValueMacros: | ||
|
||
inline def defaultValuesOf[T]: List[(String, Any)] = ${ defaultValues[T] } | ||
def defaultValues[T: Type](using Quotes): Expr[List[(String, Any)]] = | ||
import quotes.reflect.* | ||
val tpe = TypeRepr.of[T] | ||
|
||
val sym = tpe.typeSymbol | ||
|
||
val namesOfFieldsWithDefaultValues = | ||
sym.caseFields.filter(s => s.flags.is(Flags.HasDefault)).map(_.name) | ||
|
||
val companionClas = | ||
sym.companionClass | ||
|
||
val defaultRefs = | ||
companionClas.declarations | ||
.filter(_.name.startsWith("$lessinit$greater$default")) | ||
.map(Ref(_)) | ||
|
||
Expr.ofList(namesOfFieldsWithDefaultValues.zip(defaultRefs).map { case (n, ref) => | ||
Expr.ofTuple(Expr(n), ref.asExpr) | ||
}) | ||
|
||
end DefaultValueMacros |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please review this carefully, because I think L341 should be enabled and L342 disabled. This changes the existing behaviour only to match the Scala 2 behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L340 I already adjusted.