From f70e743a94d12f04832e9a1cfda9a0f0b965d5a9 Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Tue, 27 Nov 2018 15:56:45 +0900 Subject: [PATCH] Delete top level using bracket style properly In previous version, following configuration does not work properly: ``` @type dummy dummy [ {"foo.bar": "test1234", "message": "Hello"} ] tag dummy @type record_transformer remove_keys "$['foo.bar']" @type stdout ``` This shows like following: ``` 2018-11-27 15:19:18 +0900 [info]: #0 fluentd worker is now running worker=0 2018-11-27 15:19:19.008586045 +0900 dummy: {"foo.bar":"test1234","message":"Hello"} 2018-11-27 15:19:20.009721132 +0900 dummy: {"foo.bar":"test1234","message":"Hello"} 2018-11-27 15:19:21.010784035 +0900 dummy: {"foo.bar":"test1234","message":"Hello"} ``` In this version, it works well. See also #2109 Signed-off-by: Kenji Okimoto --- lib/fluent/plugin_helper/record_accessor.rb | 10 ++++++++-- test/plugin_helper/test_record_accessor.rb | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) 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|