Skip to content

Commit 364dc27

Browse files
authored
fix: exclude API to handle primitive collection or arrays (#3)
1 parent bbadb9f commit 364dc27

11 files changed

Lines changed: 235 additions & 69 deletions

File tree

src/main/java/io/github/trackerforce/PathExclude.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ private Object handleCollectionOrArray(Object value, String path, ExclusionNode
109109
boolean isArray = value.getClass().isArray();
110110

111111
if (isArray) {
112+
if (isPrimitiveArray(value)) {
113+
return value;
114+
}
115+
112116
array = (Object[]) value;
113117
list = Arrays.asList((Object[]) value);
114118
} else {
@@ -120,6 +124,10 @@ private Object handleCollectionOrArray(Object value, String path, ExclusionNode
120124
return isArray ? array : list;
121125
}
122126

127+
return addElementsToList(path, node, list);
128+
}
129+
130+
private List<Object> addElementsToList(String path, ExclusionNode node, List<?> list) {
123131
List<Object> items = new ArrayList<>();
124132
for (Object element : list) {
125133
if (isSimpleValue(element)) {
@@ -130,7 +138,6 @@ private Object handleCollectionOrArray(Object value, String path, ExclusionNode
130138
items.add(elementMap);
131139
}
132140
}
133-
134141
return items;
135142
}
136143

@@ -140,6 +147,12 @@ private boolean isSimpleValue(Object value) {
140147
value.getClass().isPrimitive();
141148
}
142149

150+
private boolean isPrimitiveArray(Object value) {
151+
return value instanceof boolean[] || value instanceof byte[] || value instanceof char[] ||
152+
value instanceof short[] || value instanceof int[] || value instanceof long[] ||
153+
value instanceof float[] || value instanceof double[];
154+
}
155+
143156
private List<String> getPropertyNames(Class<?> clazz) {
144157
List<String> names = new ArrayList<>();
145158
if (clazz.isRecord()) {

src/test/java/io/github/trackerforce/fixture/clazz/Address.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,36 @@ public class Address {
1414
String state;
1515
String zipCode;
1616
String country;
17+
int[] coordinates;
18+
19+
public static Address of123() {
20+
return new Address(
21+
"123 Main St",
22+
"Springfield",
23+
"IL",
24+
"62701",
25+
"USA",
26+
new int[]{40, 90}
27+
);
28+
}
29+
30+
public static Address of456() {
31+
return new Address(
32+
"456 Elm St",
33+
"Springfield",
34+
"IL",
35+
"62701",
36+
"USA",
37+
new int[]{39, 89});
38+
}
39+
40+
public static Address of789() {
41+
return new Address(
42+
"789 Oak St",
43+
"Springfield",
44+
"IL",
45+
"62701",
46+
"USA",
47+
new int[]{39, 89});
48+
}
1749
}

src/test/java/io/github/trackerforce/fixture/clazz/Occupation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,26 @@ public class Occupation {
1515
String department;
1616
int yearsOfExperience;
1717
Address address;
18+
19+
public static Occupation ofSoftwareEngineer() {
20+
return new Occupation(
21+
"Software Engineer",
22+
"Develops software applications",
23+
90000.00,
24+
"Engineering",
25+
5,
26+
new Address("123 Tech St", "Tech City", "CA", "90001", "USA", new int[]{37, 122})
27+
);
28+
}
29+
30+
public static Occupation ofProjectManager() {
31+
return new Occupation(
32+
"Project Manager",
33+
"Manages software projects",
34+
95000.00,
35+
"Management",
36+
7,
37+
new Address("456 Project Ave", "Project City", "CA", "90002", "USA", new int[]{34, 118})
38+
);
39+
}
1840
}

src/test/java/io/github/trackerforce/fixture/clazz/Order.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,20 @@ public class Order {
1414
List<Product> products;
1515
Date orderDate;
1616
String orderId;
17+
18+
public static Order ofOrder123() {
19+
return new Order(
20+
List.of(Product.ofLaptop(), Product.ofSmartphone()),
21+
new Date(),
22+
"order123"
23+
);
24+
}
25+
26+
public static Order ofOrder456() {
27+
return new Order(
28+
List.of(Product.ofHeadphones()),
29+
new Date(),
30+
"order456"
31+
);
32+
}
1733
}

src/test/java/io/github/trackerforce/fixture/clazz/Product.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,34 @@ public class Product {
1515
double price;
1616
String category;
1717
int stockQuantity;
18+
19+
public static Product ofLaptop() {
20+
return new Product(
21+
"1",
22+
"Laptop",
23+
"High-end gaming laptop",
24+
1500.00,
25+
"Electronics",
26+
5);
27+
}
28+
29+
public static Product ofSmartphone() {
30+
return new Product(
31+
"2",
32+
"Smartphone",
33+
"Latest model smartphone",
34+
800.00,
35+
"Electronics",
36+
10);
37+
}
38+
39+
public static Product ofHeadphones() {
40+
return new Product(
41+
"3",
42+
"Headphones",
43+
"Noise-cancelling headphones",
44+
200.00,
45+
"Accessories",
46+
15);
47+
}
1848
}
Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package io.github.trackerforce.fixture.clazz;
22

3-
import java.util.Date;
4-
import java.util.List;
5-
import java.util.Map;
63
import lombok.AccessLevel;
74
import lombok.AllArgsConstructor;
85
import lombok.Data;
96
import lombok.experimental.FieldDefaults;
107

8+
import java.util.List;
9+
import java.util.Map;
10+
1111
@Data
1212
@AllArgsConstructor
1313
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@@ -22,55 +22,34 @@ public class UserDetail {
2222
Occupation[] occupations;
2323
Map<String, String> additionalInfo;
2424
Map<String, Address> locations;
25+
int[] scoresArray;
2526

2627
public static UserDetail of() {
2728
return new UserDetail(
2829
"john_doe",
2930
"jown@email.com",
3031
"John Doe",
3132
"+1234567890",
32-
new Address(
33-
"123 Main St",
34-
"Springfield",
35-
"IL",
36-
"62701",
37-
"USA"
38-
),
33+
Address.of123(),
3934
List.of(
40-
new Order(
41-
List.of(
42-
new Product("1", "Laptop", "High-end gaming laptop", 1500.00, "Electronics", 5),
43-
new Product("2", "Smartphone", "Latest model smartphone", 800.00, "Electronics", 10)
44-
),
45-
new Date(),
46-
"order123"
47-
),
48-
new Order(
49-
List.of(
50-
new Product("3", "Headphones", "Noise-cancelling headphones", 200.00, "Accessories", 15)
51-
),
52-
new Date(),
53-
"order456"
54-
)
35+
Order.ofOrder123(),
36+
Order.ofOrder456()
5537
),
5638
new String[] {"USER", "ADMIN"},
5739
new Occupation[] {
58-
new Occupation("Software Engineer", "Develops software applications", 90000.00, "Engineering", 5,
59-
new Address("123 Tech St", "Tech City", "CA", "90001", "USA")
60-
),
61-
new Occupation("Project Manager", "Manages software projects", 95000.00, "Management", 7,
62-
new Address("456 Project Ave", "Project City", "CA", "90002", "USA")
63-
)
40+
Occupation.ofSoftwareEngineer(),
41+
Occupation.ofProjectManager()
6442
},
6543
Map.of(
6644
"preferredLanguage", "English",
6745
"subscriptionStatus", "Active",
6846
"lastLogin", "2023-10-01T12:00:00Z"
6947
),
7048
Map.of(
71-
"home", new Address("456 Elm St", "Springfield", "IL", "62701", "USA"),
72-
"work", new Address("789 Oak St", "Springfield", "IL", "62701", "USA")
73-
)
49+
"home", Address.of456(),
50+
"work", Address.of789()
51+
),
52+
new int[] {85, 90, 95}
7453
);
7554
}
7655
}

src/test/java/io/github/trackerforce/fixture/record/Address.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@ public record Address(
55
String city,
66
String state,
77
String zipCode,
8-
String country
8+
String country,
9+
int[] coordinates
910
) {
11+
public static Address of123() {
12+
return new Address(
13+
"123 Main St",
14+
"Springfield",
15+
"IL",
16+
"62701",
17+
"USA",
18+
new int[]{40, 90}
19+
);
20+
}
21+
22+
public static Address of456() {
23+
return new Address(
24+
"456 Elm St",
25+
"Springfield",
26+
"IL",
27+
"62701",
28+
"USA",
29+
new int[]{39, 89});
30+
}
31+
32+
public static Address of789() {
33+
return new Address(
34+
"789 Oak St",
35+
"Springfield",
36+
"IL",
37+
"62701",
38+
"USA",
39+
new int[]{39, 89});
40+
}
1041
}

src/test/java/io/github/trackerforce/fixture/record/Occupation.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@ public record Occupation(
88
int yearsOfExperience,
99
Address address
1010
) {
11+
public static Occupation ofSoftwareEngineer() {
12+
return new Occupation(
13+
"Software Engineer",
14+
"Develops software applications",
15+
90000.00,
16+
"Engineering",
17+
5,
18+
new Address("123 Tech St", "Tech City", "CA", "90001", "USA", new int[]{37, 122})
19+
);
20+
}
21+
22+
public static Occupation ofProjectManager() {
23+
return new Occupation(
24+
"Project Manager",
25+
"Manages software projects",
26+
95000.00,
27+
"Management",
28+
7,
29+
new Address("456 Project Ave", "Project City", "CA", "90002", "USA", new int[]{34, 118})
30+
);
31+
}
1132
}

src/test/java/io/github/trackerforce/fixture/record/Order.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,19 @@ public record Order(
88
Date orderDate,
99
String orderId
1010
) {
11+
public static Order ofOrder123() {
12+
return new Order(
13+
List.of(Product.ofLaptop(), Product.ofSmartphone()),
14+
new Date(),
15+
"order123"
16+
);
17+
}
18+
19+
public static Order ofOrder456() {
20+
return new Order(
21+
List.of(Product.ofHeadphones()),
22+
new Date(),
23+
"order456"
24+
);
25+
}
1126
}

src/test/java/io/github/trackerforce/fixture/record/Product.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,33 @@ public record Product(
88
String category,
99
int stockQuantity
1010
) {
11+
public static Product ofLaptop() {
12+
return new Product(
13+
"1",
14+
"Laptop",
15+
"High-end gaming laptop",
16+
1500.00,
17+
"Electronics",
18+
5);
19+
}
20+
21+
public static Product ofSmartphone() {
22+
return new Product(
23+
"2",
24+
"Smartphone",
25+
"Latest model smartphone",
26+
800.00,
27+
"Electronics",
28+
10);
29+
}
30+
31+
public static Product ofHeadphones() {
32+
return new Product(
33+
"3",
34+
"Headphones",
35+
"Noise-cancelling headphones",
36+
200.00,
37+
"Accessories",
38+
15);
39+
}
1140
}

0 commit comments

Comments
 (0)