-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3208 from softwaremill/is-scala-enum
Refactor reused enum-based macros
- Loading branch information
Showing
6 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
core/src/main/scala-3/sttp/tapir/internal/EnumerationMacros.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,44 @@ | ||
package sttp.tapir.internal | ||
|
||
import scala.quoted.* | ||
|
||
private[tapir] object EnumerationMacros: | ||
|
||
transparent inline def isEnumeration[T]: Boolean = inline compiletime.erasedValue[T] match | ||
case _: Null => false | ||
case _: Nothing => false | ||
case _: reflect.Enum => allChildrenObjectsOrEnumerationCases[T] | ||
case _ => false | ||
|
||
/** Checks whether type T has child types, and all children of type T are objects or enum cases or sealed parents of such. Useful for | ||
* determining whether an enum is indeed an enum, or will be desugared to a sealed hierarchy, in which case it's not really an | ||
* enumeration in context of schemas and JSON codecs. | ||
*/ | ||
inline def allChildrenObjectsOrEnumerationCases[T]: Boolean = ${ allChildrenObjectsOrEnumerationCasesImpl[T] } | ||
|
||
def allChildrenObjectsOrEnumerationCasesImpl[T: Type](using q: Quotes): Expr[Boolean] = | ||
Expr(enumerationTypeChildren[T](failOnError = false).nonEmpty) | ||
|
||
/** Recursively scans a symbol and builds a list of all children and their children, as long as all of them are objects or enum cases or | ||
* sealed parents of such. Useful for determining whether an enum is indeed an enum, or will be desugared to a sealed hierarchy, in which | ||
* case it's not really an enumeration (in context of schemas and JSON codecs). | ||
*/ | ||
def enumerationTypeChildren[T: Type](failOnError: Boolean)(using q: Quotes): List[q.reflect.Symbol] = | ||
import quotes.reflect.* | ||
|
||
val tpe = TypeRepr.of[T] | ||
val symbol = tpe.typeSymbol | ||
|
||
def flatChildren(s: Symbol): List[Symbol] = s.children.toList.flatMap { c => | ||
if (c.isClassDef) { | ||
if (!(c.flags is Flags.Sealed)) | ||
if (failOnError) | ||
report.errorAndAbort("All children must be objects or enum cases, or sealed parent of such.") | ||
else | ||
Nil | ||
else | ||
flatChildren(c) | ||
} else List(c) | ||
} | ||
|
||
flatChildren(symbol) |
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