Skip to content

Commit c34a25c

Browse files
committed
Review changes: use HasGenCodec and pattern matching
1 parent 42fa4eb commit c34a25c

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

commons-mongo/jvm/src/main/scala/com/avsystem/commons/mongo/BsonReaderInput.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class BsonReaderInput(br: BsonReader, override val legacyOptionEncoding: Boolean
1616
override def readString(): String = br.readString()
1717
override def readBoolean(): Boolean = br.readBoolean()
1818
override def readInt(): Int = br.readInt32()
19-
override def readLong(): Long = {
20-
if (bsonType == BsonType.INT32) br.readInt32().toLong // allow converting INT32 to Long
21-
else br.readInt64()
19+
override def readLong(): Long = bsonType match {
20+
case BsonType.INT32 => br.readInt32().toLong // allow converting INT32 to Long
21+
case _ => br.readInt64()
2222
}
2323
override def readTimestamp(): Long = br.readDateTime()
2424
override def readDouble(): Double = br.readDouble()

commons-mongo/jvm/src/main/scala/com/avsystem/commons/mongo/BsonValueInput.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class BsonValueInput(bsonValue: BsonValue, override val legacyOptionEncoding: Bo
2323
def readBoolean(): Boolean = handleFailures(bsonValue.asBoolean().getValue)
2424
def readInt(): Int = handleFailures(bsonValue.asInt32().getValue)
2525
def readLong(): Long = handleFailures {
26-
if (bsonType == BsonType.INT32) bsonValue.asInt32().getValue.toLong // allow converting INT32 to Long
27-
else bsonValue.asInt64().getValue
26+
bsonType match {
27+
case BsonType.INT32 => bsonValue.asInt32().getValue.toLong // allow converting INT32 to Long
28+
case _ => bsonValue.asInt64().getValue
29+
}
2830
}
2931
override def readTimestamp(): Long = handleFailures(bsonValue.asDateTime().getValue)
3032
def readDouble(): Double = handleFailures(bsonValue.asDouble().getValue)

commons-mongo/jvm/src/test/scala/com/avsystem/commons/mongo/SomethingPlain.scala

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.avsystem.commons
22
package mongo
33

44
import com.avsystem.commons.misc.Bytes
5-
import com.avsystem.commons.serialization.GenCodec
5+
import com.avsystem.commons.serialization.HasGenCodec
66
import org.scalacheck.Arbitrary.arbitrary
77
import org.scalacheck.Gen
88

@@ -17,7 +17,7 @@ case class SomethingPlain(
1717
list: List[String],
1818
map: Map[String, String]
1919
)
20-
object SomethingPlain {
20+
object SomethingPlain extends HasGenCodec[SomethingPlain] {
2121
def sizedListOf[T](maxSize: Int, gen: => Gen[T]): Gen[List[T]] = {
2222
Gen.resize(maxSize, Gen.listOf(gen))
2323
}
@@ -50,8 +50,6 @@ object SomethingPlain {
5050
list,
5151
map
5252
)
53-
54-
implicit val codec: GenCodec[SomethingPlain] = GenCodec.materialize
5553
}
5654

5755
case class SomethingComplex(
@@ -61,7 +59,7 @@ case class SomethingComplex(
6159
nestedComplexList: List[List[SomethingPlain]],
6260
option: Option[Int]
6361
)
64-
object SomethingComplex {
62+
object SomethingComplex extends HasGenCodec[SomethingComplex] {
6563
val sthListGen: Gen[List[SomethingPlain]] = SomethingPlain.sizedListOf(8, SomethingPlain.gen)
6664

6765
val gen: Gen[SomethingComplex] = for {
@@ -77,11 +75,7 @@ object SomethingComplex {
7775
nestedComplexList,
7876
option
7977
)
80-
81-
implicit val codec: GenCodec[SomethingComplex] = GenCodec.materialize
8278
}
8379

8480
case class SomethingLong(value: Long)
85-
object SomethingLong {
86-
implicit val codec: GenCodec[SomethingLong] = GenCodec.materialize
87-
}
81+
object SomethingLong extends HasGenCodec[SomethingLong]

0 commit comments

Comments
 (0)