-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
384 additions
and
272 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
enumeratum-core/src/main/scala-3/enumeratum/EnumCompat.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
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
2 changes: 0 additions & 2 deletions
2
enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package enumeratum.values | ||
|
||
import _root_.enumeratum.Enum | ||
|
||
/** Base trait for a Value-based enums. | ||
* | ||
* Example: | ||
|
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
13 changes: 13 additions & 0 deletions
13
enumeratum-core/src/test/scala/enumeratum/values/NumericPrecision.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,13 @@ | ||
package enumeratum.values | ||
|
||
sealed abstract class NumericPrecision(val value: String) extends StringEnumEntry with AllowAlias | ||
|
||
object NumericPrecision extends StringEnum[NumericPrecision] { | ||
case object Integer extends NumericPrecision("integer") | ||
case object Int extends NumericPrecision("integer") | ||
|
||
case object Float extends NumericPrecision("float") | ||
case object Double extends NumericPrecision("double") | ||
|
||
val values = findValues | ||
} |
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
36 changes: 0 additions & 36 deletions
36
macros/compat/src/main/scala-3/enumeratum/ContextUtils.scala
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package test | ||
|
||
import scala.deriving.Mirror | ||
import scala.quoted.{Expr, Quotes, Type} | ||
import scala.reflect.Enum | ||
|
||
object EnumHelper: | ||
|
||
/** {{{ | ||
* enum Color: | ||
* case Red, Green, Blue | ||
* | ||
* import reactivemongo.api.bson.EnumHelper | ||
* | ||
* val valueOf: String => Option[Color] = EnumHelper.strictValueOf[Color] | ||
* | ||
* assert(valueOf("Red") contains Color.Red) | ||
* assert(valueOf("red").isEmpty) | ||
* }}} | ||
*/ | ||
inline def strictValueOf[T](using | ||
Mirror.SumOf[T] | ||
): String => Option[T] = ${ strictValueOfImpl[T] } | ||
|
||
private def strictValueOfImpl[T](using | ||
Quotes, | ||
Type[T] | ||
): Expr[String => Option[T]] = enumValueOfImpl(identity, None) | ||
|
||
private def enumValueOfImpl[T]( | ||
labelNaming: String => String, | ||
normalize: Option[Expr[String => String]] | ||
)(using | ||
q: Quotes, | ||
tpe: Type[T] | ||
): Expr[String => Option[T]] = { | ||
import q.reflect.* | ||
|
||
val tpr = TypeRepr.of(using tpe) | ||
|
||
val compSym = tpr.typeSymbol.companionModule | ||
|
||
if (compSym == Symbol.noSymbol) { | ||
report.errorAndAbort(s"Unresolved type: ${tpr.typeSymbol.fullName}") | ||
} | ||
|
||
val compRef = Ref(compSym) | ||
|
||
val cases = compSym.fieldMembers | ||
.flatMap { fieldSym => | ||
val fieldTerm = compRef.select(fieldSym) | ||
|
||
if (fieldTerm.tpe <:< tpr) { | ||
Seq(fieldSym -> fieldTerm.asExprOf[T]) | ||
} else { | ||
Seq.empty[(Symbol, Expr[T])] | ||
} | ||
} | ||
.zipWithIndex | ||
.map { case ((sym, expr), i) => | ||
val name = sym.name.toLowerCase | ||
val body: Expr[Some[T]] = '{ Some(${ expr }) } | ||
|
||
CaseDef( | ||
Literal(StringConstant(labelNaming(sym.name))), | ||
guard = None, | ||
rhs = body.asTerm | ||
) | ||
} | ||
|
||
val none = CaseDef( | ||
Wildcard(), | ||
None, | ||
'{ Option.empty[T] }.asTerm | ||
) | ||
|
||
def mtch(s: Expr[String]): Expr[Option[T]] = { | ||
Match(s.asTerm, cases :+ none).asExprOf[Option[T]] | ||
} | ||
|
||
normalize match { | ||
case Some(nz) => | ||
'{ (s: String) => | ||
val in = ${ nz }(s) | ||
${ mtch('in) } | ||
} | ||
|
||
case _ => | ||
'{ (s: String) => ${ mtch('s) } } | ||
} | ||
} | ||
end EnumHelper |
Oops, something went wrong.