change cond-path/if-path to take a selector for the conditional (works like selected?)
This commit is contained in:
parent
2299bcc58b
commit
e4a3275ff1
3 changed files with 36 additions and 3 deletions
|
|
@ -192,7 +192,7 @@
|
|||
[& conds]
|
||||
(->> conds
|
||||
(partition 2)
|
||||
(map (fn [[c p]] [c (comp-paths* p)]))
|
||||
(map (fn [[c p]] [(comp-paths* c) (comp-paths* p)]))
|
||||
doall
|
||||
->ConditionalPath
|
||||
))
|
||||
|
|
@ -201,4 +201,4 @@
|
|||
"Like cond-path, but with if semantics."
|
||||
([cond-fn if-path] (cond-path cond-fn if-path))
|
||||
([cond-fn if-path else-path]
|
||||
(cond-path cond-fn if-path (fn [_] true) else-path)))
|
||||
(cond-path cond-fn if-path nil else-path)))
|
||||
|
|
|
|||
|
|
@ -189,6 +189,9 @@
|
|||
))))
|
||||
|
||||
(extend-protocol StructureValsPathComposer
|
||||
nil
|
||||
(comp-paths* [sp]
|
||||
(coerce-path sp))
|
||||
Object
|
||||
(comp-paths* [sp]
|
||||
(coerce-path sp))
|
||||
|
|
@ -486,7 +489,10 @@
|
|||
|
||||
(defn- retrieve-selector [cond-pairs structure]
|
||||
(->> cond-pairs
|
||||
(drop-while (fn [[c-fn _]] (not (c-fn structure))))
|
||||
(drop-while (fn [[c-selector _]]
|
||||
(->> structure
|
||||
(compiled-select* c-selector)
|
||||
empty?)))
|
||||
first
|
||||
second
|
||||
))
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
(:use [clojure.test]
|
||||
[clojure.test.check.clojure-test]
|
||||
[com.rpl specter]
|
||||
[com.rpl.specter protocols]
|
||||
[com.rpl.specter test-helpers])
|
||||
(:require [clojure.test.check
|
||||
[generators :as gen]
|
||||
|
|
@ -235,6 +236,9 @@
|
|||
(and (= [i] (select nil i))
|
||||
(= (afn i) (update nil afn i)))))
|
||||
|
||||
(deftest nil-comp-test
|
||||
(is (= [5] (select (comp-paths* nil) 5))))
|
||||
|
||||
(defspec putval-test
|
||||
(for-all+
|
||||
[kw gen/keyword
|
||||
|
|
@ -315,3 +319,26 @@
|
|||
*
|
||||
2)))
|
||||
)
|
||||
|
||||
(defspec cond-path-selector-test
|
||||
(for-all+
|
||||
[k1 (max-size 3 gen/keyword)
|
||||
k2 (max-size 3 gen/keyword)
|
||||
k3 (max-size 3 gen/keyword)
|
||||
m (max-size 5
|
||||
(gen-map-with-keys
|
||||
gen/keyword
|
||||
gen/int
|
||||
k1
|
||||
k2
|
||||
k3))
|
||||
pred (gen/elements [odd? even?])
|
||||
]
|
||||
(let [v1 (get m k1)
|
||||
k (if (pred v1) k2 k3)]
|
||||
(and
|
||||
(= (update (if-path [k1 pred] k2 k3) inc m)
|
||||
(update k inc m))
|
||||
(= (select (if-path [k1 pred] k2 k3) m)
|
||||
(select k m))
|
||||
))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue