Skip to content

Commit

Permalink
Bug-fix for Struct member field access.
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorrowe committed Oct 28, 2014
1 parent 8fc51f5 commit bb186f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Unreleased Changes
------------------

* Bug-fix, when accessing Struct objects with an invalid member
`nil` is now returned, instead of raising an error.

1.0.0 (2014-10-28)
------------------

Expand Down
3 changes: 2 additions & 1 deletion lib/jmespath/tree_interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def dispatch(node, value)
case node[:type]

when :field
# hash_like?
key = node[:key]
case value
when Hash then value.key?(key) ? value[key] : value[key.to_sym]
when Struct then value[key]
when Struct then value.respond_to?(key) ? value[key] : nil
else nil
end

Expand Down
9 changes: 9 additions & 0 deletions spec/indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ module JMESPath
expect(JMESPath.search('foo.bar.yuck', data)).to eq('abc')
end

it 'does not raise an error when accessing non-valid struct members' do
data = Struct.new(:foo).new(
Struct.new(:bar).new(
Struct.new(:yuck).new('abc')
)
)
expect(JMESPath.search('foo.baz.yuck', data)).to be(nil)
end

end
end
end

0 comments on commit bb186f0

Please sign in to comment.