forked from tethys-json/tethys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented JsonDiscriminator feature which allowes to derive JsonReader
- Loading branch information
Георгий Ковалев
committed
Apr 25, 2024
1 parent
5ec673f
commit 3d54231
Showing
8 changed files
with
222 additions
and
17 deletions.
There are no files selected for viewing
Binary file not shown.
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,9 @@ | ||
package tethys | ||
|
||
|
||
trait JsonDiscriminator[A, B]: | ||
def choose: A => B | ||
|
||
object JsonDiscriminator: | ||
def by[A, B](f: A => B): JsonDiscriminator[A, B] = new JsonDiscriminator[A, B]: | ||
override def choose: A => B = f |
61 changes: 61 additions & 0 deletions
61
modules/core/src/main/scala-3/tethys/derivation/Discriminator.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 tethys.derivation | ||
|
||
private[tethys] | ||
object Discriminator: | ||
|
||
inline def getLabel[Type, Discriminator]: String = | ||
${ DiscriminatorMacro.getLabel[Type, Discriminator] } | ||
|
||
inline def getValue[Type, SubType, Discriminator](label: String): Any = | ||
${ DiscriminatorMacro.getValue[Type, SubType, Discriminator]('{ label }) } | ||
|
||
private[derivation] | ||
object DiscriminatorMacro: | ||
import scala.quoted.* | ||
|
||
def getLabel[T: Type, D: Type](using quotes: Quotes): Expr[String] = | ||
import quotes.reflect.* | ||
val tpe = TypeRepr.of[T] | ||
val selectorTpe = TypeRepr.of[D] | ||
val symbol = tpe.typeSymbol.fieldMembers | ||
.find(tpe.memberType(_) =:= selectorTpe) | ||
.getOrElse(report.errorAndAbort(s"Selector of type ${selectorTpe.show(using Printer.TypeReprShortCode)} not found in ${tpe.show(using Printer.TypeReprShortCode)}")) | ||
|
||
tpe.typeSymbol.children | ||
.find(child => child.caseFields.contains(symbol.overridingSymbol(child))) | ||
.foreach { child => | ||
report.errorAndAbort(s"Overriding discriminator field '${symbol.name}' in ${child.typeRef.show(using Printer.TypeReprShortCode)} is prohibited") | ||
} | ||
|
||
Expr(symbol.name) | ||
|
||
|
||
def getValue[T: Type, ST: Type, D: Type](label: Expr[String])(using quotes: Quotes): Expr[Any] = | ||
import quotes.reflect.* | ||
val tpe = TypeRepr.of[T] | ||
val selectorTpe = TypeRepr.of[D] | ||
val symbol = tpe.typeSymbol.fieldMembers | ||
.find(tpe.memberType(_) =:= selectorTpe) | ||
.getOrElse(report.errorAndAbort(s"Selector of type ${selectorTpe.show(using Printer.TypeReprShortCode)} not found in ${tpe.show(using Printer.TypeReprShortCode)}")) | ||
|
||
Select(stub[ST].asTerm, symbol).asExprOf[Any] | ||
|
||
|
||
def stub[T: Type](using quotes: Quotes): Expr[T] = | ||
import quotes.reflect.* | ||
val tpe = TypeRepr.of[T] | ||
val symbol = tpe.typeSymbol | ||
val constructorFieldsFilledWithNulls: List[List[Term]] = | ||
symbol.primaryConstructor.paramSymss | ||
.filterNot(_.exists(_.isType)) | ||
.map(_.map(_.typeRef.widen match { | ||
case t@AppliedType(inner, applied) => | ||
Select.unique('{ null }.asTerm, "asInstanceOf").appliedToTypes(List(inner.appliedTo(tpe.typeArgs))) | ||
case other => | ||
Select.unique('{ null }.asTerm, "asInstanceOf").appliedToTypes(List(other)) | ||
})) | ||
|
||
New(TypeTree.ref(symbol)).select(symbol.primaryConstructor) | ||
.appliedToTypes(symbol.typeRef.typeArgs.map(_ => TypeRepr.of[Null])) | ||
.appliedToArgss(constructorFieldsFilledWithNulls) | ||
.asExprOf[T] |
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
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