Skip to content

Commit d1a3c8e

Browse files
committed
Allow path and key exists clauses to be wrapped in a list.
1 parent 09c91c9 commit d1a3c8e

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/active/clojure/match.clj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,17 @@
297297
(s/def ::qmark #{'?})
298298

299299
(s/def ::key-exists
300-
(s/or :required ::key
300+
(s/or :required (s/or :flat ::key
301+
:list (s/cat :key ::key))
301302
:optional (s/cat :qmark ::qmark :key ::key)))
302303

303304
(s/def ::key-exists-with-binding
304305
(s/or :required (s/cat :key ::key :binding-key ::binding-key :binding ::binding)
305306
:optional (s/cat :qmark ::qmark :key ::key :binding-key ::binding-key :binding ::binding)))
306307

307308
(s/def ::path-exists
308-
(s/or :required ::path
309+
(s/or :required (s/or :flat ::path
310+
:list (s/cat :path ::path))
309311
:optional (s/cat :qmark ::qmark :path ::path)))
310312

311313
(s/def ::path-exists-with-binding
@@ -351,6 +353,8 @@
351353
[[kind k]]
352354
(if (= :symbol kind) (str k) k))
353355

356+
(def flat? (partial = :flat))
357+
354358
(defn parse-clause
355359
[p]
356360
(letfn [(optional? [k]
@@ -367,7 +371,10 @@
367371
:key-exists
368372
(if (optional? mode)
369373
(opt (key-exists-clause (make-key (:key body))))
370-
(key-exists-clause (make-key body)))
374+
(let [[mode body] body]
375+
(if (flat? mode)
376+
(key-exists-clause (make-key body))
377+
(key-exists-clause (make-key (:key body))))))
371378

372379
:key-exists-with-binding
373380
(let [clause (-> (key-exists-clause (make-key (:key body)))
@@ -377,7 +384,10 @@
377384
:path-exists
378385
(if (optional? mode)
379386
(opt (path-exists-clause (mapv make-key (:path body))))
380-
(path-exists-clause (mapv make-key body)))
387+
(let [[mode body] body]
388+
(if (flat? mode)
389+
(path-exists-clause (mapv make-key body))
390+
(path-exists-clause (mapv make-key (:path body))))))
381391

382392
:path-exists-with-binding
383393
(let [{:keys [path binding]} body

test/active/clojure/match_test.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66

77
(t/deftest parse-clause-test
88
(t/testing "key exists clause"
9-
(t/is (= (p/make-key-exists-clause :k p/the-existence-matcher 'k)
10-
(p/parse-clause :k))))
9+
(t/testing "flat"
10+
(t/is (= (p/make-key-exists-clause :k p/the-existence-matcher 'k)
11+
(p/parse-clause :k))))
12+
(t/testing "list"
13+
(t/is (= (p/make-key-exists-clause :k p/the-existence-matcher 'k)
14+
(p/parse-clause (list :k))))))
1115

1216
(t/testing "key exists with binding clause"
1317
(t/is (= (p/make-key-exists-clause :k p/the-existence-matcher 'Binding)
1418
(p/parse-clause (list :k :as 'Binding)))))
1519

1620
(t/testing "path exists clause"
17-
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'V)
18-
(p/parse-clause [:k 'V]))))
21+
(t/testing "flat"
22+
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'V)
23+
(p/parse-clause [:k 'V]))))
24+
(t/testing "list"
25+
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'V)
26+
(p/parse-clause (list [:k 'V]))))))
1927

2028
(t/testing "path exists clause with binding"
2129
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'Binding)

0 commit comments

Comments
 (0)