How to merge and keep original anchors #1452
-
First of all, thank you for this wonderful project. It makes yaml operations humanly :) Describe the bug Version of yq: 4.30.4 old_vars.yml: clickhouse:
enabled: true
global:
name: app1 new_vars.yml: clickhouse: &clickhouse
enabled: false
global:
name: app1
clickhouse: *clickhouse Command
Actual behavior clickhouse:
enabled: true
global:
name: app1
clickhouse: *clickhouse Expected behavior clickhouse: &clickhouse #Should have anchor here, like &clickhouse
enabled: true
global:
name: app1
clickhouse: *clickhouse # This is not a valid alias now
Additional context I've 2 files, one the customized by me, other one the software updates, usually every release will have small changes in this yaml. I'm using yq to merge these 2 files, but this anchor is becoming a problem. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is intentionally how merge operates, it merges all the nodes (including key nodes) - and the overriding key node does not have an anchor set. What you need to do is merge in the leaf nodes only - I've had a think about this, and you could do it like so: yq '(load("old_vars.yaml") | .. | select(tag != "!!map" and tag != "!!seq")) as $i ireduce(.; setpath($i | path; $i))' new_vars.yaml Explanation:
|
Beta Was this translation helpful? Give feedback.
This is intentionally how merge operates, it merges all the nodes (including key nodes) - and the overriding key node does not have an anchor set.
What you need to do is merge in the leaf nodes only - I've had a think about this, and you could do it like so:
yq '(load("old_vars.yaml") | .. | select(tag != "!!map" and tag != "!!seq")) as $i ireduce(.; setpath($i | path; $i))' new_vars.yaml
Explanation:
setpath
on each one, setting the old_var value at that path.