- Sort config names so regexps end up handled last. This ensures literal

values (config :foo) are processed before pattern ones (config /.*/)
This commit is contained in:
Jordan Sissel 2011-02-26 22:30:01 -08:00
parent 5c5433b9e5
commit 5399ea9770

View file

@ -24,6 +24,12 @@ require "logstash/logging"
module LogStash::Config::Mixin module LogStash::Config::Mixin
attr_accessor :config attr_accessor :config
CONFIGSORT = {
Symbol => 0,
String => 0,
Regexp => 100,
}
# This method is called when someone does 'include LogStash::Config' # This method is called when someone does 'include LogStash::Config'
def self.included(base) def self.included(base)
#puts "Configurable class #{base.name}" #puts "Configurable class #{base.name}"
@ -64,6 +70,7 @@ module LogStash::Config::Mixin
def config(name, opts={}) def config(name, opts={})
@config ||= Hash.new @config ||= Hash.new
@required ||= Array.new @required ||= Array.new
# TODO(sissel): verify 'name' is of type String, Symbol, or Regexp
name = name.to_s if name.is_a?(Symbol) name = name.to_s if name.is_a?(Symbol)
@config[name] = opts[:validate] # ok if this is nil @config[name] = opts[:validate] # ok if this is nil
@ -142,20 +149,34 @@ module LogStash::Config::Mixin
# config /foo.*/ => ... # config /foo.*/ => ...
is_valid = true is_valid = true
params.each do |key, value| # string/symbols are first, then regexes.
@config.find do |config_key, config_val| config_keys = @config.keys.sort do |a,b|
if (config_key.is_a?(Regexp) && key =~ config_key) \ CONFIGSORT[a.class] <=> CONFIGSORT[b.class]
|| (config_key.is_a?(String) && key == config_key) end
success, result = validate_value(value, config_val) #puts "Key order: #{config_keys.inspect}"
if success #puts @config.keys.inspect
params[key] = result if !result.nil?
else
@logger.error("Failed config #{@plugin_name}/#{key}: #{result} (#{value.inspect})")
end
#puts "Result: #{key} / #{result.inspect} / #{success}" params.each do |key, value|
is_valid &&= success config_keys.each do |config_key|
#puts
#puts "Candidate: #{key.inspect} / #{value.inspect}"
#puts "Config: #{config_key} / #{config_val} "
next unless (config_key.is_a?(Regexp) && key =~ config_key) \
|| (config_key.is_a?(String) && key == config_key)
config_val = @config[config_key]
#puts " Key matches."
success, result = validate_value(value, config_val)
if success
# Accept coerced value if success
# Used for converting values in the config to proper objects.
params[key] = result if !result.nil?
else
@logger.error("Failed config #{@plugin_name}/#{key}: #{result} (#{value.inspect})")
end end
#puts "Result: #{key} / #{result.inspect} / #{success}"
is_valid &&= success
break # done with this param key
end # config.each end # config.each
end # params.each end # params.each