Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete top level using bracket style properly #2192

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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