Class: Fluent::Plugin::RedisEnrichmentFilter::PlaceholderExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/filter_redis_enrichment.rb

Overview

The expand recurse loop from record_transformer filter plugin

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



254
255
256
257
# File 'lib/fluent/plugin/filter_redis_enrichment.rb', line 254

def initialize(log)
  @log = log
  @cleanroom_expander = CleanroomExpander.new(log)
end

Instance Method Details

#expand(value, context = {}) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/fluent/plugin/filter_redis_enrichment.rb', line 259

def expand(value, context = {})
  new_value = nil
  case value
  when String
    num_placeholders = value.scan('${').size
    if num_placeholders == 1 && value.start_with?('${') && value.end_with?('}')
      new_value = value[2..-2] # ${..} => ..
    end
    new_value ||= "%Q[#{value.gsub('${', '#{')}]"
    new_value = @cleanroom_expander.expand(new_value, **context)
  when Hash
    new_value = {}
    value.each_pair do |k, v|
      new_value[expand(k, context)] = expand(v, context)
    end
  when Array
    new_value = []
    value.each_with_index do |v, i|
      new_value[i] = expand(v, context)
    end
  else
    new_value = value
  end

  new_value
end