Skip to content

Commit 3050874

Browse files
author
mhewedy
committed
Merge remote-tracking branch 'origin/master'
2 parents ba0b65d + 1a7da35 commit 3050874

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ private static Map<String, Object> map(String key, Object value) {
257257
return m;
258258
}
259259

260+
/**
261+
* Extracts all the field names (keys) from the current {@code Expressions} object,
262+
* including nested fields within `$and` and `$or` compound operators.
263+
* <p>
264+
* 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.
267+
* </p>
268+
* <p>
269+
* Example:
270+
* Given the following {@code Expressions} structure:
271+
* <pre>
272+
* {
273+
* "firstName": "John",
274+
* "$or": [
275+
* {"lastName": "Doe"},
276+
* {"age": {"$gt": 30}}
277+
* ]
278+
* }
279+
* </pre>
280+
* The resulting list of fields will be:
281+
* <pre>
282+
* ["firstName", "lastName", "age"]
283+
* </pre>
284+
*
285+
* @return a list of field names present in the current {@code Expressions} object, including nested fields.
286+
*/
260287
@SuppressWarnings({"unchecked"})
261288
public static List<String> extractFields(Map<String, Object> expressions) {
262289
List<String> list = new ArrayList<>();

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

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import org.junit.jupiter.api.Test;
66

7+
import java.util.Arrays;
78
import java.util.List;
89

910
import static com.github.mhewedy.expressions.Expression.*;
1011
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
1114

1215
class ExpressionsTest {
1316

17+
private final ObjectMapper objectMapper = new ObjectMapper();
18+
1419
@Test
1520
public void testObjectWithOrExpression() throws Exception {
1621

17-
ObjectMapper objectMapper = new ObjectMapper();
18-
1922
Expressions conditions =
20-
objectMapper.readValue(
21-
"{ \"status\": \"A\", \"$or\": [{ \"qty\": { \"$lt\": 30 } }, { \"item\": { \"$in\": [\"A\", \"D\"] } }] }",
23+
objectMapper.readValue("""
24+
{
25+
"status": "A",
26+
"$or": [
27+
{
28+
"qty": { "$lt": 30 }
29+
},
30+
{
31+
"item": {"$in": ["A", "D"] }
32+
}
33+
]
34+
}
35+
""",
2236
Expressions.class);
2337

2438
final List<Expression> expression = conditions.getExpressions();
@@ -68,4 +82,72 @@ public void testObjectWithOrExpression() throws Exception {
6882
}
6983
}
7084

85+
@Test
86+
void testExtractFieldsWithCompoundOperatorsJSON() throws Exception {
87+
String json = """
88+
{
89+
"firstName": "John",
90+
"$or": [
91+
{
92+
"lastName": "Doe"
93+
},
94+
{
95+
"age": { "$gt": 30 }
96+
}
97+
]
98+
}
99+
""";
100+
Expressions expressions = objectMapper.readValue(json, Expressions.class);
101+
List<String> fields = Expressions.extractFields(expressions);
102+
103+
assertEquals(Arrays.asList("firstName", "lastName", "age"), fields);
104+
}
105+
106+
@Test
107+
void testExtractFieldsWithEmptyJSON() throws Exception {
108+
String json = "{}";
109+
Expressions expressions = objectMapper.readValue(json, Expressions.class);
110+
List<String> fields = Expressions.extractFields(expressions);
111+
112+
assertTrue(fields.isEmpty());
113+
}
114+
115+
@Test
116+
void testExtractFieldsWithNestedAndOperatorJSON() throws Exception {
117+
String json = """
118+
{
119+
"$and": [
120+
{ "country": "USA" },
121+
{ "state": "California" }
122+
]
123+
}
124+
""";
125+
Expressions expressions = objectMapper.readValue(json, Expressions.class);
126+
List<String> fields = Expressions.extractFields(expressions);
127+
128+
assertEquals(Arrays.asList("country", "state"), fields);
129+
}
130+
131+
@Test
132+
void testExtractFieldsWithMultipleNestedOperatorsJSON() throws Exception {
133+
String json = """
134+
{
135+
"$and": [
136+
{
137+
"$or": [
138+
{ "city": "New York" },
139+
{ "zipcode": "10001" }
140+
]
141+
},
142+
{
143+
"city.country": "USA"
144+
}
145+
]
146+
}
147+
""";
148+
Expressions expressions = objectMapper.readValue(json, Expressions.class);
149+
List<String> fields = Expressions.extractFields(expressions);
150+
151+
assertEquals(Arrays.asList("city", "zipcode", "city.country"), fields);
152+
}
71153
}

0 commit comments

Comments
 (0)