Skip to content

Commit 88e7beb

Browse files
committed
Support UUID
1 parent bf645dd commit 88e7beb

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.ArrayList;
1212
import java.util.Arrays;
1313
import java.util.List;
14+
import java.util.UUID;
1415
import java.util.stream.Collectors;
1516

1617
import static com.github.mhewedy.expressions.Expression.*;
@@ -297,7 +298,6 @@ private static Object convertValueToAttributeType(Object value, Class javaType)
297298
if (javaType.equals(ZonedDateTime.class)) {
298299
return ZonedDateTime.parse((CharSequence) value);
299300
}
300-
301301
if (javaType.isEnum()) {
302302
if (Number.class.isAssignableFrom(value.getClass())) {
303303
return javaType.getEnumConstants()[((Number) value).intValue()];
@@ -306,6 +306,9 @@ private static Object convertValueToAttributeType(Object value, Class javaType)
306306
}
307307
throw new IllegalArgumentException("enum value should be number or string");
308308
}
309+
if (javaType.equals(UUID.class)) {
310+
return UUID.fromString((String) value);
311+
}
309312

310313
// strings and numeric types don't need conversion
311314
return value;

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.LocalDate;
2828
import java.util.Arrays;
2929
import java.util.List;
30+
import java.util.UUID;
3031

3132
import static com.github.mhewedy.expressions.model.Status.ACTIVE;
3233
import static com.github.mhewedy.expressions.model.Status.NOT_ACTIVE;
@@ -58,7 +59,8 @@ public void setup() {
5859
(short) 1,
5960
true,
6061
new Department(null, "hr", new City(null, "cairo")),
61-
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", ACTIVE))
62+
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", ACTIVE)),
63+
UUID.fromString("2dfb7bc7-38a6-4826-b6d3-297969d17244")
6264
),
6365
new Employee(null,
6466
"mohammad",
@@ -70,7 +72,8 @@ public void setup() {
7072
(short) 1,
7173
true,
7274
new Department(null, "sw arch", new City(null, "giaz")),
73-
Arrays.asList(new Task(null, "fix sw arch", ACTIVE), new Task(null, "fix sw arch", ACTIVE))
75+
Arrays.asList(new Task(null, "fix sw arch", ACTIVE), new Task(null, "fix sw arch", ACTIVE)),
76+
UUID.randomUUID()
7477
),
7578
new Employee(null,
7679
"mostafa",
@@ -82,7 +85,8 @@ public void setup() {
8285
(short) 2,
8386
true,
8487
new Department(null, "sw dev", new City(null, "alex")),
85-
Arrays.asList(new Task(null, "fix sw dev", ACTIVE), new Task(null, "fix sw dev", ACTIVE))
88+
Arrays.asList(new Task(null, "fix sw dev", ACTIVE), new Task(null, "fix sw dev", ACTIVE)),
89+
UUID.randomUUID()
8690
),
8791
new Employee(null,
8892
"wael",
@@ -94,7 +98,8 @@ public void setup() {
9498
(short) 2,
9599
true,
96100
new Department(null, "hr", new City(null, "cairo")),
97-
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", ACTIVE))
101+
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", ACTIVE)),
102+
UUID.randomUUID()
98103
),
99104
new Employee(null,
100105
"farida",
@@ -106,7 +111,8 @@ public void setup() {
106111
(short) 2,
107112
false,
108113
new Department(null, "hr", new City(null, "cairo")),
109-
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", NOT_ACTIVE))
114+
Arrays.asList(new Task(null, "fix hr", ACTIVE), new Task(null, "fix hr", NOT_ACTIVE)),
115+
UUID.randomUUID()
110116
),
111117
new Employee(null,
112118
"fofo",
@@ -118,7 +124,8 @@ public void setup() {
118124
(short) 2,
119125
false,
120126
null,
121-
null
127+
null,
128+
UUID.randomUUID()
122129
)
123130
);
124131
employeeRepository.saveAll(employees);
@@ -540,6 +547,18 @@ public void testBooleanOperatorFromJava() {
540547
// where e.active=?
541548
}
542549

550+
@Test
551+
public void testUUID() throws Exception {
552+
String json = loadResourceJsonFile("testUUID");
553+
554+
Expressions expressions = new ObjectMapper().readValue(json, Expressions.class);
555+
556+
List<Employee> employeeList = employeeRepository.findAll(expressions);
557+
assertThat(employeeList.size()).isEqualTo(1);
558+
559+
// where e.active=?
560+
}
561+
543562
@SneakyThrows
544563
private String loadResourceJsonFile(String name) {
545564
File file = ResourceUtils.getFile("classpath:" + name + ".json");

src/test/java/com/github/mhewedy/expressions/model/Employee.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.Instant;
88
import java.time.LocalDate;
99
import java.util.List;
10+
import java.util.UUID;
1011

1112
@Entity
1213
@NoArgsConstructor
@@ -34,4 +35,5 @@ public class Employee extends Auditable {
3435
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
3536
@JoinColumn(name = "employee_id")
3637
public List<Task> tasks;
38+
public UUID serial;
3739
}

src/test/resources/testUUID.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"serial": "2dfb7bc7-38a6-4826-b6d3-297969d17244"
3+
}

0 commit comments

Comments
 (0)