Sha256: 0cd50e0c75ed299e888106966a9c664c2f7d7d6900806081f74c1b961d7fa0f0

Contents?: true

Size: 1.83 KB

Versions: 19

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
require "logstash/namespace"
require "logstash/util"

module LogStash::Util::FieldReference

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

    code = "lambda do |store, &block|\n"
    selectors = accessor.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(store, #{tok.inspect}) unless block.nil?
        CODE
      end

      code << <<-"CODE"
        store = store.is_a?(Array) ? store[#{tok.to_i}] : store[#{tok.inspect}]
        return store if store.nil?
      CODE

    end
    code << "return store\nend"
    #puts code
    return code
  end # def compile

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

  def set(accessor, value, store)
    # The assignment can fail if the given field reference (accessor) does not exist
    # In this case, we'll want to set the value manually.
    if exec(accessor, store) { |hash, key| hash[key] = value }.nil?
      return (store[accessor] = value) if accessor[0,1] != "["

      # No existing element was found, so let's set one.
      *parents, key = accessor.scan(/(?<=\[)[^\]]+(?=\])/)
      parents.each do |p|
        if store.include?(p)
          store = store[p]
        else
          store[p] = {}
          store = store[p]
        end
      end
      store[key] = value
    end

    return value
  end

  extend self
end # module LogStash::Util::FieldReference

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
logstash-core-1.5.1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.1.snapshot1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.snapshot1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc4-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc4.snapshot2-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc4.snapshot1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot6-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot5-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot4-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot3-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot2-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc3.snapshot1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc2-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc2.snapshot-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc1.1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.rc1-java lib/logstash/util/fieldreference.rb
logstash-core-1.5.0.beta2-java lib/logstash/util/fieldreference.rb