This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
/
Term.scala
265 lines (211 loc) · 5.66 KB
/
Term.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2013 Steve Vickers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactivemongo.extensions.dsl.criteria
import scala.language.dynamics
import reactivemongo.bson._
/**
* A '''Term'' instance reifies the use of a MongoDB document field, both
* top-level or nested. Operators common to all ''T'' types are defined here
* with type-specific ones provided in the companion object below.
*
* @author svickers
*
*/
final case class Term[T] (`_term$name`: String)
extends Dynamic
{
/**
* Logical equality.
*/
def ===[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
`_term$name` -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Logical equality.
*/
def @==[U <: T : ValueBuilder] (rhs : U) : Expression = ===[U] (rhs);
/**
* Logical inequality: '''$ne'''.
*/
def <>[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
"$ne" -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Logical inequality: '''$ne'''.
*/
def =/=[U <: T : ValueBuilder] (rhs : U) : Expression = <>[U] (rhs);
/**
* Logical inequality: '''$ne'''.
*/
def !==[U <: T : ValueBuilder] (rhs : U) : Expression = <>[U] (rhs);
/**
* Less-than comparison: '''$lt'''.
*/
def <[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
"$lt" -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Less-than or equal comparison: '''$lte'''.
*/
def <=[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
"$lte" -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Greater-than comparison: '''$gt'''.
*/
def >[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
"$gt" -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Greater-than or equal comparison: '''$gte'''.
*/
def >=[U <: T : ValueBuilder] (rhs : U) : Expression = Expression (
`_term$name`,
"$gte" -> implicitly[ValueBuilder[U]].bson (rhs)
);
/**
* Field existence: '''$exists'''.
*/
def exists : Expression = Expression (
`_term$name`,
"$exists" -> BSONBoolean (true)
);
/**
* Field value equals one of the '''values''': '''$in'''.
*/
def in[U <: T : ValueBuilder] (values : Traversable[U])
(implicit B : ValueBuilder[U])
: Expression = Expression (
`_term$name`,
"$in" -> BSONArray (values map (B.bson))
);
/**
* Field value equals either '''head''' or one of the (optional)
* '''tail''' values: '''$in'''.
*/
def in[U <: T : ValueBuilder] (head : U, tail : U*)
(implicit B : ValueBuilder[U])
: Expression = Expression (
`_term$name`,
"$in" -> BSONArray (Seq (B.bson (head)) ++ tail.map (B.bson))
);
def selectDynamic[U] (field : String) : Term[U] = Term[U] (
`_term$name` + "." + field
);
}
object Term
{
/// Class Types
/**
* The '''CollectionTermOps''' `implicit` provides EDSL functionality to
* `Seq` [[reactivemongo.extensions.dsl.criteria.Term]]s only.
*/
implicit class CollectionTermOps[T] (val term : Term[Seq[T]])
extends AnyVal
{
def all (values : Traversable[T])
(implicit B : ValueBuilder[T])
: Expression = Expression (
term.`_term$name`,
"$all" -> BSONArray (values map (B.bson))
);
}
/**
* The '''StringTermOps''' `implicit` enriches
* [[reactivemongo.extensions.dsl.criteria.Term]]s for
* `String`-only operations.
*/
implicit class StringTermOps[T >: String](val term : Term[T])
extends AnyVal
{
def =~ (re : (String, RegexModifier)) : Expression = Expression (
term.`_term$name`,
"$regex" -> BSONRegex (re._1, re._2.value)
);
def =~ (re : String) : Expression = Expression (
term.`_term$name`,
"$regex" -> BSONRegex (re, "")
);
def !~ (re : (String, RegexModifier)) : Expression = Expression (
term.`_term$name`,
"$not" -> BSONDocument ("$regex" -> BSONRegex (re._1, re._2.value))
);
def !~ (re : String) : Expression = Expression (
term.`_term$name`,
"$not" -> BSONDocument ("$regex" -> BSONRegex (re, ""))
);
}
}
/**
* '''RegexModifier''' types provide the ability for developers to specify
* `$regex` modifiers using type-checked Scala types. For example, specifying
* a `$regex` which ignores case for the `surname` property can be written as:
*
* {{{
*
* criteria.surname =~ "smith" -> IgnoreCase;
*
* }}}
*
* Multiple modifiers can be combined using the or (`|`) operator,
* producing an implementation-defined ordering.
*
* @author svickers
*
*/
sealed trait RegexModifier
{
/**
* Use the or operator to combine two or more '''RegexModifier'''s into
* one logical value.
*/
def | (other : RegexModifier) : RegexModifier =
CombinedRegexModifier (this, other);
def value () : String;
}
case class CombinedRegexModifier (
lhs : RegexModifier,
rhs : RegexModifier
)
extends RegexModifier
{
override def value () : String = lhs.value + rhs.value;
}
case object DotMatchesEverything
extends RegexModifier
{
override val value : String = "s";
}
case object ExtendedExpressions
extends RegexModifier
{
override val value : String = "x";
}
case object IgnoreCase
extends RegexModifier
{
override val value : String = "i";
}
case object MultilineMatching
extends RegexModifier
{
override val value : String = "m";
}