From 753a63381dea71a92fa696aaa28f849580069af6 Mon Sep 17 00:00:00 2001 From: QuadStingray Date: Tue, 28 Mar 2023 21:12:03 +0200 Subject: [PATCH] refactor: DocumentIncludes and MongoImplicits to own files --- .../mongocamp/driver/DocumentIncludes.scala | 53 +++++++++++++ .../dev/mongocamp/driver/MongoImplicits.scala | 29 +++++++ .../mongocamp/driver/mongodb/package.scala | 79 +------------------ .../driver/mongodb/dao/StudentDAOSpec.scala | 4 +- .../driver/mongodb/operation/SearchSpec.scala | 3 +- .../pagination/PaginationFilterSpec.scala | 1 - 6 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 src/main/scala/dev/mongocamp/driver/DocumentIncludes.scala create mode 100644 src/main/scala/dev/mongocamp/driver/MongoImplicits.scala diff --git a/src/main/scala/dev/mongocamp/driver/DocumentIncludes.scala b/src/main/scala/dev/mongocamp/driver/DocumentIncludes.scala new file mode 100644 index 00000000..15520cde --- /dev/null +++ b/src/main/scala/dev/mongocamp/driver/DocumentIncludes.scala @@ -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) +} diff --git a/src/main/scala/dev/mongocamp/driver/MongoImplicits.scala b/src/main/scala/dev/mongocamp/driver/MongoImplicits.scala new file mode 100644 index 00000000..c5b90c30 --- /dev/null +++ b/src/main/scala/dev/mongocamp/driver/MongoImplicits.scala @@ -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 + +} diff --git a/src/main/scala/dev/mongocamp/driver/mongodb/package.scala b/src/main/scala/dev/mongocamp/driver/mongodb/package.scala index 0fea768f..2a577113 100644 --- a/src/main/scala/dev/mongocamp/driver/mongodb/package.scala +++ b/src/main/scala/dev/mongocamp/driver/mongodb/package.scala @@ -1,21 +1,12 @@ package dev.mongocamp.driver -import java.util.Date - -import dev.mongocamp.driver.mongodb.Converter import dev.mongocamp.driver.mongodb.bson.BsonConverter import dev.mongocamp.driver.mongodb.bson.convert.JsonDateTimeConverter -import dev.mongocamp.driver.mongodb.database.{ DatabaseProvider, MongoConfig } -import dev.mongocamp.driver.mongodb.gridfs.GridFSStreamObserver import dev.mongocamp.driver.mongodb.operation.ObservableIncludes -import org.bson.BsonValue -import org.bson.json.{ JsonMode, JsonWriterSettings } -import org.bson.types.ObjectId -import org.mongodb.scala.bson.conversions.Bson -import org.mongodb.scala.gridfs.{ GridFSFile, GridFSFindObservable } -import org.mongodb.scala.{ Document, FindObservable, Observable, ObservableImplicits } +import org.bson.json.{JsonMode, JsonWriterSettings} +import org.mongodb.scala.Document -import scala.jdk.CollectionConverters._ +import java.util.Date import scala.language.implicitConversions package object mongodb extends ObservableIncludes with DocumentIncludes { @@ -73,67 +64,3 @@ package object mongodb extends ObservableIncludes with DocumentIncludes { def updateValue(key: String, value: Any): Any = BsonConverter.updateDocumentValue(document, key, value) } } - -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 - -} - -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) -} diff --git a/src/test/scala/dev/mongocamp/driver/mongodb/dao/StudentDAOSpec.scala b/src/test/scala/dev/mongocamp/driver/mongodb/dao/StudentDAOSpec.scala index c869ac3f..04f162a3 100644 --- a/src/test/scala/dev/mongocamp/driver/mongodb/dao/StudentDAOSpec.scala +++ b/src/test/scala/dev/mongocamp/driver/mongodb/dao/StudentDAOSpec.scala @@ -2,14 +2,12 @@ package dev.mongocamp.driver.mongodb.dao import better.files.{File, Resource} import dev.mongocamp.driver.MongoImplicits -import dev.mongocamp.driver.mongodb._ import dev.mongocamp.driver.mongodb.model.Student import dev.mongocamp.driver.mongodb.server.LocalServer import dev.mongocamp.driver.mongodb.test.UniversityDatabase import dev.mongocamp.driver.mongodb.test.UniversityDatabase.{GradeDAO, StudentDAO} import org.specs2.mutable.Specification -import org.specs2.specification.core.Fragments -import org.specs2.specification.{AfterAll, AfterSpec, BeforeAll} +import org.specs2.specification.{AfterAll, BeforeAll} class StudentDAOSpec extends Specification with BeforeAll with AfterAll with MongoImplicits { sequential diff --git a/src/test/scala/dev/mongocamp/driver/mongodb/operation/SearchSpec.scala b/src/test/scala/dev/mongocamp/driver/mongodb/operation/SearchSpec.scala index 83c1e657..eb83a710 100644 --- a/src/test/scala/dev/mongocamp/driver/mongodb/operation/SearchSpec.scala +++ b/src/test/scala/dev/mongocamp/driver/mongodb/operation/SearchSpec.scala @@ -1,11 +1,10 @@ package dev.mongocamp.driver.mongodb.operation - import dev.mongocamp.driver.MongoImplicits import dev.mongocamp.driver.mongodb.Sort._ -import dev.mongocamp.driver.mongodb.test.TestDatabase._ import dev.mongocamp.driver.mongodb._ import dev.mongocamp.driver.mongodb.dao.PersonSpecification import dev.mongocamp.driver.mongodb.model.Person +import dev.mongocamp.driver.mongodb.test.TestDatabase._ class SearchSpec extends PersonSpecification with MongoImplicits { diff --git a/src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala b/src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala index fe91f97f..e00caa6e 100644 --- a/src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala +++ b/src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala @@ -1,5 +1,4 @@ package dev.mongocamp.driver.mongodb.pagination - import dev.mongocamp.driver.MongoImplicits import dev.mongocamp.driver.mongodb.Sort._ import dev.mongocamp.driver.mongodb._