Skip to content

Commit

Permalink
fix merge order
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Sep 28, 2024
1 parent 086e284 commit 0313407
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions planetiler-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
<groupId>org.snakeyaml</groupId>
<artifactId>snakeyaml-engine</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ private static void handleMergeOperator(Object parsed) {
if (parsed instanceof Map<?, ?> map) {
Object toMerge = map.remove("<<");
if (toMerge != null) {
Map<?, ?> orig = new LinkedHashMap<>(map);
var orig = new LinkedHashMap<>(map);
// to preserve the map key order we insert the merged operator objects first, then the original ones
map.clear();
mergeInto(map, toMerge);
mergeInto(map, orig);
mergeInto(map, toMerge, false);
mergeInto(map, orig, true);
}
for (var value : map.values()) {
handleMergeOperator(value);
Expand All @@ -68,12 +68,16 @@ private static void handleMergeOperator(Object parsed) {
}

@SuppressWarnings("rawtypes")
private static void mergeInto(Map dest, Object source) {
private static void mergeInto(Map dest, Object source, boolean replace) {
if (source instanceof Map<?, ?> map) {
dest.putAll(map);
if (replace) {
dest.putAll(map);
} else {
map.forEach(dest::putIfAbsent);
}
} else if (source instanceof List<?> nesteds) {
for (var nested : nesteds) {
mergeInto(dest, nested);
mergeInto(dest, nested, replace);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void testMergeOperatorMultiple() {
a: 2
b: 3
dest:
a: 2
a: 1 # from label1 since it came first
b: 4
c: 5
z: 1
Expand Down Expand Up @@ -182,10 +182,11 @@ void testMergeOperatorFromDraft1() {
- &BIG { r: 10 }
- &SMALL { r: 1 }
- # Merge one map
<< : *CENTER
r: 10
label: center/big
<< : *CENTER
r: 10
label: center/big
""");
}

@Test
void testMergeOperatorFromDraft2() {
Expand Down

0 comments on commit 0313407

Please sign in to comment.