Merge pull request #1216 from jdve/fix-array-accessor

Fix problem accessing array-indexed fields.
This commit is contained in:
Colin Surprenant 2014-04-07 18:50:09 -04:00
commit 6e1e799aae
2 changed files with 8 additions and 1 deletions

View file

@ -57,7 +57,7 @@ module LogStash::Util
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

View file

@ -146,6 +146,13 @@ describe LogStash::Util::Accessors, :if => true do
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