-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support all annotations in Scala 3
- Loading branch information
1 parent
b832b8b
commit d2cdce5
Showing
12 changed files
with
478 additions
and
61 deletions.
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.