Skip to content

Commit f88b122

Browse files
author
Kevin Paulisse
committed
Add code and spec for overriding elements within a hash
1 parent 964d92b commit f88b122

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

  • lib/octocatalog-diff/catalog-util
  • spec/octocatalog-diff/tests/catalog-util

lib/octocatalog-diff/catalog-util/enc.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative 'enc/script'
66

77
require 'stringio'
8+
require 'yaml'
89

910
module OctocatalogDiff
1011
module CatalogUtil
@@ -46,6 +47,36 @@ def error_message
4647

4748
private
4849

50+
# Override of ENC parameters with parameters specified on the command line.
51+
# Modifies structures in @enc_obj.
52+
# @param logger [Logger] Logger object
53+
def override_enc_parameters(_logger)
54+
return unless @options[:enc_override].is_a?(Array) && @options[:enc_override].any?
55+
content_structure = YAML.load(@content)
56+
@options[:enc_override].each { |x| merge_enc_param(x.key, x.value) }
57+
@content = content_structure.to_yaml
58+
end
59+
60+
# Merging behavior for ENC overrides
61+
# @param pointer [Hash] Portion of the content structure to modify
62+
# @param key [String] String representing structure, delimited by '::'
63+
# @param value [?] Value to insert at structure point
64+
def merge_enc_param(pointer, key, value)
65+
if key =~ /::/
66+
first_key, the_rest = key.split(/::/, 2)
67+
if pointer[first_key].nil?
68+
pointer[first_key] = {}
69+
elsif !pointer[first_key].is_a?(Hash)
70+
raise ArgumentError, "Attempt to override #{pointer[first_key].class} with hash for #{key}"
71+
end
72+
merge_enc_param(pointer[first_key], the_rest, value)
73+
elsif value.nil?
74+
pointer.delete(key)
75+
else
76+
pointer[key] = value
77+
end
78+
end
79+
4980
# Backend - given options, choose an appropriate backend and construct the corresponding object.
5081
# @return [?] Backend object
5182
def backend
@@ -72,6 +103,7 @@ def execute(logger = nil)
72103
logger ||= @options[:logger]
73104
logger ||= Logger.new(StringIO.new)
74105
@enc_obj.execute(logger) if @enc_obj.respond_to?(:execute)
106+
override_enc_parameters(logger)
75107
@executed = true
76108
end
77109
end

spec/octocatalog-diff/tests/catalog-util/enc_spec.rb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,100 @@
126126
end
127127
end
128128
end
129+
130+
describe '#merge_enc_param' do
131+
before(:each) do
132+
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
133+
@subject = described_class.allocate
134+
end
135+
136+
it 'should replace a parameter at the top level' do
137+
struct = {
138+
'parameters' => {
139+
'foo' => 'bar',
140+
'baz' => 'fuzz'
141+
}
142+
}
143+
@subject.send(:merge_enc_param, struct, 'parameters', 'baz4')
144+
expect(struct['parameters']).to eq('baz4')
145+
end
146+
147+
it 'should add a parameter at the top level' do
148+
struct = {
149+
'parameters' => {
150+
'foo' => 'bar',
151+
'baz' => 'fuzz'
152+
}
153+
}
154+
@subject.send(:merge_enc_param, struct, 'stuffs', 'baz4')
155+
expect(struct['stuffs']).to eq('baz4')
156+
end
157+
158+
it 'should remove a parameter at the top level' do
159+
struct = {
160+
'parameters' => {
161+
'foo' => 'bar',
162+
'baz' => 'fuzz'
163+
}
164+
}
165+
@subject.send(:merge_enc_param, struct, 'parameters', nil)
166+
expect(struct).to eq({})
167+
end
168+
169+
it 'should error if breaking the structure' do
170+
struct = {
171+
'parameters' => {
172+
'foo' => 'bar',
173+
'baz' => 'fuzz'
174+
}
175+
}
176+
expect do
177+
@subject.send(:merge_enc_param, struct, 'parameters::foo::baz3', 'baz4')
178+
end.to raise_error(ArgumentError, /Attempt to override String with hash for foo::baz3/)
179+
end
180+
181+
it 'should overwrite a key nested in a hash with same datatype' do
182+
struct = {
183+
'parameters' => {
184+
'foo' => 'bar',
185+
'baz' => 'fuzz'
186+
}
187+
}
188+
@subject.send(:merge_enc_param, struct, 'parameters::foo', 'baz4')
189+
expect(struct['parameters']['foo']).to eq('baz4')
190+
end
191+
192+
it 'should overwrite a key nested in a hash with different datatype' do
193+
struct = {
194+
'parameters' => {
195+
'foo' => 'bar',
196+
'baz' => 'fuzz'
197+
}
198+
}
199+
@subject.send(:merge_enc_param, struct, 'parameters::foo', %w(kittens puppies))
200+
expect(struct['parameters']['foo']).to eq(%w(kittens puppies))
201+
end
202+
203+
it 'should delete a key nested in a hash' do
204+
struct = {
205+
'parameters' => {
206+
'foo' => 'bar',
207+
'baz' => 'fuzz'
208+
}
209+
}
210+
@subject.send(:merge_enc_param, struct, 'parameters::foo', nil)
211+
expect(struct['parameters'].key?('foo')).to eq(false)
212+
end
213+
214+
it 'should overwrite a key nested deeply in a hash with same datatype' do
215+
struct = {
216+
'parameters' => {
217+
'foo' => { 'bar' => { 'baz' => 'chicken' } },
218+
'baz' => 'fuzz'
219+
}
220+
}
221+
@subject.send(:merge_enc_param, struct, 'parameters::foo::bar::baz', 'turkey')
222+
expect(struct['parameters']['foo']['bar']['baz']).to eq('turkey')
223+
end
224+
end
129225
end

0 commit comments

Comments
 (0)