@@ -137,6 +137,113 @@ public <R extends Expression> R cond(final R left, final R right) {
137
137
return newMqlExpression (ast ("$cond" , left , right ));
138
138
}
139
139
140
+ /** @see DocumentExpression */
141
+
142
+ private Function <CodecRegistry , AstPlaceholder > getFieldInternal (final String fieldName ) {
143
+ return (cr ) -> {
144
+ BsonValue value = fieldName .startsWith ("$" )
145
+ ? new BsonDocument ("$literal" , new BsonString (fieldName ))
146
+ : new BsonString (fieldName );
147
+ return astDoc ("$getField" , new BsonDocument ()
148
+ .append ("input" , this .fn .apply (cr ).bsonValue )
149
+ .append ("field" , value ));
150
+ };
151
+ }
152
+
153
+ @ Override
154
+ public Expression getField (final String fieldName ) {
155
+ return new MqlExpression <>(getFieldInternal (fieldName ));
156
+ }
157
+
158
+ @ Override
159
+ public BooleanExpression getBoolean (final String fieldName ) {
160
+ return new MqlExpression <>(getFieldInternal (fieldName ));
161
+ }
162
+
163
+ @ Override
164
+ public BooleanExpression getBoolean (final String fieldName , final BooleanExpression other ) {
165
+ return getBoolean (fieldName ).isBooleanOr (other );
166
+ }
167
+
168
+ @ Override
169
+ public NumberExpression getNumber (final String fieldName ) {
170
+ return new MqlExpression <>(getFieldInternal (fieldName ));
171
+ }
172
+
173
+ @ Override
174
+ public NumberExpression getNumber (final String fieldName , final NumberExpression other ) {
175
+ return getNumber (fieldName ).isNumberOr (other );
176
+ }
177
+
178
+ @ Override
179
+ public IntegerExpression getInteger (final String fieldName ) {
180
+ return new MqlExpression <>(getFieldInternal (fieldName ));
181
+ }
182
+
183
+ @ Override
184
+ public IntegerExpression getInteger (final String fieldName , final IntegerExpression other ) {
185
+ return getInteger (fieldName ).isIntegerOr (other );
186
+ }
187
+
188
+ @ Override
189
+ public StringExpression getString (final String fieldName ) {
190
+ return new MqlExpression <>(getFieldInternal (fieldName ));
191
+ }
192
+
193
+ @ Override
194
+ public StringExpression getString (final String fieldName , final StringExpression other ) {
195
+ return getString (fieldName ).isStringOr (other );
196
+ }
197
+
198
+ @ Override
199
+ public DateExpression getDate (final String fieldName ) {
200
+ return new MqlExpression <>(getFieldInternal (fieldName ));
201
+ }
202
+
203
+ @ Override
204
+ public DateExpression getDate (final String fieldName , final DateExpression other ) {
205
+ return getDate (fieldName ).isDateOr (other );
206
+ }
207
+
208
+ @ Override
209
+ public DocumentExpression getDocument (final String fieldName ) {
210
+ return new MqlExpression <>(getFieldInternal (fieldName ));
211
+ }
212
+
213
+ @ Override
214
+ public DocumentExpression getDocument (final String fieldName , final DocumentExpression other ) {
215
+ return getDocument (fieldName ).isDocumentOr (other );
216
+ }
217
+
218
+ @ Override
219
+ public <R extends Expression > ArrayExpression <R > getArray (final String fieldName ) {
220
+ return new MqlExpression <>(getFieldInternal (fieldName ));
221
+ }
222
+
223
+ @ Override
224
+ public <R extends Expression > ArrayExpression <R > getArray (final String fieldName , final ArrayExpression <? extends R > other ) {
225
+ return getArray (fieldName ).isArrayOr (other );
226
+ }
227
+
228
+ @ Override
229
+ public DocumentExpression merge (final DocumentExpression other ) {
230
+ return new MqlExpression <>(ast ("$mergeObjects" , other ));
231
+ }
232
+
233
+ @ Override
234
+ public DocumentExpression setField (final String fieldName , final Expression exp ) {
235
+ return newMqlExpression ((cr ) -> astDoc ("$setField" , new BsonDocument ()
236
+ .append ("field" , new BsonString (fieldName ))
237
+ .append ("input" , this .toBsonValue (cr ))
238
+ .append ("value" , extractBsonValue (cr , exp ))));
239
+ }
240
+
241
+ @ Override
242
+ public DocumentExpression unsetField (final String fieldName ) {
243
+ return newMqlExpression ((cr ) -> astDoc ("$unsetField" , new BsonDocument ()
244
+ .append ("field" , new BsonString (fieldName ))
245
+ .append ("input" , this .toBsonValue (cr ))));
246
+ }
140
247
141
248
/** @see Expression */
142
249
@@ -175,55 +282,71 @@ public BooleanExpression isBoolean() {
175
282
}
176
283
177
284
@ Override
178
- public BooleanExpression isBooleanOr (final BooleanExpression or ) {
179
- return this .isBoolean ().cond (this , or );
285
+ public BooleanExpression isBooleanOr (final BooleanExpression other ) {
286
+ return this .isBoolean ().cond (this , other );
180
287
}
181
288
182
289
public BooleanExpression isNumber () {
183
290
return new MqlExpression <>(astWrapped ("$isNumber" ));
184
291
}
185
292
186
293
@ Override
187
- public NumberExpression isNumberOr (final NumberExpression or ) {
188
- return this .isNumber ().cond (this , or );
294
+ public NumberExpression isNumberOr (final NumberExpression other ) {
295
+ return this .isNumber ().cond (this , other );
296
+ }
297
+
298
+ public BooleanExpression isInteger () {
299
+ return this .isNumber ().cond (this .eq (this .round ()), of (false ));
300
+ }
301
+
302
+ @ Override
303
+ public IntegerExpression isIntegerOr (final IntegerExpression other ) {
304
+ return this .isInteger ().cond (this , other );
189
305
}
190
306
191
307
public BooleanExpression isString () {
192
308
return new MqlExpression <>(ast ("$type" )).eq (of ("string" ));
193
309
}
194
310
195
311
@ Override
196
- public StringExpression isStringOr (final StringExpression or ) {
197
- return this .isString ().cond (this , or );
312
+ public StringExpression isStringOr (final StringExpression other ) {
313
+ return this .isString ().cond (this , other );
198
314
}
199
315
200
316
public BooleanExpression isDate () {
201
317
return ofStringArray ("date" ).contains (new MqlExpression <>(ast ("$type" )));
202
318
}
203
319
204
320
@ Override
205
- public DateExpression isDateOr (final DateExpression or ) {
206
- return this .isDate ().cond (this , or );
321
+ public DateExpression isDateOr (final DateExpression other ) {
322
+ return this .isDate ().cond (this , other );
207
323
}
208
324
209
325
public BooleanExpression isArray () {
210
326
return new MqlExpression <>(astWrapped ("$isArray" ));
211
327
}
212
328
213
- @ SuppressWarnings ("unchecked" ) // TODO
329
+ /**
330
+ * checks if array (but cannot check type)
331
+ * user asserts array is of type R
332
+ *
333
+ * @param other
334
+ * @return
335
+ * @param <R>
336
+ */
337
+ @ SuppressWarnings ("unchecked" )
214
338
@ Override
215
- public ArrayExpression <Expression > isArrayOr (final ArrayExpression <? extends Expression > or ) {
216
- // TODO it seems that ArrEx<T> does not make sense here
217
- return (ArrayExpression <Expression >) this .isArray ().cond (this .assertImplementsAllExpressions (), or );
339
+ public <R extends Expression > ArrayExpression <R > isArrayOr (final ArrayExpression <? extends R > other ) {
340
+ return (ArrayExpression <R >) this .isArray ().cond (this .assertImplementsAllExpressions (), other );
218
341
}
219
342
220
343
public BooleanExpression isDocument () {
221
344
return new MqlExpression <>(ast ("$type" )).eq (of ("object" ));
222
345
}
223
346
224
347
@ Override
225
- public <R extends DocumentExpression > R isDocumentOr (final R or ) {
226
- return this .isDocument ().cond (this .assertImplementsAllExpressions (), or );
348
+ public <R extends DocumentExpression > R isDocumentOr (final R other ) {
349
+ return this .isDocument ().cond (this .assertImplementsAllExpressions (), other );
227
350
}
228
351
229
352
@ Override
0 commit comments