Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
gguan committed Nov 22, 2023
1 parent 3bfa2a1 commit b15c3f0
Show file tree
Hide file tree
Showing 59 changed files with 757 additions and 530 deletions.
38 changes: 18 additions & 20 deletions bson/src/main/scala/dao/BsonDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@

package reactivemongo.extensions.dao

import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import play.api.libs.json.{ JsObject, Json }

import scala.util.Random
import scala.concurrent.duration._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration._
import reactivemongo.api.bson._
import scala.util.Random

import akka.stream.scaladsl.Sink
import akka.stream.Materializer
import play.api.libs.json.{ JsObject, Json }
import reactivemongo.akkastream.cursorProducer
import reactivemongo.api.{ Collation, Cursor, DB, ReadConcern, WriteConcern }
import reactivemongo.api.indexes.Index
import reactivemongo.api.bson._
import reactivemongo.api.bson.collection.BSONCollection
import reactivemongo.api.commands.WriteResult
import reactivemongo.extensions.dsl.BsonDsl._
import reactivemongo.api.indexes.Index
import reactivemongo.api.Cursor.FailOnError
import reactivemongo.api.bson.collection.BSONCollection
import reactivemongo.akkastream.cursorProducer
import reactivemongo.extensions.dsl.BsonDsl._

/** A DAO implementation operates on BSONCollection using BSONDocument.
/**
* A DAO implementation operates on BSONCollection using BSONDocument.
*
* To create a DAO for a concrete model extend this class.
*
Expand Down Expand Up @@ -120,8 +121,7 @@ abstract class BsonDao[Model, ID](db: => Future[DB], collectionName: String)(
)(implicit ec: ExecutionContext): Future[List[Model]] = {
val from = page * pageSize
collection.flatMap(
_
.find(selector)
_.find(selector)
.sort(sort)
.skip(from)
.cursor[Model]()
Expand All @@ -146,8 +146,7 @@ abstract class BsonDao[Model, ID](db: => Future[DB], collectionName: String)(
): Future[List[Model]] = {
val from = page * pageSize
collection.flatMap(
_
.find(selector)
_.find(selector)
.collation(collation)
.sort(sort)
.skip(from)
Expand Down Expand Up @@ -226,10 +225,9 @@ abstract class BsonDao[Model, ID](db: => Future[DB], collectionName: String)(
_id <- Future(doc.getAsTry[ID]("_id").get)
res <- {
val mappedModel = lifeCycle.prePersist(model)
collection.flatMap(_.update.one($id(_id), mappedModel, upsert = true).map {
result =>
lifeCycle.postPersist(mappedModel)
result
collection.flatMap(_.update.one($id(_id), mappedModel, upsert = true).map { result =>
lifeCycle.postPersist(mappedModel)
result
})
}
} yield res
Expand Down
6 changes: 3 additions & 3 deletions bson/src/main/scala/dao/BsonDaoBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package reactivemongo.extensions.dao

import reactivemongo.api.DB
import reactivemongo.api.bson._

import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import reactivemongo.api.bson._
import reactivemongo.api.DB

class BsonDaoBuilder[Model, ID](db: => Future[DB]) {
def apply(collectionName: String)(
implicit modelReader: BSONDocumentReader[Model],
Expand Down
41 changes: 21 additions & 20 deletions bson/src/main/scala/dsl/BsonDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package reactivemongo.extensions.dsl

import scala.reflect.runtime.universe.TypeTag

import reactivemongo.api.bson._
import reactivemongo.extensions.BsonTypes

import scala.reflect.runtime.universe.TypeTag

trait BsonDsl {

implicit def bsonDocumentToPretty(document: BSONDocument): String = {
BSONDocument.pretty(document)
}

//**********************************************************************************************//
// **********************************************************************************************//
// Helpers
def $empty: BSONDocument = BSONDocument.empty

