Skip to content

Commit

Permalink
Merge pull request #2192 from okkez/fix-record-accessor
Browse files Browse the repository at this point in the history
Delete top level using bracket style properly
  • Loading branch information
repeatedly authored Nov 27, 2018
2 parents f73dcf9 + f70e743 commit 1982f2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/fluent/plugin_helper/record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/plugin_helper/test_record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']")
Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit 1982f2f

Please sign in to comment.