diff --git a/lib/fluent/plugin_helper/record_accessor.rb b/lib/fluent/plugin_helper/record_accessor.rb index 59062379bd..b34da5e3f8 100644 --- a/lib/fluent/plugin_helper/record_accessor.rb +++ b/lib/fluent/plugin_helper/record_accessor.rb @@ -40,8 +40,14 @@ def initialize(param) if @keys.is_a?(Array) @last_key = @keys.last @dig_keys = @keys[0..-2] - mcall = method(:call_dig) - mdelete = method(:delete_nest) + if @dig_keys.empty? + @keys = @keys.first + mcall = method(:call_index) + mdelete = method(:delete_top) + else + mcall = method(:call_dig) + mdelete = method(:delete_nest) + end else # Call [] for single key to reduce dig overhead mcall = method(:call_index) diff --git a/test/plugin_helper/test_record_accessor.rb b/test/plugin_helper/test_record_accessor.rb index f3e052bbcf..5d942ed77f 100644 --- a/test/plugin_helper/test_record_accessor.rb +++ b/test/plugin_helper/test_record_accessor.rb @@ -110,6 +110,12 @@ class Dummy < Fluent::Plugin::TestBase assert_equal r[param], accessor.call(r) end + test "access single dot key using bracket style" do + r = {'key1' => 'v1', 'ke y2' => 'v2', 'this.is.key3' => 'v3'} + accessor = @d.record_accessor_create('$["this.is.key3"]') + assert_equal 'v3', accessor.call(r) + end + test "nested bracket keys with dot" do r = {'key1' => {'this.is.key3' => 'value'}} accessor = @d.record_accessor_create("$['key1']['this.is.key3']") @@ -164,6 +170,13 @@ class Dummy < Fluent::Plugin::TestBase assert_not_include(r, param) end + test "delete top key using bracket style" do + r = {'key1' => 'v1', 'ke y2' => 'v2', 'this.is.key3' => 'v3'} + accessor = @d.record_accessor_create('$["this.is.key3"]') + accessor.delete(r) + assert_not_include(r, 'this.is.key3') + end + data('bracket' => "$['key1'][0]['ke y2']", 'bracket w/ double quotes' => '$["key1"][0]["ke y2"]') test "delete nested keys ['key1', 0, 'ke y2']" do |param|