mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Add force parameter to Rackstash::Fields::Hash#set so it works like #merge!
This commit is contained in:
parent
9fc9ea248b
commit
67955f8629
@ -451,6 +451,10 @@ module Rackstash
|
|||||||
#
|
#
|
||||||
# @param key [#to_s] the field name. When setting the field, this name
|
# @param key [#to_s] the field name. When setting the field, this name
|
||||||
# will be normalized as a frozen UTF-8 string.
|
# will be normalized as a frozen UTF-8 string.
|
||||||
|
# @param force [Boolean] if `true`, we overwrite existing values for
|
||||||
|
# conflicting keys but raise an `ArgumentError` when trying to set a
|
||||||
|
# forbidden key. If `false`, we silently ignore values for existing or
|
||||||
|
# forbidden keys.
|
||||||
#
|
#
|
||||||
# @yield [key] if the key doesn't exist yet, we call the block and use its
|
# @yield [key] if the key doesn't exist yet, we call the block and use its
|
||||||
# return value as the value to insert at `key`
|
# return value as the value to insert at `key`
|
||||||
@ -462,11 +466,15 @@ module Rackstash
|
|||||||
# @return [Object, nil] The return value of the block or `nil` if no
|
# @return [Object, nil] The return value of the block or `nil` if no
|
||||||
# insertion happened. Note that `nil` is also a valid value to insert
|
# insertion happened. Note that `nil` is also a valid value to insert
|
||||||
# into the hash.
|
# into the hash.
|
||||||
def set(key)
|
def set(key, force: true)
|
||||||
key = utf8_encode(key)
|
key = utf8_encode(key)
|
||||||
|
|
||||||
return if forbidden_key?(key)
|
if force
|
||||||
return unless @raw[key].nil?
|
raise ArgumentError, "Forbidden field #{key}" if forbidden_key?(key)
|
||||||
|
else
|
||||||
|
return if forbidden_key?(key)
|
||||||
|
return unless @raw[key].nil?
|
||||||
|
end
|
||||||
|
|
||||||
@raw[key] = normalize(yield(key))
|
@raw[key] = normalize(yield(key))
|
||||||
end
|
end
|
||||||
|
|||||||
@ -92,10 +92,10 @@ describe Rackstash::Fields::Hash do
|
|||||||
expect(hash['key']).to eql 'value'
|
expect(hash['key']).to eql 'value'
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with forbidden fields' do
|
context 'with forbidden_keys' do
|
||||||
let(:forbidden_keys) { ['forbidden', :foo, 42] }
|
let(:forbidden_keys) { ['forbidden', :foo, 42] }
|
||||||
|
|
||||||
it 'denies setting a forbidden field' do
|
it 'denies setting a forbidden key' do
|
||||||
expect { hash[:forbidden] = 'value' }.to raise_error ArgumentError
|
expect { hash[:forbidden] = 'value' }.to raise_error ArgumentError
|
||||||
expect { hash['forbidden'] = 'value' }.to raise_error ArgumentError
|
expect { hash['forbidden'] = 'value' }.to raise_error ArgumentError
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ describe Rackstash::Fields::Hash do
|
|||||||
expect { hash[:'42'] = 'value' }.to raise_error ArgumentError
|
expect { hash[:'42'] = 'value' }.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns nil when accessing forbidden fields' do
|
it 'returns nil when accessing forbidden keys' do
|
||||||
expect(hash['forbidden']).to be_nil
|
expect(hash['forbidden']).to be_nil
|
||||||
|
|
||||||
expect(hash[:foo]).to be_nil
|
expect(hash[:foo]).to be_nil
|
||||||
@ -683,27 +683,50 @@ describe Rackstash::Fields::Hash do
|
|||||||
expect(hash['symbol']).to eql 'value'
|
expect(hash['symbol']).to eql 'value'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'ignores forbidden keys' do
|
context 'with force: false' do
|
||||||
forbidden_keys << 'forbidden'
|
it 'ignores forbidden keys' do
|
||||||
|
forbidden_keys << 'forbidden'
|
||||||
|
|
||||||
expect { |b| hash.set(:forbidden, &b) }.not_to yield_control
|
expect { |b| hash.set(:forbidden, force: false, &b) }.not_to yield_control
|
||||||
expect { |b| hash.set('forbidden', &b) }.not_to yield_control
|
expect { |b| hash.set('forbidden', force: false, &b) }.not_to yield_control
|
||||||
|
|
||||||
expect(hash['forbidden']).to be_nil
|
expect(hash['forbidden']).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'ignores existing keys' do
|
||||||
|
hash['key'] = 'value'
|
||||||
|
|
||||||
|
expect { |b| hash.set(:key, force: false, &b) }.not_to yield_control
|
||||||
|
expect { |b| hash.set('key', force: false, &b) }.not_to yield_control
|
||||||
|
|
||||||
|
expect(hash['key']).to eql 'value'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'overwrites nil value' do
|
||||||
|
hash['nil'] = nil
|
||||||
|
expect { |b| hash.set('nil', force: false, &b) }.to yield_control
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'ignores existing keys' do
|
context 'with force: true' do
|
||||||
hash['key'] = 'value'
|
it 'denies setting a forbidden key' do
|
||||||
|
forbidden_keys << 'forbidden'
|
||||||
|
|
||||||
expect { |b| hash.set(:key, &b) }.not_to yield_control
|
expect { hash.set(:forbidden, force: true) { 'value' } }.to raise_error ArgumentError
|
||||||
expect { |b| hash.set('key', &b) }.not_to yield_control
|
expect { hash.set('forbidden', force: true) { 'value' } }.to raise_error ArgumentError
|
||||||
|
|
||||||
expect(hash['key']).to eql 'value'
|
expect(hash['forbidden']).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'overwrites nil value' do
|
it 'overwrites existing keys' do
|
||||||
hash['nil'] = nil
|
hash['key'] = 'value'
|
||||||
expect { |b| hash.set('nil', &b) }.to yield_control
|
|
||||||
|
expect(hash.set(:key, force: true) { 'new_symbol' }).to eql 'new_symbol'
|
||||||
|
expect(hash['key']).to eql 'new_symbol'
|
||||||
|
|
||||||
|
expect(hash.set('key', force: true) { 'new_string' }).to eql 'new_string'
|
||||||
|
expect(hash['key']).to eql 'new_string'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user