Skip to content

Commit

Permalink
Create Chimney module
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Oct 4, 2024
1 parent 13a28c5 commit 47050d2
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ class EnumPartialTransformerSpec extends munit.FunSuite {
test("Convert between enums") {

assertEquals(
Enum[TestEnumeration.TestEnumeration].values.transformInto[Vector[TestEnumeratum]],
Enum[TestEnumeratum].values
Enum[TestEnumeration.TestEnumeration].values.transformIntoPartial[Vector[TestEnumeratum]].asOption,
Some(Enum[TestEnumeratum].values)
)

assertEquals(
Enum[TestEnumeratum].values.transformInto[Vector[TestSumType]],
Enum[TestSumType].values
Enum[TestEnumeratum].values.transformIntoPartial[Vector[TestSumType]].asOption,
Some(Enum[TestSumType].values)
)

assertEquals(
Enum[TestSumType].values.transformInto[Vector[TestEnumeration.TestEnumeration]],
Enum[TestEnumeration.TestEnumeration].values
Enum[TestSumType].values.transformIntoPartial[Vector[TestEnumeration.TestEnumeration]].asOption,
Some(Enum[TestEnumeration.TestEnumeration].values)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.scalaland.enumz

import scala.language.experimental.macros

trait EnumerationImplicits {
private[enumz] trait EnumerationImplicits {

implicit def enumerationEnum[E <: Enumeration#Value]: Enum[E] = macro internal.EnumerationMacros.`enum`[E]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.scalaland.enumz

import scala.language.experimental.macros

trait EnumeratumImplicits {
private[enumz] trait EnumeratumImplicits {

implicit def enumeratumEnum[E <: enumeratum.EnumEntry]: Enum[E] = macro internal.EnumeratumMacros.`enum`[E]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.scalaland.enumz

import scala.language.experimental.macros

trait SumTypeEnumImplicits {
private[enumz] trait SumTypeEnumImplicits {

implicit def sumTypeEnum[E]: Enum[E] = macro internal.SumTypeEnumMacros.`enum`[E]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.scalaland.enumz.internal

import io.scalaland.enumz.Enum
import scala.reflect.macros.blackbox.*
import scala.reflect.macros.whitebox.*

object SumTypeEnumMacros {

Expand All @@ -18,7 +18,7 @@ object SumTypeEnumMacros {
val order = Ordering.fromLessThan[Position]((a, b) => a.line < b.line || (a.line == b.line && a.column < b.column))
val children: List[CSymbol] = symbol.knownDirectSubclasses.toList.sortBy(_.pos)(order)
if (!children.forall(_.isModuleClass)) {
c.abort(c.enclosingPosition, "All children must be (case) objects.")
c.abort(c.enclosingPosition, s"All children of ${weakTypeOf[E].dealias} must be (case) objects.")
}

val sourceModuleRef: CSymbol => Ident = sym => Ident(sym.asInstanceOf[ISymbol].sourceModule.asInstanceOf[CSymbol])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.scalaland.enumz

trait EnumerationImplicits {
private[enumz] trait EnumerationImplicits {

inline given enumerationEnum[E <: Enumeration#Value]: Enum[E] = ${ internal.EnumerationMacros.`enum`[E] }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.scalaland.enumz

trait EnumeratumImplicits {
private[enumz] trait EnumeratumImplicits {

inline given enumeratumEnum[E <: enumeratum.EnumEntry]: Enum[E] = ${ internal.EnumeratumMacros.`enum`[E] }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.scalaland.enumz

trait SumTypeEnumImplicits {
private[enumz] trait SumTypeEnumImplicits {

inline given sumTypeEnum[E]: Enum[E] = ${ internal.SumTypeEnumMacros.`enum`[E] }
transparent inline given sumTypeEnum[E]: Enum[E] = ${ internal.SumTypeEnumMacros.`enum`[E] }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object SumTypeEnumMacros {
def extractRecursively(sym: Symbol): List[Symbol] =
if sym.flags.is(Flags.Sealed) then sym.children.flatMap(extractRecursively)
else if sym.flags.is(Flags.Enum) || sym.flags.is(Flags.Module) then List(sym)
else report.errorAndAbort("All children must be (case) objects.")
else report.errorAndAbort(s"All children of ${E.show} must be (case) objects.")

given Ordering[Option[Position]] = Ordering.Option(Ordering.fromLessThan[Position] { (a, b) =>
a.startLine < b.startLine || (a.startLine == b.startLine && a.startColumn < b.startColumn)
Expand Down
9 changes: 5 additions & 4 deletions enumz/src/main/scala/io/scalaland/enumz/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ trait Enum[E] extends Dynamic {
def selectDynamic(name: String): E = withName(name)
}

object Enum extends Implicits {
object Enum extends Implicits0 {

def apply[E](implicit `enum`: Enum[E]): Enum[E] = `enum`
}

trait Implicits extends JavaEnumImplicits with EnumerationImplicits with EnumeratumImplicits with LowPriorityImplicits

trait LowPriorityImplicits extends SumTypeEnumImplicits
private[enumz] trait Implicits0 extends JavaEnumImplicits with Implicits1
private[enumz] trait Implicits1 extends EnumerationImplicits with Implicits2
private[enumz] trait Implicits2 extends EnumeratumImplicits with Implicits3
private[enumz] trait Implicits3 extends SumTypeEnumImplicits
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalaland.enumz

// empty trait made to simplify composition of implicits' traits in shared part
trait JavaEnumImplicits
private[enumz] trait JavaEnumImplicits
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.scalaland.enumz

import scala.language.experimental.macros

trait JavaEnumImplicits {
private[enumz] trait JavaEnumImplicits {

implicit def javaEnumEnum[E <: java.lang.Enum[E]]: Enum[E] = macro internal.JavaEnumMacros.`enum`[E]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalaland.enumz

// Java Enums are handled by sealed/enum macro
trait JavaEnumImplicits
private[enumz] trait JavaEnumImplicits
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalaland.enumz

// empty trait made to simplify composition of implicits' traits in shared part
trait JavaEnumImplicits
private[enumz] trait JavaEnumImplicits

0 comments on commit 47050d2

Please sign in to comment.