Expand All @@ -42,9 +42,9 @@ trait BsonDsl {
def $id[T](id: T)(implicit writer: BSONWriter[T]): BSONDocument = BSONDocument("_id" -> id)

// End of Helpers
//**********************************************************************************************//
// **********************************************************************************************//

//**********************************************************************************************//
// **********************************************************************************************//
// Top Level Logical Operators
def $or(expressions: BSONDocument*): BSONDocument = {
BSONDocument("$or" -> expressions)
Expand All @@ -58,9 +58,9 @@ trait BsonDsl {
BSONDocument("$nor" -> expressions)
}
// End of Top Level Logical Operators
//**********************************************************************************************//
// **********************************************************************************************//

//**********************************************************************************************//
// **********************************************************************************************//
// Top Level Evaluation Operators
def $text(search: String): BSONDocument = {
BSONDocument("$text" -> BSONDocument("$search" -> search))
Expand All @@ -74,9 +74,9 @@ trait BsonDsl {
BSONDocument("$where" -> expression)
}
// End of Top Level Evaluation Operators
//**********************************************************************************************//
// **********************************************************************************************//

//**********************************************************************************************//
// **********************************************************************************************//
// Top Level Field Update Operators
def $inc(item: ElementProducer, items: ElementProducer*): BSONDocument = {
BSONDocument("$inc" -> BSONDocument((Seq(item) ++ items): _*))
Expand Down Expand Up @@ -133,9 +133,9 @@ trait BsonDsl {
BSONDocument("$currentDate" -> BSONDocument(items.map(item => item._1 -> item._2.produce)))
}
// End of Top Level Field Update Operators
//**********************************************************************************************//
// **********************************************************************************************//

//**********************************************************************************************//
// **********************************************************************************************//
// Top Level Array Update Operators
def $addToSet(item: ElementProducer, items: ElementProducer*): BSONDocument = {
BSONDocument("$addToSet" -> BSONDocument((Seq(item) ++ items): _*))
Expand All @@ -160,9 +160,10 @@ trait BsonDsl {
BSONDocument("$pull" -> BSONDocument(item))
}
// End ofTop Level Array Update Operators
//**********************************************************************************************//
// **********************************************************************************************//

/** Represents the inital state of the expression which has only the name of the field.
/**
* Represents the inital state of the expression which has only the name of the field.
* It does not know the value of the expression.
*/
trait ElementBuilder {
Expand All @@ -178,24 +179,24 @@ trait BsonDsl {
/*
* This type of expressions cannot be cascaded. Examples:
*
* {{{
* {{{
* "price" $eq 10
* "price" $ne 1000
* "size" $in ("S", "M", "L")
* "size" $nin ("S", "XXL")
* }}}
*
*/
*/
case class SimpleExpression[V <: BSONValue](field: String, value: V) extends Expression[V]

/** Expressions of this type can be cascaded. Examples:
/**
* Expressions of this type can be cascaded. Examples:
*
* {{{
* {{{
* "age" $gt 50 $lt 60
* "age" $gte 50 $lte 60
* }}}
*
*/
*/
case class CompositeExpression(field: String, value: BSONDocument)
extends Expression[BSONDocument]
with ComparisonOperators {
Expand All @@ -221,7 +222,7 @@ trait BsonDsl {
CompositeExpression(field, append(BSONDocument("$gte" -> value)))
}

/** Matches any of the values that exist in an array specified in the query.*/
/** Matches any of the values that exist in an array specified in the query. */
def $in[T](values: T*)(implicit writer: BSONWriter[T]): SimpleExpression[BSONDocument] = {
SimpleExpression(field, BSONDocument("$in" -> values))
}
Expand Down
75 changes: 41 additions & 34 deletions bson/src/main/scala/dsl/criteria/Expression.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package reactivemongo.extensions.dsl.criteria

import scala.language.dynamics
import scala.language.implicitConversions
import reactivemongo.api.bson._

import scala.util.Try

/** The '''Expression''' type defines a recursive propositional abstract
import reactivemongo.api.bson._

/**
* The '''Expression''' type defines a recursive propositional abstract
* syntax tree central to the MongoDB embedded domain-specific language (EDSL).
* It is the main abstraction used to provide the EDSL and results in being
* able to write:
Expand All @@ -30,45 +31,45 @@ import scala.util.Try
* import Untyped._
*
* val edslQuery = criteria.first < 10 && (
* criteria.second >= 20.0 || criteria.second.in (0.0, 1.0)
* );
* criteria.second >= 20.0 || criteria.second.in (0.0, 1.0)
* );
* }}}
*
* And have that equivalent to this filter:
*
* {{{
* val bsonQuery = BSONDocument (
* "$and" ->
* BSONArray (
* BSONDocument (
* "first" -> BSONDocument ("$lt" -> BSONInteger (10))
* ),
* BSONDocument (
* "$or" ->
* BSONArray (
* BSONDocument (
* "second" -> BSONDocument ("$gte" -> BSONDouble (20.0))
* ),
* BSONDocument (
* "second" ->
* BSONDocument (
* "$in" -> BSONArray (BSONDouble (0.0), BSONDouble (1.0))
* )
* )
* )
* )
* )
* );
* "$and" ->
* BSONArray (
* BSONDocument (
* "first" -> BSONDocument ("$lt" -> BSONInteger (10))
* ),
* BSONDocument (
* "$or" ->
* BSONArray (
* BSONDocument (
* "second" -> BSONDocument ("$gte" -> BSONDouble (20.0))
* ),
* BSONDocument (
* "second" ->
* BSONDocument (
* "$in" -> BSONArray (BSONDouble (0.0), BSONDouble (1.0))
* )
* )
* )
* )
* )
* );
* }}}
*
* @author svickers
*
*/
case class Expression(name: Option[String], element: BSONElement) {
/// Class Imports
import Expression._

/** The logical negation operator attempts to invert this '''Expression'''
/**
* The logical negation operator attempts to invert this '''Expression'''
* by using complimentary operators if possible, falling back to the
* general-case wrapping in a `$not` operator.
*/
Expand Down Expand Up @@ -102,19 +103,23 @@ case class Expression(name: Option[String], element: BSONElement) {
Expression(Some("$not"), el)
}

/** Conjunction: ''AND''.
/**
* Conjunction: ''AND''.
*/
def &&(rhs: Expression): Expression = combine("$and", rhs)

/** Negation of conjunction: ''NOR''.
/**
* Negation of conjunction: ''NOR''.
*/
def !&&(rhs: Expression): Expression = combine("$nor", rhs)

/** Disjunction: ''OR''.
/**
* Disjunction: ''OR''.
*/
def ||(rhs: Expression): Expression = combine("$or", rhs)

/** The isEmpty method reports as to whether or not this '''Expression'''
/**
* The isEmpty method reports as to whether or not this '''Expression'''
* has neither a `name` nor an assigned value.
*/
def isEmpty: Boolean = name.isEmpty && element.name.isEmpty
Expand All @@ -137,12 +142,14 @@ case class Expression(name: Option[String], element: BSONElement) {

object Expression {

/** The empty property is provided so that ''monoid'' definitions for
/**
* The empty property is provided so that ''monoid'' definitions for
* '''Expression''' can be easily provided.
*/
val empty = new Expression(None, "" -> BSONDocument.empty)

/** The apply method provides functional-style creation syntax for
/**
* The apply method provides functional-style creation syntax for
* [[reactivemongo.extensions.dsl.criteria.Expression]] instances.
*/
def apply(name: String, element: BSONElement): Expression = new Expression(Some(name), element)
Expand Down
Loading

0 comments on commit b15c3f0

Please sign in to comment.