Sha256: c3aa4859bdbeda40c93b2eb6ffa52847e4ae5e7f28a5961d1b0f008e40f0b0f1

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'fluent/config'

module Fluent::Config::Expander
  def self.replace(str, mapping)
    mapping.reduce(str){|r,p| str.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
        vname = '__' + $1 + '__'
        vargs = $2.split(/ +/).select{|v| v.size > 0}
        vargs.each do |v|
          expanded = expand(e, mapping.merge({vname => 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

2 entries across 2 versions & 1 rubygems

Version Path
fluent-plugin-config-expander-0.1.0 lib/fluent/plugin/expander.rb
fluent-plugin-config-expander-0.0.1 lib/fluent/plugin/expander.rb