Skip to content

Commit

Permalink
Define SingletonType for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Jul 2, 2024
1 parent 1c787f7 commit 4f63fc5
Showing 1 changed file with 44 additions and 0 deletions.
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

View check run for this annotation

Codecov / codecov/patch

chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala#L17-L32

Added lines #L17 - L32 were not covered by tests
// $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

View check run for this annotation

Codecov / codecov/patch

chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala#L34-L35

Added lines #L34 - L35 were not covered by tests
)
// $COVERAGE-ON$
}
case _ => None

Check warning on line 39 in chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala

View check run for this annotation

Codecov / codecov/patch

chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala#L39

Added line #L39 was not covered by tests
}
}
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

View check run for this annotation

Codecov / codecov/patch

chimney-macro-commons/src/main/scala/io/scalaland/chimney/internal/compiletime/datatypes/SingletonTypes.scala#L42

Added line #L42 was not covered by tests
}
}

0 comments on commit 4f63fc5

Please sign in to comment.