Skip to content

Commit 4a51c3a

Browse files
committed
enhance the implementation and interface of extractFields
1 parent a525d2e commit 4a51c3a

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ For spring-boot 3.x:
6363
<dependency>
6464
<groupId>com.github.mhewedy</groupId>
6565
<artifactId>spring-data-jpa-mongodb-expressions</artifactId>
66-
<version>0.1.8</version>
66+
<version>0.1.9</version>
6767
</dependency>
6868

6969
```

docs/include.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:author: Mohammad Hewedy, The Spring Data JPA MongoDB Expressions Team
2-
:revnumber: 0.1.8
2+
:revnumber: 0.1.9
33
:jsondir: ../src/test/resources
44
:sectlinks: true
55
:source-highlighter: highlight.js

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.github.mhewedy</groupId>
1414
<artifactId>spring-data-jpa-mongodb-expressions</artifactId>
15-
<version>0.1.8</version>
15+
<version>0.1.9</version>
1616
<name>spring-data-jpa-mongodb-expressions</name>
1717
<description>Spring Data JPA Mongodb Expressions</description>
1818

src/main/java/com/github/mhewedy/expressions/Expressions.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ List<Expression> getExpressions() {
199199
}
200200

201201
/**
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
203203
* Spring Data Specification builder {@link ExpressionsPredicateBuilder}
204204
*/
205205
@SuppressWarnings({"unchecked"})
206-
private List<Expression> getExpressions(Map<String, Object> map) {
206+
private static List<Expression> getExpressions(Map<String, Object> map) {
207207

208208
List<Expression> expressions = new ArrayList<>();
209209

@@ -258,12 +258,13 @@ private static Map<String, Object> map(String key, Object value) {
258258
}
259259

260260
/**
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.
263264
* <p>
264265
* 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.
267268
* </p>
268269
* <p>
269270
* Example:
@@ -277,28 +278,34 @@ private static Map<String, Object> map(String key, Object value) {
277278
* ]
278279
* }
279280
* </pre>
280-
* The resulting list of fields will be:
281+
* The resulting map of fields will be:
281282
* <pre>
282-
* ["firstName", "lastName", "age"]
283+
* {
284+
* "firstName": "John",
285+
* "lastName": "Doe",
286+
* "age": 30
287+
* }
283288
* </pre>
284289
*
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.
286291
*/
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+
}
290295

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));
300307
}
301308
}
302-
return list;
309+
return map;
303310
}
304311
}

src/test/java/com/github/mhewedy/expressions/ExpressionsTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.util.Arrays;
8+
import java.util.Collection;
89
import java.util.List;
10+
import java.util.Set;
911

1012
import static com.github.mhewedy.expressions.Expression.*;
1113
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatList;
1215
import static org.junit.jupiter.api.Assertions.assertEquals;
1316
import static org.junit.jupiter.api.Assertions.assertTrue;
1417

@@ -98,16 +101,16 @@ void testExtractFieldsWithCompoundOperatorsJSON() throws Exception {
98101
}
99102
""";
100103
Expressions expressions = objectMapper.readValue(json, Expressions.class);
101-
List<String> fields = Expressions.extractFields(expressions);
104+
Set<String> fields = expressions.extractFields().keySet();
102105

103-
assertEquals(Arrays.asList("firstName", "lastName", "age"), fields);
106+
assertEquals(Set.of("firstName", "lastName", "age"), fields);
104107
}
105108

106109
@Test
107110
void testExtractFieldsWithEmptyJSON() throws Exception {
108111
String json = "{}";
109112
Expressions expressions = objectMapper.readValue(json, Expressions.class);
110-
List<String> fields = Expressions.extractFields(expressions);
113+
Set<String> fields = expressions.extractFields().keySet();
111114

112115
assertTrue(fields.isEmpty());
113116
}
@@ -123,9 +126,9 @@ void testExtractFieldsWithNestedAndOperatorJSON() throws Exception {
123126
}
124127
""";
125128
Expressions expressions = objectMapper.readValue(json, Expressions.class);
126-
List<String> fields = Expressions.extractFields(expressions);
129+
Set<String> fields = expressions.extractFields().keySet();
127130

128-
assertEquals(Arrays.asList("country", "state"), fields);
131+
assertEquals(Set.of("country", "state"), fields);
129132
}
130133

131134
@Test
@@ -146,8 +149,10 @@ void testExtractFieldsWithMultipleNestedOperatorsJSON() throws Exception {
146149
}
147150
""";
148151
Expressions expressions = objectMapper.readValue(json, Expressions.class);
149-
List<String> fields = Expressions.extractFields(expressions);
152+
Set<String> fields = expressions.extractFields().keySet();
153+
List<Object> values = expressions.extractFields().values().stream().toList();
150154

151-
assertEquals(Arrays.asList("city", "zipcode", "city.country"), fields);
155+
assertEquals(Set.of("city", "zipcode", "city.country"), fields);
156+
assertThatList(values).containsExactlyInAnyOrder("New York", "10001", "USA");
152157
}
153158
}

0 commit comments

Comments
 (0)