Skip to content

Commit 09c91c9

Browse files
committed
Interpret symbols as strings in clause parser.
1 parent 4ab28b4 commit 09c91c9

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/active/clojure/match.clj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@
337337
:key-matches-with-binding ::key-matches-with-binding
338338
:path-matches ::path-matches
339339
:path-matches-with-binding ::path-matches-with-binding))
340-
(s/conform ::clause (list :k (list :compare-fn even?)))
341340

342341
(defn match-value->matcher
343342
[[kind match-value]]
@@ -347,6 +346,11 @@
347346
(= :compare-fn kind) (match-predicate (:fn match-value))
348347
:else (match-const match-value)))
349348

349+
(defn make-key
350+
"Returns k as a string if k is a symbol, otherwise returns k."
351+
[[kind k]]
352+
(if (= :symbol kind) (str k) k))
353+
350354
(defn parse-clause
351355
[p]
352356
(letfn [(optional? [k]
@@ -362,49 +366,49 @@
362366
(case match
363367
:key-exists
364368
(if (optional? mode)
365-
(opt (key-exists-clause (second (:key body))))
366-
(key-exists-clause (second body)))
369+
(opt (key-exists-clause (make-key (:key body))))
370+
(key-exists-clause (make-key body)))
367371

368372
:key-exists-with-binding
369-
(let [clause (-> (key-exists-clause (second (:key body)))
373+
(let [clause (-> (key-exists-clause (make-key (:key body)))
370374
(bind-match (:binding body)))]
371375
(if (optional? mode) (opt clause) clause))
372376

373377
:path-exists
374378
(if (optional? mode)
375-
(opt (path-exists-clause (mapv second (:path body))))
376-
(path-exists-clause (mapv second body)))
379+
(opt (path-exists-clause (mapv make-key (:path body))))
380+
(path-exists-clause (mapv make-key body)))
377381

378382
:path-exists-with-binding
379383
(let [{:keys [path binding]} body
380384

381-
components (mapv second path)
385+
components (mapv make-key path)
382386
clause (bind-match (path-exists-clause components) binding)]
383387
(if (optional? mode) (opt clause) clause))
384388

385389
:key-matches
386390
(let [{:keys [key match-value]} body
387391

388-
clause (key-matches-clause (second key) (match-value->matcher match-value))]
392+
clause (key-matches-clause (make-key key) (match-value->matcher match-value))]
389393
(if (optional? mode) (opt clause) clause))
390394

391395
:key-matches-with-binding
392396
(let [{:keys [key match-value binding]} body
393397

394-
clause (bind-match (key-matches-clause (second key) (match-value->matcher match-value)) binding)]
398+
clause (bind-match (key-matches-clause (make-key key) (match-value->matcher match-value)) binding)]
395399
(if (optional? mode) (opt clause) clause))
396400

397401
:path-matches
398402
(let [{:keys [path match-value]} body
399403

400-
components (mapv second path)
404+
components (mapv make-key path)
401405
clause (path-matches-clause components (match-value->matcher match-value))]
402406
(if (optional? mode) (opt clause) clause))
403407

404408
:path-matches-with-binding
405409
(let [{:keys [path match-value binding]} body
406410

407-
components (mapv second path)
411+
components (mapv make-key path)
408412
clause (bind-match (path-matches-clause components (match-value->matcher match-value)) binding)]
409413
(if (optional? mode) (opt clause) clause))))))))
410414

test/active/clojure/match_test.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
(p/parse-clause (list :k :as 'Binding)))))
1515

1616
(t/testing "path exists clause"
17-
(t/is (= (p/make-path-exists-clause [:k 'V] p/the-existence-matcher 'V)
17+
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'V)
1818
(p/parse-clause [:k 'V]))))
1919

2020
(t/testing "path exists clause with binding"
21-
(t/is (= (p/make-path-exists-clause [:k 'V] p/the-existence-matcher 'Binding)
21+
(t/is (= (p/make-path-exists-clause [:k "V"] p/the-existence-matcher 'Binding)
2222
(p/parse-clause (list [:k 'V] :as 'Binding)))))
2323

2424
(t/testing "key matches clause"
@@ -36,11 +36,11 @@
3636
(p/parse-clause (list :k "foo" :as 'Binding)))))
3737

3838
(t/testing "path matches clause"
39-
(t/is (= (p/make-path-matches-clause [:k 'bar 'baz] (p/make-constant-matcher "foo") 'baz)
39+
(t/is (= (p/make-path-matches-clause [:k "bar" "baz"] (p/make-constant-matcher "foo") 'baz)
4040
(p/parse-clause (list [:k 'bar 'baz] "foo")))))
4141

4242
(t/testing "path matches clause with binding"
43-
(t/is (= (p/make-path-matches-clause [:k 'bar 'baz] (p/make-constant-matcher "foo") 'Binding)
43+
(t/is (= (p/make-path-matches-clause [:k "bar" "baz"] (p/make-constant-matcher "foo") 'Binding)
4444
(p/parse-clause (list [:k 'bar 'baz] "foo" :as 'Binding)))))
4545

4646
(t/testing "with an options matcher"
@@ -58,7 +58,7 @@
5858
(p/parse-clause (list '? :k))))
5959
(t/is (= (p/make-optional-clause (p/make-key-exists-clause :k p/the-existence-matcher 'Binding))
6060
(p/parse-clause (list '? :k :as 'Binding))))
61-
(t/is (= (p/make-optional-clause (p/make-path-matches-clause [:k 'bar 'baz] (p/make-constant-matcher "foo") 'Binding))
61+
(t/is (= (p/make-optional-clause (p/make-path-matches-clause [:k "bar" "baz"] (p/make-constant-matcher "foo") 'Binding))
6262
(p/parse-clause (list '? [:k 'bar 'baz] "foo" :as 'Binding))))))
6363

6464
(t/deftest parse-pattern-test

0 commit comments

Comments
 (0)