Skip to content

Commit

Permalink
Fix for handling accessor sets on array elements.
Browse files Browse the repository at this point in the history
Previously: Filters like mutate would throw an exception when attempting
to convert an element within an array to a specific type. This was
raising an error because the index in the field reference was being
treated as a String instead of an integer representing the index of the
field to convert.

Now: Plugins like Mutate can access and convert elements within arrays
to specific types, or, updating their values.
  • Loading branch information
talevy committed Nov 13, 2014
1 parent 86037bb commit dd3667e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/logstash/util/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get(accessor)

def set(accessor, value)
target, key = lookup(accessor)
target[key] = value
target[target.is_a?(Array) ? key.to_i : key] = value
end

def strict_set(accessor, value)
Expand Down
8 changes: 8 additions & 0 deletions spec/util/accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@
insist { data } == { "hello" => { "world" => ["foo", "bar"] } }
end

it "should set element within array value" do
str = "[hello][0]"
data = {"hello" => ["foo", "bar"]}
accessors = LogStash::Util::Accessors.new(data)
insist { accessors.set(str, "world") } == "world"
insist { data } == {"hello" => ["world", "bar"]}
end

it "should retrieve array item" do
data = { "hello" => { "world" => ["a", "b"], "bar" => "baz" } }
accessors = LogStash::Util::Accessors.new(data)
Expand Down

0 comments on commit dd3667e

Please sign in to comment.