Skip to content

Commit

Permalink
Fix problem accessing array-indexed fields.
Browse files Browse the repository at this point in the history
This change fixes accessing fields of the form [list][0][key], where
[list] is an array, [0] is the numeric index into the array, and [key]
is the hash key into one of the items in the array.
  • Loading branch information
Van Eenwyk, Jonathan committed Apr 7, 2014
1 parent e1bd6a1 commit 82650a2
Show file tree
Hide file tree
Showing 2 changed files with 8 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 @@ -57,7 +57,7 @@ def lookup(accessor)

def store_path(accessor)
key, path = PathCache.get(accessor)
target = path.inject(@store) {|r, k| r[k] ||= {}}
target = path.inject(@store) {|r, k| r[r.is_a?(Array) ? k.to_i : k] ||= {}}
[target, key]
end

Expand Down
7 changes: 7 additions & 0 deletions spec/util/accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
insist { accessors.get("[hello][world][0]") } == data["hello"]["world"][0]
insist { accessors.get("[hello][world][1]") } == data["hello"]["world"][1]
end

it "should retrieve array item containing hash" do
data = { "hello" => { "world" => [ { "a" => 123 }, { "b" => 345 } ], "bar" => "baz" } }
accessors = LogStash::Util::Accessors.new(data)
insist { accessors.get("[hello][world][0][a]") } == data["hello"]["world"][0]["a"]
insist { accessors.get("[hello][world][1][b]") } == data["hello"]["world"][1]["b"]
end
end

context "using invalid encoding" do
Expand Down

0 comments on commit 82650a2

Please sign in to comment.