Sha256: 4bf9b401b7ac6ba5d41d47331280d81483f459013e0f5d6479281aa232e47f0d

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

require 'fluent/config'

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

  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fluent-plugin-config-expander-0.2.1 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.2.0 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.1.5 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.1.4 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.1.3 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.1.2 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.1.1 lib/fluent/plugin/expander.rb