Module: Fluent::Config::Expander

Defined in:
lib/fluent/plugin/expander.rb

Class Method Summary collapse

Class Method Details

.expand(element, mapping) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/expander.rb', line 8

def self.expand(element, mapping)
  name = replace(element.name, mapping)
  arg = replace(element.arg, mapping)
  attrs = element.reduce({}){|r,p| r[replace(p.first, mapping)] = replace(p.last, mapping); r}
  elements = []
  element.elements.each do |e|
    if e.name == 'for'
      unless e.arg =~ /^([a-zA-Z0-9]+) in (.+)$/
        raise Fluent::ConfigError, "invalid for tag syntax: <for NAME in ARG1 ARG2 ...>"
      end
      vkey = $1.dup
      vargs = $2.split(/ +/).select{|v| v.size > 0}

      vname = '__' + vkey + '__'
      vname2 = '${' + vkey + '}'
      vargs.each do |v|
        expanded = expand(e, mapping.merge({vname => v, vname2 => v}))
        attrs.update(expanded)
        elements += expanded.elements.map{|xe| expand(xe, mapping)}
      end
    else
      elements.push(expand(e, mapping))
    end
  end
  Fluent::Config::Element.new(name, arg, attrs, elements, [])
end

.replace(str, mapping) ⇒ Object



4
5
6
# File 'lib/fluent/plugin/expander.rb', line 4

def self.replace(str, mapping)
  mapping.reduce(str){|r,p| r.gsub(p[0], p[1])}
end