Class: LogStash::Config::AST::Branch

Inherits:
Node
  • Object
show all
Defined in:
lib/logstash/config/config_ast.rb

Instance Method Summary collapse

Methods inherited from Node

#text_value_for_comments

Instance Method Details

#compileObject



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/logstash/config/config_ast.rb', line 418

def compile

  # this construct is non obvious. we need to loop through each event and apply the conditional.
  # each branch of a conditional will contain a construct (a filter for example) that also loops through
  # the events variable so we have to initialize it to [event] for the branch code.
  # at the end, events is returned to handle the case where no branch match and no branch code is executed
  # so we must make sure to return the current event.

  type = recursive_select_parent(PluginSection).first.plugin_type.text_value

  if type == "filter"
    i = LogStash::Config::AST.deferred_conditionals_index += 1
    source = <<-CODE
      @generated_objects[:cond_func_#{i}] = lambda do |input_events|
        result = []
        input_events.each do |event|
          events = [event]
          #{super}
          end
          result += events
        end
        result
      end
    CODE
    LogStash::Config::AST.deferred_conditionals << source

    <<-CODE
      events = @generated_objects[:cond_func_#{i}].call(events)
    CODE
  else # Output
    <<-CODE
      #{super}
      end
    CODE
  end
end