mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Always resolve procs when setting them into fields
This commit is contained in:
parent
5fc679a63a
commit
3ec21e7646
@ -88,8 +88,8 @@ module Rackstash
|
|||||||
|
|
||||||
# Note: You should never mutate an array or hash returned by normalize
|
# Note: You should never mutate an array or hash returned by normalize
|
||||||
# when `wrap` is `false`.
|
# when `wrap` is `false`.
|
||||||
def normalize(value, resolve: true, scope: nil, wrap: true)
|
def normalize(value, scope: nil, wrap: true)
|
||||||
value = resolve_value(value, scope: scope) if resolve
|
value = resolve_value(value, scope: scope)
|
||||||
|
|
||||||
case value
|
case value
|
||||||
when ::String, ::Symbol
|
when ::String, ::Symbol
|
||||||
@ -102,14 +102,14 @@ module Rackstash
|
|||||||
return wrap ? value : value.raw
|
return wrap ? value : value.raw
|
||||||
when ::Hash
|
when ::Hash
|
||||||
hash = value.each_with_object({}) do |(k, v), memo|
|
hash = value.each_with_object({}) do |(k, v), memo|
|
||||||
memo[utf8_encode(k)] = normalize(v, scope: scope, resolve: resolve)
|
memo[utf8_encode(k)] = normalize(v, scope: scope)
|
||||||
end
|
end
|
||||||
hash = Rackstash::Fields::Hash.new.tap do |hash_field|
|
hash = Rackstash::Fields::Hash.new.tap do |hash_field|
|
||||||
hash_field.raw = hash
|
hash_field.raw = hash
|
||||||
end if wrap
|
end if wrap
|
||||||
return hash
|
return hash
|
||||||
when ::Array, ::Set, ::Enumerator
|
when ::Array, ::Set, ::Enumerator
|
||||||
array = value.map { |e| normalize(e, scope: scope, resolve: resolve) }
|
array = value.map { |e| normalize(e, scope: scope) }
|
||||||
array = Rackstash::Fields::Array.new.tap do |array_field|
|
array = Rackstash::Fields::Array.new.tap do |array_field|
|
||||||
array_field.raw = array
|
array_field.raw = array
|
||||||
end if wrap
|
end if wrap
|
||||||
@ -127,7 +127,7 @@ module Rackstash
|
|||||||
exception << "\n" << value.backtrace.join("\n") if value.backtrace
|
exception << "\n" << value.backtrace.join("\n") if value.backtrace
|
||||||
return utf8_encode(exception).freeze
|
return utf8_encode(exception).freeze
|
||||||
when ::Proc
|
when ::Proc
|
||||||
return resolve ? utf8_encode(value.inspect).freeze : value
|
return utf8_encode(value.inspect).freeze
|
||||||
when ::BigDecimal
|
when ::BigDecimal
|
||||||
# A BigDecimal would be naturally represented as a JSON number. Most
|
# A BigDecimal would be naturally represented as a JSON number. Most
|
||||||
# libraries, however, parse non-integer JSON numbers directly as
|
# libraries, however, parse non-integer JSON numbers directly as
|
||||||
@ -152,7 +152,7 @@ module Rackstash
|
|||||||
next unless value.respond_to?(method)
|
next unless value.respond_to?(method)
|
||||||
value = value.public_send(method) rescue next
|
value = value.public_send(method) rescue next
|
||||||
|
|
||||||
return normalize(value, scope: scope, wrap: wrap, resolve: resolve)
|
return normalize(value, scope: scope, wrap: wrap)
|
||||||
end
|
end
|
||||||
|
|
||||||
utf8_encode(value.inspect).freeze
|
utf8_encode(value.inspect).freeze
|
||||||
|
|||||||
@ -439,12 +439,6 @@ describe Rackstash::Fields::AbstractCollection do
|
|||||||
|
|
||||||
expect(normalize(outer)).to match %r{\A#<Proc:0x[0-9a-f]+@#{__FILE__}:#{__LINE__-3}>\z}
|
expect(normalize(outer)).to match %r{\A#<Proc:0x[0-9a-f]+@#{__FILE__}:#{__LINE__-3}>\z}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the proc when not resolving' do
|
|
||||||
outer = proc { :return }
|
|
||||||
|
|
||||||
expect(normalize(outer, resolve: false)).to equal outer
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms Complex to String' do
|
it 'transforms Complex to String' do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user