Class: Fluent::RecordReformerOutputCore::PlaceholderExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_record_reformer/core.rb

Overview

THIS CLASS MUST BE THREAD-SAFE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



190
191
192
193
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 190

def initialize(params)
  @log = params[:log]
  @auto_typecast = params[:auto_typecast]
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



188
189
190
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 188

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



188
189
190
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 188

def placeholders
  @placeholders
end

Instance Method Details

#expand(str, placeholders, force_stringify = false) ⇒ Object

Expand string with placeholders

Parameters:

  • str (String)
  • force_stringify (Boolean) (defaults to: false)

    the value must be string, used for hash key



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 232

def expand(str, placeholders, force_stringify = false)
  if @auto_typecast and !force_stringify
    single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
    if single_placeholder_matched
      log_if_unknown_placeholder($1, placeholders)
      return placeholders[single_placeholder_matched[1]]
    end
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log_if_unknown_placeholder($1, placeholders)
    placeholders[$1]
  }
end

#prepare_placeholders(placeholder_values) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 203

def prepare_placeholders(placeholder_values)
  placeholders = {}

  placeholder_values.each do |key, value|
    if value.kind_of?(Array) # tag_parts, etc
      size = value.size
      value.each_with_index do |v, idx|
        placeholders.store("${#{key}[#{idx}]}", v)
        placeholders.store("${#{key}[#{idx-size}]}", v) # support [-1]
      end
    elsif value.kind_of?(Hash) # record, etc
      value.each do |k, v|
        unless placeholder_values.has_key?(k) # prevent overwriting the reserved keys such as tag
          placeholders.store("${#{k}}", v)
        end
        placeholders.store(%Q[${#{key}["#{k}"]}], v) # record["foo"]
      end
    else # string, interger, float, and others?
      placeholders.store("${#{key}}", value)
    end
  end

  placeholders
end

#preprocess_map(value, force_stringify = false) ⇒ Object



199
200
201
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 199

def preprocess_map(value, force_stringify = false)
  value
end

#time_value(time) ⇒ Object



195
196
197
# File 'lib/fluent/plugin/out_record_reformer/core.rb', line 195

def time_value(time)
  Time.at(time).to_s
end