Skip to content

Commit 4e437de

Browse files
committed
More robust mref merging
1 parent c1d0064 commit 4e437de

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/yaml_loader.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,36 @@ def self.expand(filename, obj, yaml_opts = {})
7474

7575
target_obj = expand(filename, target_obj, yaml_opts)
7676
target_obj.each do |target_key, target_value|
77-
new_obj[target_key] = target_value
77+
if (new_obj[target_key].is_a?(Hash))
78+
raise "Should be a hash" unless target_value.is_a?(Hash)
79+
new_obj[target_key] = target_value.merge(new_obj[target_key])
80+
else
81+
new_obj[target_key] = target_value
82+
end
7883
end
7984
end
8085

8186
obj.delete("$mref")
82-
obj_keys = obj.keys
83-
obj_keys.each do |key|
84-
value = obj[key]
85-
86-
expanded = expand(filename, value, yaml_opts)
87-
if new_obj[key].is_a?(Hash)
88-
raise "should be a hash" unless expanded.is_a?(Hash)
89-
new_obj[key].merge!(expanded)
87+
# now merge target_obj and obj
88+
keys = (obj.keys + new_obj.keys).uniq
89+
final_obj = {}
90+
keys.each do |key|
91+
if !obj.key?(key)
92+
final_obj[key] = new_obj[key]
93+
elsif !new_obj.key?(key)
94+
final_obj[key] = expand(filename, obj[key], yaml_opts)
9095
else
91-
new_obj[key] = expanded
96+
value = obj[key]
97+
98+
if new_obj[key].is_a?(Hash)
99+
raise "should be a hash" unless new_obj[key].is_a?(Hash)
100+
final_obj[key] = new_obj[key].merge(obj[key])
101+
else
102+
final_obj[key] = expand(filename, obj[key], yaml_opts)
103+
end
92104
end
93105
end
94-
new_obj
106+
final_obj
95107
else
96108
obj_keys = obj.keys
97109
obj_keys.each do |key|

0 commit comments

Comments
 (0)