Skip to content

Commit f823e5c

Browse files
committed
Refactor ReactiveMongo code (#34)
- Optimise Reading BSON values - Rename handler to bsonHandler for consistency - Add check for convertable to Short
1 parent b1a8cd4 commit f823e5c

File tree

5 files changed

+15
-33
lines changed

5 files changed

+15
-33
lines changed

enumeratum-reactivemongo-bson/compat/src/main/scala-2.11/enumeratum/values/BSONValueHandlers.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import reactivemongo.bson.{ BSONHandler, BSONInteger, BSONLong, BSONReader, BSON
1414
*/
1515
object BSONValueHandlers extends BSONValueReads with BSONValueWrites {
1616

17-
implicit def bsonHandler[A](implicit reader: BSONReader[BSONValue, A], writer: BSONWriter[A, BSONValue]) = new BSONHandler[BSONValue, A] {
17+
implicit def anyBsonHandler[A](implicit reader: BSONReader[BSONValue, A], writer: BSONWriter[A, BSONValue]) = new BSONHandler[BSONValue, A] {
1818
def write(t: A): BSONValue = writer.write(t)
1919
def read(bson: BSONValue): A = reader.read(bson)
2020
}
@@ -25,7 +25,7 @@ trait BSONValueReads {
2525

2626
implicit val bsonReaderShort = new BSONReader[BSONValue, Short] {
2727
override def read(bson: BSONValue): Short = bson match {
28-
case BSONInteger(x) => x.toShort
28+
case BSONInteger(x) if Short.MaxValue >= x && Short.MinValue <= x => x.toShort
2929
case _ => throw new RuntimeException(s"Could not convert $bson to Short")
3030
}
3131
}

enumeratum-reactivemongo-bson/compat/src/main/scala-2.11/enumeratum/values/EnumHandler.scala

+2-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,8 @@ object EnumHandler {
1515
*/
1616
def reader[ValueType <: AnyVal, EntryType <: ValueEnumEntry[ValueType]](enum: ValueEnum[ValueType, EntryType])(implicit baseBsonReader: BSONReader[BSONValue, ValueType]): BSONReader[BSONValue, EntryType] = new BSONReader[BSONValue, EntryType] {
1717
override def read(bson: BSONValue): EntryType = {
18-
val result: Try[EntryType] = baseBsonReader.readTry(bson).flatMap { s =>
19-
val maybeBound = enum.withValueOpt(s)
20-
maybeBound match {
21-
case Some(obj) => Success(obj)
22-
case None => Failure(
23-
new RuntimeException(s"Enumeration expected of type: '$enum', but it does not appear to contain the value: '$s'")
24-
)
25-
}
26-
}
27-
28-
result.get
18+
val value = baseBsonReader.read(bson)
19+
enum.withValue(value)
2920
}
3021
}
3122

enumeratum-reactivemongo-bson/compat/src/main/scala-2.11/enumeratum/values/ReactiveMongoBsonValueEnum.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sealed trait ReactiveMongoBsonValueEnum[ValueType <: AnyVal, EntryType <: ValueE
1414
/**
1515
* Implicit BSON handler for the entries of this enum
1616
*/
17-
implicit def handler: BSONHandler[BSONValue, EntryType]
17+
implicit def bsonHandler: BSONHandler[BSONValue, EntryType]
1818
}
1919

2020
/**
@@ -23,7 +23,7 @@ sealed trait ReactiveMongoBsonValueEnum[ValueType <: AnyVal, EntryType <: ValueE
2323
trait IntReactiveMongoBsonValueEnum[EntryType <: IntEnumEntry] extends ReactiveMongoBsonValueEnum[Int, EntryType] {
2424
this: IntEnum[EntryType] =>
2525

26-
implicit val handler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
26+
implicit val bsonHandler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
2727
}
2828

2929
/**
@@ -32,7 +32,7 @@ trait IntReactiveMongoBsonValueEnum[EntryType <: IntEnumEntry] extends ReactiveM
3232
trait LongReactiveMongoBsonValueEnum[EntryType <: LongEnumEntry] extends ReactiveMongoBsonValueEnum[Long, EntryType] {
3333
this: LongEnum[EntryType] =>
3434

35-
implicit val handler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
35+
implicit val bsonHandler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
3636
}
3737

3838
/**
@@ -41,5 +41,5 @@ trait LongReactiveMongoBsonValueEnum[EntryType <: LongEnumEntry] extends Reactiv
4141
trait ShortReactiveMongoBsonValueEnum[EntryType <: ShortEnumEntry] extends ReactiveMongoBsonValueEnum[Short, EntryType] {
4242
this: ShortEnum[EntryType] =>
4343

44-
implicit val handler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
44+
implicit val bsonHandler: BSONHandler[BSONValue, EntryType] = EnumHandler.handler(this)
4545
}

enumeratum-reactivemongo-bson/compat/src/test/scala-2.11/enumeratum/values/EnumBsonHandlerSpec.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class EnumBsonHandlerSpec extends FunSpec with Matchers with EnumBsonHandlerHelp
3030
testHandler("IntEnum", LibraryItem)
3131
testHandler("LongEnum", ContentType)
3232
testHandler("ShortEnum", Drinks)
33-
testHandler("ShortReactiveMongoBsonValueEnum", BsonDrinks, Some(BsonDrinks.handler))
34-
testHandler("LongReactiveMongoBsonValueEnum", BsonContentType, Some(BsonContentType.handler))
35-
testHandler("IntReactiveMongoBsonValueEnum", BsonLibraryItem, Some(BsonLibraryItem.handler))
33+
testHandler("ShortReactiveMongoBsonValueEnum", BsonDrinks, Some(BsonDrinks.bsonHandler))
34+
testHandler("LongReactiveMongoBsonValueEnum", BsonContentType, Some(BsonContentType.bsonHandler))
35+
testHandler("IntReactiveMongoBsonValueEnum", BsonLibraryItem, Some(BsonLibraryItem.bsonHandler))
3636

3737
}
3838

enumeratum-reactivemongo-bson/src/main/scala/enumeratum/EnumHandler.scala

+4-13
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,11 @@ object EnumHandler {
2020
def reader[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): BSONReader[BSONValue, A] =
2121
new BSONReader[BSONValue, A] {
2222
override def read(bson: BSONValue): A = {
23-
val result = bson match {
24-
case BSONString(s) => {
25-
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
26-
maybeBound match {
27-
case Some(obj) => Success(obj)
28-
case None => Failure(new RuntimeException(
29-
s"Enumeration expected of type: '$enum', but it does not appear to contain the value: '$s'"
30-
))
31-
}
32-
}
33-
case _ => Failure(new RuntimeException("String value expected"))
23+
bson match {
24+
case BSONString(s) if insensitive => enum.withNameInsensitive(s)
25+
case BSONString(s) => enum.withName(s)
26+
case _ => throw new RuntimeException("String value expected")
3427
}
35-
36-
result.get
3728
}
3829
}
3930

0 commit comments

Comments
 (0)