add Event method_missing to give better error messages for #[] and #[]=

Change strings and regexs to CONSTANTS.

move method missing code to event.rb

doh - put Event class in already defined Logstash module

Fixes #6045
This commit is contained in:
Guy Boertje 2016-10-13 09:34:14 +01:00
parent 37243d1602
commit 8cec4fa514
3 changed files with 37 additions and 4 deletions

View file

@ -25,4 +25,4 @@ end
require "jruby_event_ext"
require "jruby_timestamp_ext"
require "logstash/event"
require "logstash/timestamp"
require "logstash/timestamp"

View file

@ -29,9 +29,26 @@ module LogStash
# LogStash::SHUTDOWN is used by plugins
SHUTDOWN = ShutdownEvent.new
class Event
MSG_BRACKETS_METHOD_MISSING = "Direct event field references (i.e. event['field']) have been disabled in favor of using event get and set methods (e.g. event.get('field')). Please consult the Logstash 5.0 breaking changes documentation for more details.".freeze
MSG_BRACKETS_EQUALS_METHOD_MISSING = "Direct event field references (i.e. event['field'] = 'value') have been disabled in favor of using event get and set methods (e.g. event.set('field', 'value')). Please consult the Logstash 5.0 breaking changes documentation for more details.".freeze
RE_BRACKETS_METHOD = /^\[\]$/.freeze
RE_BRACKETS_EQUALS_METHOD = /^\[\]=$/.freeze
def method_missing(method_name, *arguments, &block)
if RE_BRACKETS_METHOD.match(method_name.to_s)
raise NoMethodError.new(MSG_BRACKETS_METHOD_MISSING)
end
if RE_BRACKETS_EQUALS_METHOD.match(method_name.to_s)
raise NoMethodError.new(MSG_BRACKETS_EQUALS_METHOD_MISSING)
end
super
end
end
end
# for backward compatibility, require "logstash/event" is used a lots of places so let's bootstrap the
# Java code loading from here.
# TODO: (colin) I think we should mass replace require "logstash/event" with require "logstash-core-event"
require "logstash-core-event"
require "logstash-core-event"