Skip to content

Commit

Permalink
Move all syntactic sugar into syntax package (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
dieproht authored Jun 5, 2024
1 parent e0b3f1c commit 4aa72b6
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 81 deletions.
7 changes: 7 additions & 0 deletions molly-core/src/main/scala/molly/core/MollyCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ import org.bson.BsonDocument
trait MollyCodec[F[_], A](using Async[F]):
def decode(doc: BsonDocument): F[A]
def encode(obj: A): F[BsonDocument]

object MollyCodec:

given bsonDocumentCodec[F[_]](using f: Async[F]): MollyCodec[F, BsonDocument] =
new MollyCodec[F, BsonDocument]:
override def decode(doc: BsonDocument): F[BsonDocument] = f.pure(doc)
override def encode(obj: BsonDocument): F[BsonDocument] = f.pure(obj)
18 changes: 9 additions & 9 deletions molly-core/src/main/scala/molly/core/MollyCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import com.mongodb.client.result.InsertManyResult
import com.mongodb.client.result.InsertOneResult
import com.mongodb.client.result.UpdateResult
import com.mongodb.reactivestreams.client.MongoCollection
import molly.core.model.BulkWriteOptions
import molly.core.model.CreateIndexOptions
import molly.core.model.FindOneAndReplaceOptions
import molly.core.model.FindOneAndUpdateOptions
import molly.core.model.IndexModel
import molly.core.model.IndexOptions
import molly.core.model.ReplaceOptions
import molly.core.model.UpdateOptions
import molly.core.model.WriteModel
import molly.core.query.AggregateQuery
import molly.core.query.FindQuery
import molly.core.query.WatchQuery
import molly.core.reactivestreams.fromOptionPublisher
import molly.core.reactivestreams.fromSinglePublisher
import molly.core.reactivestreams.fromStreamPublisher
import molly.core.reactivestreams.fromVoidPublisher
import molly.core.syntax.model.BulkWriteOptions
import molly.core.syntax.model.CreateIndexOptions
import molly.core.syntax.model.FindOneAndReplaceOptions
import molly.core.syntax.model.FindOneAndUpdateOptions
import molly.core.syntax.model.IndexModel
import molly.core.syntax.model.IndexOptions
import molly.core.syntax.model.ReplaceOptions
import molly.core.syntax.model.UpdateOptions
import molly.core.syntax.model.WriteModel
import org.bson.BsonDocument
import org.bson.Document
import org.bson.conversions.Bson
Expand Down
3 changes: 1 addition & 2 deletions molly-core/src/main/scala/molly/core/MollyDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.mongodb.ReadConcern
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern
import com.mongodb.reactivestreams.client.MongoDatabase
import molly.core.bsondocument.BsonDocumentCollection
import molly.core.reactivestreams.fromStreamPublisher
import molly.core.syntax.bsondocument.BsonDocumentCollection
import org.bson.BsonDocument

/** Molly's counterpart to
Expand All @@ -19,7 +19,6 @@ final case class MollyDatabase[F[_]] private[core] (private[core] val delegate:
/** [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#getCollection(java.lang.String)]]
*/
def getCollection(collectionName: String): F[BsonDocumentCollection[F]] =
import bsondocument.bsonDocumentCodec
f.delay(delegate.getCollection(collectionName, classOf[BsonDocument])).map(MollyCollection(_))

/** Like [[this.getCollection]], but returns a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final case class MollySyncDatabase[F[_]] private[core] (private[core] val delega
/** [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-sync/com/mongodb/client/MongoDatabase.html#getCollection(java.lang.String)]]
*/
def getCollection(collectionName: String): F[MollySyncCollection[F, BsonDocument]] =
import bsondocument.bsonDocumentCodec
f.delay(delegate.getCollection(collectionName, classOf[BsonDocument])).map(MollySyncCollection(_))

/** Like [[this.getCollection]], but returns a
Expand Down
33 changes: 0 additions & 33 deletions molly-core/src/main/scala/molly/core/bsondocument.scala

This file was deleted.

2 changes: 1 addition & 1 deletion molly-core/src/main/scala/molly/core/syntax/all.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package molly.core.syntax

object all extends bsondocument with filters
object all extends bson, bsondocument, filters, model
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package molly.core
package molly.core.syntax

import scala.jdk.CollectionConverters.*

/** Convenience layer over [[org.bson.conversions.Bson the Java driver's Bson class]].
/** Syntactic sugar for
* [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/bson/org/bson/package-summary.html the Java driver's bson classes]].
*/

object bson:
trait bson:
type BsonArray = org.bson.BsonArray

type BsonValue = org.bson.BsonValue
Expand Down Expand Up @@ -107,3 +107,5 @@ object bson:
object BsonUndefined:

def apply(): BsonUndefined = new BsonUndefined()

object bson extends bson
24 changes: 22 additions & 2 deletions molly-core/src/main/scala/molly/core/syntax/bsondocument.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package molly.core.syntax

import org.bson.*
import molly.core.MollyCollection
import org.bson.BsonValue

import scala.reflect.ClassTag
import scala.reflect.classTag
import scala.util.Failure
import scala.util.Success
import scala.util.Try

/** Syntax extensions for [[org.bson.BsonDocument the Java driver's BsonDocument class]].
/** Syntactic sugar for [[org.bson.BsonDocument the Java driver's BsonDocument class]].
*/
trait bsondocument:

type BsonDocumentCollection[F[_]] = MollyCollection[F, BsonDocument]

type BsonDocument = org.bson.BsonDocument

object BsonDocument:
def apply(): BsonDocument = new BsonDocument()

def apply(key: String, value: BsonValue) = new BsonDocument(key, value)

def apply(elems: Iterable[(String, BsonValue)]): BsonDocument =
val bsonDoc = new BsonDocument()
elems.foreach(e => bsonDoc.put(e._1, e._2))
bsonDoc

def apply(elems: (String, BsonValue)*): BsonDocument =
val bsonDoc = new BsonDocument()
elems.foreach(e => bsonDoc.put(e._1, e._2))
bsonDoc

extension (bsonDoc: BsonDocument)

/** Append an optional value with the given key to the document.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package molly.core
package molly.core.syntax

import org.bson.BsonDocument
import org.bson.conversions.Bson

/** Convenience layer over
/** Syntactic sugar for
* [[https://mongodb.github.io/mongo-java-driver/5.1/apidocs/mongodb-driver-core/com/mongodb/client/model/package-summary.html the Java driver's model classes]].
*/
object model:
trait model:

type BulkWriteOptions = com.mongodb.client.model.BulkWriteOptions

Expand Down Expand Up @@ -93,3 +93,5 @@ object model:
def apply(): UpdateOptions = new UpdateOptions()

type WriteModel = com.mongodb.client.model.WriteModel[BsonDocument]

object model extends model
10 changes: 5 additions & 5 deletions molly-core/src/test/scala/molly/core/BsonDocumentTest.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package molly.core

import molly.core.bson.BsonDateTime
import molly.core.bson.BsonDouble
import molly.core.bson.BsonInt32
import molly.core.bson.BsonString
import molly.core.bsondocument.BsonDocument
import molly.core.syntax.bson.BsonDateTime
import molly.core.syntax.bson.BsonDouble
import molly.core.syntax.bson.BsonInt32
import molly.core.syntax.bson.BsonString
import molly.core.syntax.bsondocument.BsonDocument
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand Down
2 changes: 1 addition & 1 deletion molly-core/src/test/scala/molly/core/BsonValueTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package molly.core

import molly.core.bson.*
import molly.core.syntax.bson.*
import org.bson.types.Decimal128
import org.bson.types.ObjectId
import org.scalatest.flatspec.AnyFlatSpec
Expand Down
22 changes: 11 additions & 11 deletions molly-core/src/test/scala/molly/core/MollyCollectionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import com.mongodb.client.model.Projections
import com.mongodb.client.model.ReturnDocument
import com.mongodb.client.model.Sorts
import com.mongodb.client.model.Updates
import molly.core.bsondocument.BsonDocumentCollection
import molly.core.model.CreateIndexOptions
import molly.core.model.DeleteOneModel
import molly.core.model.FindOneAndReplaceOptions
import molly.core.model.FindOneAndUpdateOptions
import molly.core.model.IndexModel
import molly.core.model.IndexOptions
import molly.core.model.InsertOneModel
import molly.core.model.ReplaceOptions
import molly.core.model.UpdateOptions
import molly.core.model.WriteModel
import molly.core.syntax.bsondocument.BsonDocumentCollection
import molly.core.syntax.model.CreateIndexOptions
import molly.core.syntax.model.DeleteOneModel
import molly.core.syntax.model.FindOneAndReplaceOptions
import molly.core.syntax.model.FindOneAndUpdateOptions
import molly.core.syntax.model.IndexModel
import molly.core.syntax.model.IndexOptions
import molly.core.syntax.model.InsertOneModel
import molly.core.syntax.model.ReplaceOptions
import molly.core.syntax.model.UpdateOptions
import molly.core.syntax.model.WriteModel
import org.bson.BsonDocument
import org.bson.BsonInt32
import org.bson.BsonString
Expand Down
4 changes: 2 additions & 2 deletions molly-core/src/test/scala/molly/core/MollyDatabaseTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package molly.core

import cats.effect.IO
import com.dimafeng.testcontainers.MongoDBContainer
import molly.core.bson.*
import molly.core.bsondocument.*
import molly.core.syntax.bson.*
import molly.core.syntax.bsondocument.*
import org.testcontainers.utility.DockerImageName
import weaver.IOSuite

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.effect.IO
import com.dimafeng.testcontainers.MongoDBContainer
import com.mongodb.client.model.Filters
import com.mongodb.client.model.Updates
import molly.core.bsondocument.BsonDocumentCollection
import molly.core.syntax.bsondocument.BsonDocumentCollection
import org.bson.BsonDocument
import org.bson.BsonInt32
import org.testcontainers.utility.DockerImageName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package molly.core

import cats.effect.IO
import com.dimafeng.testcontainers.MongoDBContainer
import molly.core.bson.*
import molly.core.bsondocument.*
import molly.core.syntax.bson.*
import molly.core.syntax.bsondocument.*
import org.testcontainers.utility.DockerImageName
import weaver.IOSuite

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package molly.core.syntax

import cats.syntax.option.*
import molly.core.bson.*
import molly.core.bsondocument.BsonDocument
import molly.core.syntax.bson.*
import molly.core.syntax.bsondocument.*
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import molly.core.MollyClient
import molly.core.MollyCollection
import molly.core.MollyTestSupport
import molly.core.TestContainerForAll
import molly.core.model.FindOneAndReplaceOptions
import molly.core.model.ReplaceOptions
import molly.core.syntax.model.FindOneAndReplaceOptions
import molly.core.syntax.model.ReplaceOptions
import org.testcontainers.utility.DockerImageName
import weaver.IOSuite

Expand Down

0 comments on commit 4aa72b6

Please sign in to comment.