-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: DocumentIncludes and MongoImplicits to own files
- Loading branch information
QuadStingray
committed
Mar 28, 2023
1 parent
e567be7
commit 753a633
Showing
6 changed files
with
87 additions
and
82 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/scala/dev/mongocamp/driver/DocumentIncludes.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,53 @@ | ||
package dev.mongocamp.driver | ||
|
||
import dev.mongocamp.driver.mongodb.Converter | ||
import dev.mongocamp.driver.mongodb.bson.BsonConverter | ||
import dev.mongocamp.driver.mongodb.database.DatabaseProvider | ||
import org.bson.types.ObjectId | ||
import org.mongodb.scala.Document | ||
import org.mongodb.scala.bson.conversions.Bson | ||
|
||
import scala.jdk.CollectionConverters._ | ||
import scala.language.implicitConversions | ||
trait DocumentIncludes { | ||
implicit def mapToBson(value: Map[_, _]): Bson = Converter.toDocument(value) | ||
|
||
implicit def documentFromJavaMap(map: java.util.Map[String, Any]): Document = | ||
documentFromScalaMap(map.asScala.toMap) | ||
|
||
implicit def documentFromMutableMap(map: collection.mutable.Map[String, Any]): Document = | ||
documentFromScalaMap(map.toMap) | ||
|
||
implicit def documentFromScalaMap(map: Map[String, Any]): Document = { | ||
var result = Document() | ||
map.keys.foreach { key => | ||
val v = map.getOrElse(key, null) | ||
result.+=(key -> BsonConverter.toBson(v)) | ||
} | ||
result | ||
} | ||
|
||
implicit def documentFromDocument(doc: org.bson.Document): Document = { | ||
var result = Document() | ||
doc | ||
.keySet() | ||
.asScala | ||
.foreach { key => | ||
val v = doc.get(key) | ||
result.+=(key -> BsonConverter.toBson(v)) | ||
} | ||
result | ||
} | ||
|
||
implicit def mapFromDocument(document: Document): Map[String, Any] = | ||
BsonConverter.asMap(document) | ||
|
||
implicit def mapListFromDocuments(documents: List[Document]): List[Map[String, Any]] = | ||
BsonConverter.asMapList(documents) | ||
|
||
// ObjectId | ||
implicit def stringToObjectId(str: String): ObjectId = new ObjectId(str) | ||
|
||
implicit def documentToObjectId(doc: Document): ObjectId = | ||
doc.getObjectId(DatabaseProvider.ObjectIdKey) | ||
} |
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,29 @@ | ||
package dev.mongocamp.driver | ||
|
||
import dev.mongocamp.driver.mongodb.operation.ObservableIncludes | ||
import org.bson.BsonValue | ||
import org.bson.types.ObjectId | ||
import org.mongodb.scala.gridfs.{GridFSFile, GridFSFindObservable} | ||
import org.mongodb.scala.{FindObservable, Observable, ObservableImplicits} | ||
|
||
import scala.language.implicitConversions | ||
trait MongoImplicits extends ObservableIncludes with ObservableImplicits { | ||
|
||
implicit def observableToResult[T](obs: Observable[T]): T = obs.result() | ||
|
||
implicit def findObservableToResultList[T](obs: FindObservable[T]): List[T] = | ||
obs.resultList() | ||
|
||
implicit def findObservableToResultOption[T](obs: FindObservable[T]): Option[T] = obs.resultOption() | ||
|
||
// gridfs | ||
|
||
implicit def gridFSFindObservableToFiles(observable: GridFSFindObservable): List[GridFSFile] = | ||
observable.resultList() | ||
|
||
implicit def gridFSFileToObjectId(file: GridFSFile): ObjectId = | ||
file.getObjectId | ||
|
||
implicit def gridFSFileToBSonIdValue(file: GridFSFile): BsonValue = file.getId | ||
|
||
} |
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
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: 1 addition & 2 deletions
3
src/test/scala/dev/mongocamp/driver/mongodb/operation/SearchSpec.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
1 change: 0 additions & 1 deletion
1
src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.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