-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BsonWriter typeclass #12
- Loading branch information
Showing
11 changed files
with
74 additions
and
9 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
3 changes: 3 additions & 0 deletions
3
core/src/main/scala/io/github/weakteam/mongo/AllInstances.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,3 @@ | ||
package io.github.weakteam.mongo | ||
|
||
trait AllInstances |
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,5 @@ | ||
package io.github.weakteam.mongo | ||
|
||
import io.github.weakteam.mongo.bson.BsonWriter | ||
|
||
trait AllSyntax extends BsonWriter.ToBsonWriterOps |
2 changes: 1 addition & 1 deletion
2
...io/github/weakteam/bson/BsonSubtype.scala → ...hub/weakteam/mongo/bson/BsonSubtype.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,4 +1,4 @@ | ||
package io.github.weakteam.bson | ||
package io.github.weakteam.mongo.bson | ||
|
||
import cats.Show | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...a/io/github/weakteam/bson/BsonValue.scala → ...ithub/weakteam/mongo/bson/BsonValue.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
19 changes: 19 additions & 0 deletions
19
core/src/main/scala/io/github/weakteam/mongo/bson/BsonWriter.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,19 @@ | ||
package io.github.weakteam.mongo.bson | ||
|
||
import cats.Contravariant | ||
import simulacrum.typeclass | ||
|
||
@typeclass | ||
trait BsonWriter[-T] { self => | ||
def writeBson(arg: T): BsonValue | ||
} | ||
|
||
object BsonWriter { | ||
def instance[A](f: A => BsonValue): BsonWriter[A] = f(_) | ||
|
||
implicit val bsonWriterContravariantInstance: Contravariant[BsonWriter] = new Contravariant[BsonWriter] { | ||
def contramap[A, B](fa: BsonWriter[A])(f: B => A): BsonWriter[B] = { arg => | ||
fa.writeBson(f(arg)) | ||
} | ||
} | ||
} |
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,3 @@ | ||
package io.github.weakteam.mongo | ||
|
||
object implicits extends AllSyntax with AllInstances |
4 changes: 2 additions & 2 deletions
4
...ithub/weakteam/bson/BsonSubtypeSpec.scala → ...weakteam/mongo/bson/BsonSubtypeSpec.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
4 changes: 2 additions & 2 deletions
4
.../github/weakteam/bson/BsonValueSpec.scala → ...b/weakteam/mongo/bson/BsonValueSpec.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
35 changes: 35 additions & 0 deletions
35
core/src/test/scala/io/github/weakteam/mongo/bson/BsonWriterSpec.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,35 @@ | ||
package io.github.weakteam.mongo.bson | ||
|
||
import io.github.weakteam.mongo.bson.BsonValue.BsonInt | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import io.github.weakteam.mongo.implicits._ | ||
import cats.syntax.contravariant._ | ||
|
||
class BsonWriterSpec extends AnyWordSpec with Matchers { | ||
"BsonWriter" should { | ||
"return right value" in { | ||
implicit val intBsonWriter: BsonWriter[Int] = BsonInt(_) | ||
|
||
1.writeBson mustBe BsonInt(1) | ||
} | ||
|
||
"return right value with contramap" in { | ||
implicit val intBsonWriter: BsonWriter[Int] = BsonInt(_) | ||
implicit val anyBsonWriter: BsonWriter[Any] = intBsonWriter.contramap { | ||
case i: Int => i | ||
case _ => 0 | ||
} | ||
|
||
1.writeBson mustBe BsonInt(1) | ||
"".writeBson mustBe BsonInt(0) | ||
1L.writeBson mustBe BsonInt(0) | ||
} | ||
|
||
"return right value with manual instance creation" in { | ||
implicit val intBsonWriter: BsonWriter[Int] = BsonWriter.instance(BsonInt) | ||
|
||
1.writeBson mustBe BsonInt(1) | ||
} | ||
} | ||
} |
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