-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.scalaland.chimney.internal.compiletime.datatypes | ||
|
||
import io.scalaland.chimney.internal.compiletime.Definitions | ||
|
||
trait SingletonTypes { this: (Definitions & ProductTypes) => | ||
|
||
/** Describes all types which are singletons (singleton literal types, Unit, Null, case objects, vals, ...). | ||
* | ||
* Should have the same behavior as `ValueOf` without relying on it (it's unavailable in Scala 2.12). | ||
*/ | ||
final protected case class Singleton[A](value: Expr[A]) | ||
|
||
protected object SingletonType { | ||
|
||
import Type.Implicits.* | ||
|
||
final def parse[A: Type]: Option[Singleton[A]] = { | ||
def found[B](b: Expr[B]): Option[Singleton[A]] = Some(Singleton(b.asInstanceOf[Expr[A]])) | ||
Type[A] match { | ||
case _ if Type[A] <:< Type[Unit] => found(Expr.Unit) | ||
case _ if Type[A] <:< Type[Null] => found(Expr.Null) | ||
case Type.BooleanLiteral(b) => found(Expr.Boolean(b.value)) | ||
case Type.IntLiteral(i) => found(Expr.Int(i.value)) | ||
case Type.LongLiteral(l) => found(Expr.Long(l.value)) | ||
case Type.FloatLiteral(f) => found(Expr.Float(f.value)) | ||
case Type.DoubleLiteral(d) => found(Expr.Double(d.value)) | ||
case Type.CharLiteral(c) => found(Expr.Char(c.value)) | ||
case Type.StringLiteral(s) => found(Expr.String(s.value)) | ||
case _ if ProductType.isCaseObject[A] || ProductType.isCaseVal[A] || ProductType.isJavaEnumValue[A] => | ||
Type[A] match { | ||
case Product.Constructor(params, ctor) if params.isEmpty => found(ctor(Map.empty)) | ||
case _ => | ||
Check warning on line 32 in chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala
|
||
// $COVERAGE-OFF$should never happen unless we messed up | ||
assertionFailed( | ||
s"Expected case object/case with no params/Java enum of ${Type.prettyPrint[A]} to have a nullary constructor" | ||
Check warning on line 35 in chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala
|
||
) | ||
// $COVERAGE-ON$ | ||
} | ||
case _ => None | ||
Check warning on line 39 in chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala
|
||
} | ||
} | ||
final def unapply[A](tpe: Type[A]): Option[Singleton[A]] = parse(tpe) | ||
Check warning on line 42 in chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala
|
||
} | ||
} |