Skip to content
Browse files

Adding multiple index expression support.

  • Loading branch information...
1 parent d619445 commit 33bac717efdb8203a3cfd7ee26bf2dab6988d6b0 @jcoglan committed
Showing with 97 additions and 6 deletions.
  1. +73 −1 lib/json_query.rb
  2. +5 −1 lib/json_query.tt
  3. +16 −4 lib/json_query/nodes.rb
  4. +3 −0 test/test_siren.rb
View
74 lib/json_query.rb
@@ -299,7 +299,7 @@ def _nt_field_access
s6 << r7
if r7
i8 = index
- r9 = _nt_expression
+ r9 = _nt_field_access_expression
if r9
r8 = r9
else
@@ -344,6 +344,78 @@ def _nt_field_access
return r0
end
+ module FieldAccessExpression0
+ def expression
+ elements[1]
+ end
+ end
+
+ module FieldAccessExpression1
+ def first
+ elements[0]
+ end
+
+ def others
+ elements[1]
+ end
+ end
+
+ def _nt_field_access_expression
+ start_index = index
+ if node_cache[:field_access_expression].has_key?(index)
+ cached = node_cache[:field_access_expression][index]
+ @index = cached.interval.end if cached
+ return cached
+ end
+
+ i0, s0 = index, []
+ r1 = _nt_expression
+ s0 << r1
+ if r1
+ s2, i2 = [], index
+ loop do
+ i3, s3 = index, []
+ if input.index(",", index) == index
+ r4 = (SyntaxNode).new(input, index...(index + 1))
+ @index += 1
+ else
+ terminal_parse_failure(",")
+ r4 = nil
+ end
+ s3 << r4
+ if r4
+ r5 = _nt_expression
+ s3 << r5
+ end
+ if s3.last
+ r3 = (SyntaxNode).new(input, i3...index, s3)
+ r3.extend(FieldAccessExpression0)
+ else
+ self.index = i3
+ r3 = nil
+ end
+ if r3
+ s2 << r3
+ else
+ break
+ end
+ end
+ r2 = SyntaxNode.new(input, i2...index, s2)
+ s0 << r2
+ end
+ if s0.last
+ r0 = (FieldAccessExpression).new(input, i0...index, s0)
+ r0.extend(FieldAccessExpression1)
+ else
+ self.index = i0
+ r0 = nil
+ end
+
+ node_cache[:field_access_expression][start_index] = r0
+
+ return r0
+ end
+
def _nt_all
start_index = index
if node_cache[:all].has_key?(index)
View
6 lib/json_query.tt
@@ -28,7 +28,11 @@ grammar JsonQuery
end
rule field_access
- ("." (symbol / all) / "[" (expression / all) "]") <FieldAccess>
+ ("." (symbol / all) / "[" (field_access_expression / all) "]") <FieldAccess>
+ end
+
+ rule field_access_expression
+ first:expression others:("," expression)* <FieldAccessExpression>
end
rule all
View
20 lib/json_query/nodes.rb
@@ -38,13 +38,18 @@ module Filter
module FieldAccess
def index(object, root, symbols, current = nil)
element = elements[1]
- return element.text_value if Symbol === element
+ return [element.text_value] if Symbol === element
element.value(root, symbols, object)
end
def value(object, root, symbols, current = nil)
- index = index(object, root, symbols, current)
-
+ indexes = index(object, root, symbols, current)
+ indexes.size == 1 ?
+ access(object, indexes.first) :
+ indexes.map { |i| access(object, i) }
+ end
+
+ def access(object, index)
return (Hash === object ? object.values : object) if index == :*
return object[index] if Array === object and Numeric === index
@@ -58,9 +63,16 @@ def value(object, root, symbols, current = nil)
end
end
+ class FieldAccessExpression < Treetop::Runtime::SyntaxNode
+ def value(root, symbols, current = nil)
+ exprs = [first] + others.elements.map { |e| e.expression }
+ exprs.map { |e| e.value(root, symbols, current) }
+ end
+ end
+
class AllFilter < Treetop::Runtime::SyntaxNode
def value(root, symbols, current = nil)
- :*
+ [:*]
end
end
View
3 test/test_siren.rb
@@ -96,6 +96,9 @@ def test_json_query
assert_equal "The Lord of the Rings",
Siren.query("$.store.book[@.length - 1]", fixtures(:store))["title"]
+
+ assert_equal ["Sayings of the Century", "The Lord of the Rings"],
+ Siren.query("$.store.book[0,3]", fixtures(:store)).map { |b| b["title"] }
end
end

0 comments on commit 33bac71

Please sign in to comment.
Something went wrong with that request. Please try again.