Skip to content

Commit 9b649f6

Browse files
Merge remote-tracking branch 'origin/main' into 250-use-$mref-to-inherit-from-one-profile-to-another-or-one-profile-to-a-certificate
2 parents 1e8183f + 74cebf0 commit 9b649f6

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

lib/test/test_yaml_loader.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class TestYamlLoader < Minitest::Test
99

10+
1011
def test_remove
1112
yaml = <<~YAML
1213
base:
@@ -41,7 +42,7 @@ def test_multiple_remove
4142
- key3
4243
key4: value4
4344
YAML
44-
45+
4546
f = Tempfile.new("yml")
4647
f.write(yaml)
4748
f.flush

lib/yaml_loader.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,38 @@ def self.expand(filename, obj, yaml_opts = {})
8585

8686
target_obj = expand(filename, target_obj, yaml_opts)
8787
target_obj.each do |target_key, target_value|
88-
89-
new_obj[target_key] = target_value
88+
if (new_obj[target_key].is_a?(Hash))
89+
raise "Should be a hash" unless target_value.is_a?(Hash)
90+
new_obj[target_key] = target_value.merge(new_obj[target_key])
91+
else
92+
new_obj[target_key] = target_value
93+
end
9094
end
9195
end
9296

9397
obj.delete("$mref")
94-
obj_keys = obj.keys
95-
obj_keys.each do |key|
96-
value = obj[key]
97-
98-
expanded = expand(filename, value, yaml_opts)
99-
if new_obj[key].is_a?(Hash)
100-
raise "should be a hash" unless expanded.is_a?(Hash)
101-
new_obj[key].merge!(expanded)
98+
# now merge target_obj and obj
99+
keys = (obj.keys + new_obj.keys).uniq
100+
final_obj = {}
101+
keys.each do |key|
102+
if !obj.key?(key)
103+
final_obj[key] = new_obj[key]
104+
elsif !new_obj.key?(key)
105+
final_obj[key] = expand(filename, obj[key], yaml_opts)
102106
else
103-
new_obj[key] = expanded
107+
value = obj[key]
108+
109+
if new_obj[key].is_a?(Hash)
110+
raise "should be a hash" unless new_obj[key].is_a?(Hash)
111+
final_obj[key] = new_obj[key].merge(obj[key])
112+
else
113+
final_obj[key] = expand(filename, obj[key], yaml_opts)
114+
end
104115
end
105116
end
106117

107-
new_obj
118+
final_obj
119+
108120
else
109121
obj_keys = obj.keys
110122
obj_keys.each do |key|

0 commit comments

Comments
 (0)