Module: LogStash::Util::FieldReference

Extended by:
FieldReference
Included in:
FieldReference
Defined in:
lib/logstash/util/fieldreference.rb

Instance Method Summary collapse

Instance Method Details

#compile(str) ⇒ Object



6
7
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
34
35
36
37
38
39
40
41
# File 'lib/logstash/util/fieldreference.rb', line 6

def compile(str)
  if str[0,1] != '['
    return <<-"CODE"
      lambda do |e, &block|
        return block.call(e, #{str.inspect}) unless block.nil?
        return e[#{str.inspect}]
      end
    CODE
  end

  code = "lambda do |e, &block|\n"
  selectors = str.scan(/(?<=\[).+?(?=\])/)
  selectors.each_with_index do |tok, i|
    last = (i == selectors.count() - 1)
    code << "   # [#{tok}]#{ last ? " (last selector)" : "" }\n"
   
    if last
      code << <<-"CODE"
        return block.call(e, #{tok.inspect}) unless block.nil?
      CODE
    end

    code << <<-"CODE"
      if e.is_a?(Array)
        e = e[#{tok.to_i}]
      else
        e = e[#{tok.inspect}]
      end
      return e if e.nil?
    CODE
    
  end
  code << "return e\nend"
  #puts code
  return code
end

#exec(str, obj, &block) ⇒ Object

def compile



43
44
45
46
47
# File 'lib/logstash/util/fieldreference.rb', line 43

def exec(str, obj, &block)
  @__fieldeval_cache ||= {}
  @__fieldeval_cache[str] ||= eval(compile(str))
  return @__fieldeval_cache[str].call(obj, &block)
end