@Override
public void process(MapNode configuration) {
Set<String> activeProfiles = parseProfiles(System.getProperty(SEEDSTACK_PROFILES_PROPERTY, ""));
Map<MapNode, List<String>> toRemove = new HashMap<>();
Map<TreeNode, Map<String, String>> moves = new HashMap<>();
configuration.walk()
.filter(node -> node instanceof MapNode)
.map(MapNode.class::cast)
.forEach(mapNode -> mapNode.namedNodes().forEach(namedNode -> {
Matcher matcher = keyWithProfilePattern.matcher(namedNode.name());
if (matcher.matches()) {
if (parseProfiles(matcher.group(2)).stream().noneMatch(activeProfiles::contains)) {
toRemove.computeIfAbsent(mapNode, k -> new ArrayList<>()).add(namedNode.name());
} else {
moves.computeIfAbsent(mapNode, k -> new HashMap<>())
.put(matcher.group(0), matcher.group(1));
}
}
}));
toRemove.forEach((node, list) -> list.forEach(node::remove));
for (Map.Entry<TreeNode, Map<String, String>> movesEntry : moves.entrySet()) {
for (Map.Entry<String, String> moveEntry : movesEntry.getValue().entrySet()) {
movesEntry.getKey().move(moveEntry.getKey(), moveEntry.getValue());
}
}
}