-
Notifications
You must be signed in to change notification settings - Fork 723
/
OverrideMergeStrategy.java
78 lines (72 loc) · 3.2 KB
/
OverrideMergeStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package io.jenkins.plugins.casc.yaml;
import hudson.Extension;
import io.jenkins.plugins.casc.ConfiguratorConflictException;
import io.jenkins.plugins.casc.ConfiguratorException;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
/**
* Override the configuration by loading order
*/
@Extension
public class OverrideMergeStrategy implements MergeStrategy {
@Override
public void merge(Node root, Node node, String source) throws ConfiguratorException {
if (root.getNodeId() != node.getNodeId()) {
// means one of those yaml file doesn't conform to JCasC schema
throw new ConfiguratorException(
String.format("Found incompatible configuration elements %s %s", source, node.getStartMark()));
}
switch (root.getNodeId()) {
case sequence:
SequenceNode seq = (SequenceNode) root;
SequenceNode seq2 = (SequenceNode) node;
seq.getValue().addAll(seq2.getValue());
return;
case mapping:
MappingNode map = (MappingNode) root;
MappingNode map2 = (MappingNode) node;
// merge common entries
for (int i = 0; i < map2.getValue().size(); ) {
NodeTuple t2 = map2.getValue().get(i);
boolean found = false;
for (NodeTuple tuple : map.getValue()) {
final Node key = tuple.getKeyNode();
final Node key2 = t2.getKeyNode();
if (key.getNodeId() == NodeId.scalar) {
// We dont support merge for more complex cases (yet)
if (((ScalarNode) key).getValue().equals(((ScalarNode) key2).getValue())) {
try {
merge(tuple.getValueNode(), t2.getValueNode(), source);
} catch (ConfiguratorConflictException e) {
map.getValue().set(map.getValue().indexOf(tuple), t2);
}
map2.getValue().remove(i);
found = true;
break;
}
} else {
throw new ConfiguratorException(String.format(
"Found non-mergeable configuration keys %s %s)", source, node.getEndMark()));
}
}
if (!found) {
++i;
}
}
// .. and add others
map.getValue().addAll(map2.getValue());
return;
default:
throw new ConfiguratorConflictException(
String.format("Found conflicting configuration at %s %s", source, node.getStartMark()));
}
}
@Override
public String getName() {
return "override";
}
}