Skip to content

Commit 8f61675

Browse files
arve0hvesalai
authored andcommitted
Use Enumeration type in custom-scalar-types-example, issue slick#2028
1 parent efce62c commit 8f61675

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

doc/code/LiftedEmbedding.scala

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,22 @@ object LiftedEmbedding extends App {
542542

543543
{
544544
//#mappedtype1
545-
// An algebraic data type for booleans
546-
sealed trait Bool
547-
case object True extends Bool
548-
case object False extends Bool
549-
550-
// And a ColumnType that maps it to Int values 1 and 0
551-
implicit val boolColumnType = MappedColumnType.base[Bool, Int](
552-
{ b => if(b == True) 1 else 0 }, // map Bool to Int
553-
{ i => if(i == 1) True else False } // map Int to Bool
554-
)
545+
// Custom data type for booleans that maps to NUMBER in database
546+
object Bool extends Enumeration {
547+
type Bool = Value
548+
val True, False = Value
549+
550+
// A ColumnType that maps it to NUMBER values 1 and 0
551+
val columnMapper = MappedColumnType.base[Bool, Int](
552+
{ case True => 1; case False => 0 }, // map Bool to NUMBER
553+
{ i => if (i == 1) True else False } // map NUMBER to Bool
554+
)
555+
}
556+
557+
// Make columnMapper available in table definitions and where you do queries
558+
implicit val boolColumnType = Bool.columnMapper
555559

556-
// You can now use Bool like any built-in column type (in tables, queries, etc.)
560+
// You can now use Bool.{True, False} like any built-in column type (in tables, queries, etc.)
557561
//#mappedtype1
558562
}
559563

0 commit comments

Comments
 (0)