Skip to content

Commit 4dde216

Browse files
committed
refactor Rules.from to perform type assertions before heavyweight list manipulation
1 parent 13bef8d commit 4dde216

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/stableconfig/Rule.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,6 @@ public static Rule from(Map<?, ?> map) {
3838
selectorsObj);
3939
}
4040

41-
List<Selector> selectors =
42-
unmodifiableList(
43-
((List<?>) selectorsObj)
44-
.stream()
45-
.filter(Objects::nonNull)
46-
.map(
47-
s -> {
48-
if (s instanceof Map) {
49-
return Selector.from((Map<?, ?>) s);
50-
}
51-
throwStableConfigMappingException(
52-
"Each selector must be a map, but got: "
53-
+ s.getClass().getSimpleName()
54-
+ ": ",
55-
s);
56-
return null;
57-
})
58-
.collect(toList()));
59-
6041
Object configObj = map.get("configuration");
6142
if (configObj == null) {
6243
throwStableConfigMappingException("Missing 'configuration' in rule:", map);
@@ -66,9 +47,26 @@ public static Rule from(Map<?, ?> map) {
6647
"'configuration' must be a map, but got: " + configObj.getClass().getSimpleName() + ": ",
6748
configObj);
6849
}
69-
Map<String, Object> configuration = (Map<String, Object>) configObj;
7050

71-
return new Rule(selectors, configuration);
51+
List<Selector> selectors =
52+
((List<?>) selectorsObj)
53+
.stream()
54+
.filter(Objects::nonNull)
55+
.map(
56+
s -> {
57+
if (!(s instanceof Map)) {
58+
throwStableConfigMappingException(
59+
"Each selector must be a map, but got: "
60+
+ s.getClass().getSimpleName()
61+
+ ": ",
62+
s);
63+
}
64+
65+
return Selector.from((Map<?, ?>) s);
66+
})
67+
.collect(toList());
68+
69+
return new Rule(unmodifiableList(selectors), (Map<String, Object>) configObj);
7270
}
7371

7472
public List<Selector> getSelectors() {

internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/StableConfigSourceTest.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ class StableConfigSourceTest extends DDSpecification {
160160
DD_SERVICE: "test"
161161
''' | "'selectors' must be a list, but got: String"
162162
'''apm_configuration_rules:
163-
- selectors:
164-
- "not-a-map"
165-
''' | "Each selector must be a map, but got: String"
163+
- selectors:
164+
- "not-a-map"
165+
configuration:
166+
DD_SERVICE: "test"
167+
''' | "Each selector must be a map, but got: String"
166168
'''apm_configuration_rules:
167169
- selectors:
168170
- origin: process_arguments

0 commit comments

Comments
 (0)