mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Support arbitrarily nested Procs when setting/merging fields
This commit is contained in:
parent
dd7f79db0a
commit
9f2a330a6c
@ -104,6 +104,8 @@ module Rackstash
|
||||
def resolve_value(value, scope: nil)
|
||||
return value unless value.is_a?(Proc)
|
||||
scope == nil ? value.call : scope.instance_exec(&value)
|
||||
rescue
|
||||
value.inspect
|
||||
end
|
||||
|
||||
# Note: You should never mutate an array or hash returned by normalize
|
||||
@ -157,7 +159,7 @@ module Rackstash
|
||||
exception << "\n" << value.backtrace.join("\n") if value.backtrace
|
||||
return utf8_encode(exception)
|
||||
when ::Proc
|
||||
return utf8_encode(value.inspect)
|
||||
return normalize(value, scope: scope, wrap: wrap)
|
||||
when ::BigDecimal
|
||||
# A BigDecimal would be naturally represented as a JSON number. Most
|
||||
# libraries, however, parse non-integer JSON numbers directly as
|
||||
|
||||
@ -67,6 +67,8 @@ module Rackstash
|
||||
value = value.map { |tag| normalize_tags(tag, scope: scope) }
|
||||
value.flatten!
|
||||
value
|
||||
elsif value.is_a?(Proc)
|
||||
normalize_tags(value, scope: scope)
|
||||
elsif value.respond_to?(:to_ary)
|
||||
value = value.to_ary.map { |tag| normalize_tags(tag, scope: scope) }
|
||||
value.flatten!
|
||||
|
||||
@ -489,18 +489,29 @@ describe Rackstash::Fields::AbstractCollection do
|
||||
|
||||
describe 'with Proc' do
|
||||
it 'calls the proc by default and normalizes the result' do
|
||||
proc = proc { :return }
|
||||
proc = -> { :return }
|
||||
|
||||
expect(normalize(proc)).to eql 'return'
|
||||
expect(normalize(proc).encoding).to eql Encoding::UTF_8
|
||||
expect(normalize(proc)).to be_frozen
|
||||
end
|
||||
|
||||
it 'inspects a nested proc' do
|
||||
inner = proc { :return }
|
||||
outer = proc { inner }
|
||||
it 'calls a nested proc and normalizes the result' do
|
||||
inner = -> { :return }
|
||||
outer = -> { inner }
|
||||
|
||||
expect(normalize(outer)).to match %r{\A#<Proc:0x[0-9a-f]+@#{__FILE__}:#{__LINE__ - 3}>\z}
|
||||
expect(normalize(outer)).to eql 'return'
|
||||
end
|
||||
|
||||
it 'returns the inspected proc on errors' do
|
||||
error = -> { raise 'Oh, no!' }
|
||||
expected_arguments = ->(arg1, args, arg3) { 'cherio' }
|
||||
ok = -> { :ok }
|
||||
outer = -> { [ok, error, expected_arguments] }
|
||||
|
||||
expect(normalize(outer))
|
||||
.to be_a(Rackstash::Fields::Array)
|
||||
.and contain_exactly('ok', error.inspect, expected_arguments.inspect)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user