@@ -199,11 +199,11 @@ List<Expression> getExpressions() {
199
199
}
200
200
201
201
/**
202
- * Returns this object as as list of {@link Expression} to be passed to
202
+ * Returns this object as list of {@link Expression} to be passed to
203
203
* Spring Data Specification builder {@link ExpressionsPredicateBuilder}
204
204
*/
205
205
@ SuppressWarnings ({"unchecked" })
206
- private List <Expression > getExpressions (Map <String , Object > map ) {
206
+ private static List <Expression > getExpressions (Map <String , Object > map ) {
207
207
208
208
List <Expression > expressions = new ArrayList <>();
209
209
@@ -258,12 +258,13 @@ private static Map<String, Object> map(String key, Object value) {
258
258
}
259
259
260
260
/**
261
- * Extracts all the field names (keys) from the current {@code Expressions} object,
262
- * including nested fields within `$and` and `$or` compound operators.
261
+ * Extracts all field names and their corresponding values from the current
262
+ * {@code Expressions} object, including nested fields within `$and` and `$or`
263
+ * compound operators.
263
264
* <p>
264
265
* This method traverses the structure of the {@code Expressions} object recursively.
265
- * If a compound operator (`$and` or `$or`) is encountered, it extracts fields from
266
- * all nested expressions.
266
+ * If a compound operator (`$and` or `$or`) is encountered, it extracts fields and
267
+ * values from all nested expressions.
267
268
* </p>
268
269
* <p>
269
270
* Example:
@@ -277,28 +278,34 @@ private static Map<String, Object> map(String key, Object value) {
277
278
* ]
278
279
* }
279
280
* </pre>
280
- * The resulting list of fields will be:
281
+ * The resulting map of fields will be:
281
282
* <pre>
282
- * ["firstName", "lastName", "age"]
283
+ * {
284
+ * "firstName": "John",
285
+ * "lastName": "Doe",
286
+ * "age": 30
287
+ * }
283
288
* </pre>
284
289
*
285
- * @return a list of field names present in the current {@code Expressions} object , including nested fields.
290
+ * @return a map containing field names as keys and their corresponding values , including nested fields.
286
291
*/
287
- @ SuppressWarnings ({ "unchecked" })
288
- public static List < String > extractFields ( Map < String , Object > expressions ) {
289
- List < String > list = new ArrayList <>();
292
+ public Map < String , Object > extractFields () {
293
+ return extractFields ( getExpressions ( this ));
294
+ }
290
295
291
- for (Map .Entry <String , Object > entry : expressions .entrySet ()) {
292
- String key = entry .getKey ();
293
- if (key .equals ($and .name ()) || key .equals ($or .name ())) {
294
- List <Object > values = (List <Object >) entry .getValue ();
295
- for (Object value : values ) {
296
- list .addAll (extractFields ((Map <String , Object >) value ));
297
- }
298
- } else {
299
- list .add (key );
296
+ private static Map <String , Object > extractFields (List <Expression > expressionList ) {
297
+ var map = new HashMap <String , Object >();
298
+ for (Expression expression : expressionList ) {
299
+ if (expression instanceof SingularExpression singularExpression ) {
300
+ map .put (singularExpression .field , singularExpression .value );
301
+ } else if (expression instanceof ListExpression listExpression ) {
302
+ map .put (listExpression .field , listExpression .values );
303
+ } else if (expression instanceof AndExpression andExpression ) {
304
+ map .putAll (extractFields (andExpression .expressions ));
305
+ } else if (expression instanceof OrExpression andExpression ) {
306
+ map .putAll (extractFields (andExpression .expressions ));
300
307
}
301
308
}
302
- return list ;
309
+ return map ;
303
310
}
304
311
}
0 commit comments