Skip to content

Commit 22dc1b6

Browse files
committed
Better newtype handling in reactivemongo
1 parent 96cbec9 commit 22dc1b6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

modules/reactivemongo/src/main/scala/derevo/reactivemongo/package.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ package object reactivemongo {
2020
with NewTypeDerivation[BSONDocumentReader] {
2121
def instance[A]: BSONDocumentReader[A] = macro Derevo.delegate[BSONDocumentReader, A]
2222
}
23+
24+
@delegating("reactivemongo.bson.Macros.writer")
25+
object bsonNewtypeWriter extends Derivation[BsonValueWriter] with NewTypeDerivation[BsonValueWriter]
26+
27+
@delegating("reactivemongo.bson.Macros.reader")
28+
object bsonNewtypeReader extends Derivation[BsonValueReader] with NewTypeDerivation[BsonValueReader]
2329
}

modules/reactivemongo/src/test/scala/derevo/reactivemongo/DerivedHandlerSpec.scala

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package derevo.reactivemongo
22

33
import derevo.derive
4+
import io.estatico.newtype.macros.newtype
45
import org.scalatest.Assertion
56
import org.scalatest.matchers.should.Matchers
67
import org.scalatest.refspec.RefSpec
7-
import reactivemongo.bson.{BSONDocumentReader, BSONDocumentWriter, BSONInteger, BSONString, document}
8+
import reactivemongo.bson.{BSONReader, BSONInteger, BSONDocumentWriter, document, BSONString, BSONDocumentReader}
89

910
@derive(bsonDocumentWriter, bsonDocumentReader)
1011
case class Peka(yoba: String, amount: Int)
1112

1213
@derive(bsonDocumentWriter, bsonDocumentReader)
1314
case class Pekarnya[T](yoba: String, peka: T)
1415

16+
object newtypeStuff {
17+
@derive(bsonNewtypeWriter, bsonNewtypeReader)
18+
@newtype
19+
case class Newtypo(str: String)
20+
21+
@derive(bsonDocumentWriter, bsonDocumentReader)
22+
case class NewtypoContainer(newtypo: Newtypo)
23+
}
24+
25+
import newtypeStuff._
26+
1527
class DerivedHandlerSpec extends RefSpec with Matchers {
1628
object `Derived writer` {
1729
def `should serialize`: Assertion = {
@@ -21,6 +33,16 @@ class DerivedHandlerSpec extends RefSpec with Matchers {
2133
)
2234
}
2335

36+
def `should serialize newtype`: Assertion = {
37+
implicitly[BsonValueWriter[Newtypo]].write(Newtypo("azaza")) shouldBe BSONString("azaza")
38+
}
39+
40+
def `should serialize newtype container`: Assertion = {
41+
implicitly[BSONDocumentWriter[NewtypoContainer]].write(NewtypoContainer(Newtypo("azaza"))) shouldBe document(
42+
"newtypo" -> BSONString("azaza"),
43+
)
44+
}
45+
2446
def `should serialize polymorphic classes` = {
2547
implicitly[BSONDocumentWriter[Pekarnya[Peka]]].write(Pekarnya("peka", Peka("azaza", 42))) shouldBe document(
2648
"yoba" -> BSONString("peka"),
@@ -42,6 +64,20 @@ class DerivedHandlerSpec extends RefSpec with Matchers {
4264
) shouldBe Peka("azaza", 42)
4365
}
4466

67+
def `should deserialize newtype`: Assertion = {
68+
implicitly[BsonValueReader[Newtypo]]
69+
.asInstanceOf[BSONReader[BSONString, Newtypo]] // hack to avoid covariance hell
70+
.read(BSONString("azaza")) shouldBe Newtypo("azaza")
71+
}
72+
73+
def `should deserialize newtype container`: Assertion = {
74+
implicitly[BSONDocumentReader[NewtypoContainer]].read(
75+
document(
76+
"newtypo" -> BSONString("azaza"),
77+
)
78+
) shouldBe NewtypoContainer(Newtypo("azaza"))
79+
}
80+
4581
def `should deserialize polymorphic classes`: Assertion = {
4682
implicitly[BSONDocumentReader[Pekarnya[Peka]]].read(
4783
document(

0 commit comments

Comments
 (0)