Skip to content

Commit 5b639aa

Browse files
committed
Github suggestions: Use static factory methods as constructors for Rule and Selector classes
1 parent e730cbe commit 5b639aa

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ public Rule(List<Selector> selectors, Map<String, Object> configuration) {
2525
this.configuration = configuration;
2626
}
2727

28-
public Rule(Object yaml) {
29-
if (!(yaml instanceof Map)) {
30-
throw new StableConfigMappingException(
31-
"Rule must be a map, but got: " + yaml.getClass().getSimpleName());
32-
}
33-
Map map = (Map) yaml;
34-
28+
public static Rule from(Map<?, ?> map) {
3529
Object selectorsObj = map.get("selectors");
3630
if (selectorsObj == null) {
3731
throw new StableConfigMappingException("Missing 'selectors' in rule: " + map);
@@ -40,10 +34,20 @@ public Rule(Object yaml) {
4034
throw new StableConfigMappingException(
4135
"'selectors' must be a list, but got: " + selectorsObj.getClass().getSimpleName());
4236
}
43-
selectors =
37+
List<Selector> selectors =
4438
unmodifiableList(
45-
((List<Object>) selectorsObj)
46-
.stream().filter(Objects::nonNull).map(Selector::new).collect(toList()));
39+
((List<?>) selectorsObj)
40+
.stream()
41+
.filter(Objects::nonNull)
42+
.map(
43+
s -> {
44+
if (!(s instanceof Map)) {
45+
throw new StableConfigMappingException(
46+
"Selector must be a map, but got: " + s.getClass().getSimpleName());
47+
}
48+
return Selector.from((Map<?, ?>) s);
49+
})
50+
.collect(toList()));
4751

4852
Object configObj = map.get("configuration");
4953
if (configObj == null) {
@@ -53,7 +57,9 @@ public Rule(Object yaml) {
5357
throw new StableConfigMappingException(
5458
"'configuration' must be a map, but got: " + configObj.getClass().getSimpleName());
5559
}
56-
configuration = unmodifiableMap((Map<String, Object>) configObj);
60+
Map<String, Object> configuration = (Map<String, Object>) configObj;
61+
62+
return new Rule(selectors, configuration);
5763
}
5864

5965
public List<Selector> getSelectors() {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ public Selector(String origin, String key, List<String> matches, String operator
1717
this.operator = operator;
1818
}
1919

20-
public Selector(Object yaml) {
21-
if (!(yaml instanceof Map)) {
22-
throw new StableConfigMappingException(
23-
"Selector must be a map, but got: " + yaml.getClass().getSimpleName());
24-
}
25-
Map map = (Map) yaml;
26-
20+
public static Selector from(Map<?, ?> map) {
2721
Object originObj = map.get("origin");
2822
if (originObj == null) {
2923
throw new StableConfigMappingException("Missing 'origin' in selector: " + map);
@@ -32,18 +26,18 @@ public Selector(Object yaml) {
3226
throw new StableConfigMappingException(
3327
"'origin' must be a string, but got: " + originObj.getClass().getSimpleName());
3428
}
35-
origin = (String) originObj;
29+
String origin = (String) originObj;
3630

3731
Object keyObj = map.get("key");
38-
key = (keyObj instanceof String) ? (String) keyObj : null;
32+
String key = (keyObj instanceof String) ? (String) keyObj : null;
3933

4034
Object matchesObj = map.get("matches");
4135
if (matchesObj != null && !(matchesObj instanceof List)) {
4236
throw new StableConfigMappingException(
4337
"'matches' must be a list, but got: " + matchesObj.getClass().getSimpleName());
4438
}
4539
List<String> rawMatches = (List<String>) matchesObj;
46-
matches =
40+
List<String> matches =
4741
rawMatches != null ? Collections.unmodifiableList(rawMatches) : Collections.emptyList();
4842

4943
Object operatorObj = map.get("operator");
@@ -54,7 +48,9 @@ public Selector(Object yaml) {
5448
throw new StableConfigMappingException(
5549
"'operator' must be a string, but got: " + operatorObj.getClass().getSimpleName());
5650
}
57-
operator = (String) operatorObj;
51+
String operator = (String) operatorObj;
52+
53+
return new Selector(origin, key, matches, operator);
5854
}
5955

6056
public String getOrigin() {

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static java.util.Collections.emptyMap;
55
import static java.util.Collections.unmodifiableList;
66
import static java.util.Collections.unmodifiableMap;
7-
import static java.util.stream.Collectors.toList;
87

98
import java.util.ArrayList;
109
import java.util.List;
@@ -21,10 +20,7 @@ public StableConfig(Object yaml) {
2120
this.apmConfigurationDefault =
2221
unmodifiableMap(
2322
(Map<String, Object>) map.getOrDefault("apm_configuration_default", emptyMap()));
24-
this.apmConfigurationRules =
25-
unmodifiableList(
26-
((List<Object>) map.getOrDefault("apm_configuration_rules", emptyList()))
27-
.stream().map(Rule::new).collect(toList()));
23+
this.apmConfigurationRules = parseRules(map);
2824
}
2925

3026
// test only
@@ -45,4 +41,25 @@ public Map<String, Object> getApmConfigurationDefault() {
4541
public List<Rule> getApmConfigurationRules() {
4642
return apmConfigurationRules;
4743
}
44+
45+
private List<Rule> parseRules(Map<?, ?> map) {
46+
Object rulesObj = map.get("apm_configuration_rules");
47+
if (rulesObj instanceof List) {
48+
List<?> rulesList = (List<?>) rulesObj;
49+
List<Rule> rules = new ArrayList<>();
50+
for (Object ruleObj : rulesList) {
51+
if (ruleObj instanceof Map) {
52+
rules.add(Rule.from((Map<?, ?>) ruleObj));
53+
} else {
54+
// Log or throw with a detailed message
55+
throw new StableConfigMappingException(
56+
"Rule must be a map, but got: "
57+
+ (ruleObj == null ? "null" : ruleObj.getClass().getSimpleName()));
58+
}
59+
}
60+
return unmodifiableList(rules);
61+
} else {
62+
return emptyList();
63+
}
64+
}
4865
}

0 commit comments

Comments
 (0)