Class: LogStash::Config::AST::Plugin

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/logstash/config/config_ast.rb', line 268

def compile
  case plugin_type
  when "input"
    return "start_input(@generated_objects[:#{variable_name}])"
  when "filter"
    return <<-CODE
      events = @generated_objects[:#{variable_name}].multi_filter(events)
    CODE
  when "output"
    return "targeted_outputs << @generated_objects[:#{variable_name}]\n"
  when "codec"
    settings = attributes.recursive_select(Attribute).collect(&:compile).reject(&:empty?)
    attributes_code = "LogStash::Util.hash_merge_many(#{settings.map { |c| "{ #{c} }" }.join(", ")})"
    return "plugin(#{plugin_type.inspect}, #{plugin_name.inspect}, #{attributes_code})"
  end
end

#compile_initializerObject



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/logstash/config/config_ast.rb', line 255

def compile_initializer
  # If any parent is a Plugin, this must be a codec.

  if attributes.elements.nil?
    return "plugin(#{plugin_type.inspect}, #{plugin_name.inspect})" << (plugin_type == "codec" ? "" : "\n")
  else
    settings = attributes.recursive_select(Attribute).collect(&:compile).reject(&:empty?)

    attributes_code = "LogStash::Util.hash_merge_many(#{settings.map { |c| "{ #{c} }" }.join(", ")})"
    return "plugin(#{plugin_type.inspect}, #{plugin_name.inspect}, #{attributes_code})" << (plugin_type == "codec" ? "" : "\n")
  end
end

#compile_starting_hereObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/logstash/config/config_ast.rb', line 285

def compile_starting_here
  return unless plugin_type == "filter" # only filter supported.

  expressions = [
    LogStash::Config::AST::Branch,
    LogStash::Config::AST::Plugin
  ]
  code = []

  # Find the branch we are in, if any (the 'if' statement, etc)
  self_branch = recursive_select_parent(LogStash::Config::AST::BranchEntry).first

  # Find any siblings to our branch so we can skip them later.  For example,
  # if we are in an 'else if' we want to skip any sibling 'else if' or
  # 'else' blocks.
  branch_siblings = []
  if self_branch
    branch_siblings = recursive_select_parent(LogStash::Config::AST::Branch).first \
      .recursive_select(LogStash::Config::AST::BranchEntry) \
      .reject { |b| b == self_branch }
  end

  #ast = recursive_select_parent(LogStash::Config::AST::PluginSection).first
  ast = recursive_select_parent(LogStash::Config::AST::Config).first

  found = false
  recurse(ast) do |element, depth|
    next false if element.is_a?(LogStash::Config::AST::PluginSection) && element.plugin_type.text_value != "filter"
    if element == self
      found = true
      next false
    end
    if found && expressions.include?(element.class)
      code << element.compile
      next false
    end
    next false if branch_siblings.include?(element)
    next true
  end

  return code.collect { |l| "#{l}\n" }.join("")
end

#plugin_nameObject



247
248
249
# File 'lib/logstash/config/config_ast.rb', line 247

def plugin_name
  return name.text_value
end

#plugin_typeObject



239
240
241
242
243
244
245
# File 'lib/logstash/config/config_ast.rb', line 239

def plugin_type
  if recursive_select_parent(Plugin).any?
    return "codec"
  else
    return recursive_select_parent(PluginSection).first.plugin_type.text_value
  end
end

#variable_nameObject



251
252
253
# File 'lib/logstash/config/config_ast.rb', line 251

def variable_name
  return recursive_select_parent(PluginSection).first.variable(self)
end