Skip to content

Commit cf745f9

Browse files
committed
Support numbers in filter expressions
1 parent 8bf42a3 commit cf745f9

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

src/json_path/parser.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
(let [next (first remaining)]
3535
(cond
3636
(empty? remaining) []
37+
(re-matches #"\d+" next) [:val (Integer/parseInt next)]
38+
(re-matches #"\d+\.\d*" next) [:val (Double/parseDouble next)]
3739
(= "\"" next) [:val (apply str (extract-sub-tree "\"" "\"" remaining))]
3840
(= "[" next) (do
3941
(let [idx (parse-indexer (extract-sub-tree "[" "]" remaining))

test/Clojure_json-path.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ queries:
9797
result: "first"
9898
status: fail
9999
consensus: ["first", "second"]
100+
- id: filter_greater_than
101+
selector: $[?(@.key>42)]
102+
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}]
103+
status: error
104+
consensus: [{"key": 43}, {"key": 42.0001}, {"key": 100}]
105+
- id: filter_greater_than_or_equal
106+
selector: $[?(@.key>=42)]
107+
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"some": "value"}]
108+
status: error
109+
consensus: [{"key": 42}, {"key": 43}, {"key": 42.0001}, {"key": 100}]
110+
- id: filter_less_than
111+
selector: $[?(@.key<42)]
112+
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"some": "value"}]
113+
status: error
114+
consensus: [{"key": 0}, {"key": -1}, {"key": 41}, {"key": 41.9999}]
115+
- id: filter_less_than_on_string
116+
selector: $[?(@.key<42)]
117+
document: [{"key": 0}, {"key": "value"}]
118+
status: error
119+
- id: filter_less_than_or_equal
120+
selector: $[?(@.key<=42)]
121+
document: [{"key": 0}, {"key": 42}, {"key": -1}, {"key": 41}, {"key": 43}, {"key": 42.0001}, {"key": 41.9999}, {"key": 100}, {"some": "value"}]
122+
status: error
123+
- id: filter_with_value
124+
selector: $[?(@.key)]
125+
document: [{"some": "some value"}, {"key": "value"}]
126+
result: [{"key": "value"}]
127+
status: pass
100128
- id: key_bracket_notation
101129
selector: $['key']
102130
document: {"key": "value"}

test/json_path/test/json_path_test.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
(at-path "$[*]" {:foo 1 :bar [2 3]}) => [1 [2 3]]
1919
(at-path "$..*" {:foo 1 :bar [2 3]}) => [1 [2 3] 2 3]
2020
(at-path "$[-2]" [1 2 3]) => 2
21+
(at-path "$[?(@.bar<2)]" [{:bar 1} {:bar 2}]) => [{:bar 1}]
2122
(at-path "$.foo[?(@.bar=\"baz\")].hello"
2223
{:foo [{:bar "wrong" :hello "goodbye"}
2324
{:bar "baz" :hello "world"}]}) => ["world"]

test/json_path/test/parser_test.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
(parse-expr '("\"" "bar" "\"" "<" "\"" "bar" "\"")) => [:lt [:val "bar"] [:val "bar"]]
1818
(parse-expr '("\"" "bar" "\"" "<=" "\"" "bar" "\"")) => [:lt-eq [:val "bar"] [:val "bar"]]
1919
(parse-expr '("\"" "bar" "\"" ">" "\"" "bar" "\"")) => [:gt [:val "bar"] [:val "bar"]]
20-
(parse-expr '("\"" "bar" "\"" ">=" "\"" "bar" "\"")) => [:gt-eq [:val "bar"] [:val "bar"]])
20+
(parse-expr '("\"" "bar" "\"" ">=" "\"" "bar" "\"")) => [:gt-eq [:val "bar"] [:val "bar"]]
21+
(parse-expr '("\"" "bar" "\"" "=" "42")) => [:eq [:val "bar"] [:val 42]]
22+
(parse-expr '("\"" "bar" "\"" "=" "3.1415")) => [:eq [:val "bar"] [:val 3.1415]])
2123

2224
(fact
2325
(parse-indexer '("*")) => [:index "*"]
@@ -45,6 +47,12 @@
4547
[:selector [:filter [:some [:path [[:current]
4648
[:child]
4749
[:key "baz"]]]]]]]
50+
(parse-path "$[?(@.bar<2)]") => [:path [[:root]]
51+
[:selector [:filter [:lt
52+
[:path [[:current]
53+
[:child]
54+
[:key "bar"]]]
55+
[:val 2]]]]]
4856
(parse-path "$.foo[?(@.bar=\"baz\")].hello") => [:path [[:root] [:child] [:key "foo"]]
4957
[:selector [:filter [:eq [:path [[:current]
5058
[:child]

test/json_path/test/walker_test.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
(walk-selector [:filter [:eq [:path [[:current] [:child] [:key "bar"]]] [:val "baz"]]]
5151
{:current (m/root [{:bar "wrong"} {:bar "baz"}])}) => (list (m/create {:bar "baz"} [1]))
5252
(walk-selector [:filter [:eq [:path [[:current] [:child] [:key "bar"]]] [:val "baz"]]]
53-
{:current (m/root {:one {:bar "wrong"} :other {:bar "baz"}})}) => (list (m/create {:bar "baz"} [:other])))
53+
{:current (m/root {:one {:bar "wrong"} :other {:bar "baz"}})}) => (list (m/create {:bar "baz"} [:other]))
54+
(walk-selector [:filter [:gt [:path [[:current] [:child] [:key "bar"]]] [:val 42]]]
55+
{:current (m/root [{:bar 41} {:bar 43}])}) => (list (m/create {:bar 43} [1])))
5456

5557
(fact "selecting places constraints on the shape of the object being selected from"
5658
(walk-selector [:index "1"] {:current (m/root {:foo "bar"})}) => (throws Exception))

0 commit comments

Comments
 (0)