Add select-view path selector + test case
This commit is contained in:
parent
e1c63e51d3
commit
96f6bbc9f4
2 changed files with 48 additions and 0 deletions
|
|
@ -256,6 +256,23 @@
|
||||||
ancestry))
|
ancestry))
|
||||||
)))
|
)))
|
||||||
|
|
||||||
|
(defn select-view
|
||||||
|
"Navigates to a sequence that contains the results of (select ...),
|
||||||
|
but is a view to the original structure that can be transformed."
|
||||||
|
[& path]
|
||||||
|
(fixed-pathed-path [late path]
|
||||||
|
(select* [this structure next-fn]
|
||||||
|
(next-fn (compiled-select late structure)))
|
||||||
|
(transform* [this structure next-fn]
|
||||||
|
(let [select-result (compiled-select late structure)
|
||||||
|
transformed (next-fn select-result)
|
||||||
|
values-to-insert (atom transformed)]
|
||||||
|
(compiled-transform late
|
||||||
|
(fn [_] (let [next-val (first @values-to-insert)]
|
||||||
|
(swap! values-to-insert rest)
|
||||||
|
next-val))
|
||||||
|
structure)))))
|
||||||
|
|
||||||
(defpath keypath [key]
|
(defpath keypath [key]
|
||||||
(select* [this structure next-fn]
|
(select* [this structure next-fn]
|
||||||
(next-fn (get structure key)))
|
(next-fn (get structure key)))
|
||||||
|
|
|
||||||
|
|
@ -510,6 +510,37 @@
|
||||||
))
|
))
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(defspec select-view-nested-vectors
|
||||||
|
(for-all+
|
||||||
|
[v1 (gen/vector
|
||||||
|
(gen/vector gen/int))]
|
||||||
|
(let [path (s/comp-paths (s/select-view s/ALL s/ALL))
|
||||||
|
v2 (s/compiled-transform path reverse v1)]
|
||||||
|
(and
|
||||||
|
(= (s/compiled-select path v1) [(flatten v1)])
|
||||||
|
(= (flatten v1) (reverse (flatten v2)))
|
||||||
|
(= (map count v1) (map count v2))))))
|
||||||
|
|
||||||
|
(defspec select-view-param-test
|
||||||
|
(for-all+
|
||||||
|
[k gen/keyword
|
||||||
|
v (gen/vector
|
||||||
|
(limit-size 5
|
||||||
|
(gen-map-with-keys
|
||||||
|
gen/keyword
|
||||||
|
gen/int
|
||||||
|
k)))]
|
||||||
|
(and
|
||||||
|
(= (s/compiled-select ((s/select-view s/ALL s/keypath) k) v)
|
||||||
|
[(map k v)])
|
||||||
|
(let [v2 (s/compiled-transform ((s/comp-paths (s/select-view s/ALL s/keypath)) k)
|
||||||
|
reverse
|
||||||
|
v)]
|
||||||
|
(and (= (map k v) (reverse (map k v2)))
|
||||||
|
(= (map #(dissoc % k) v)
|
||||||
|
(map #(dissoc % k) v2))) ; only key k was touched in any of the maps
|
||||||
|
))))
|
||||||
|
|
||||||
(defspec param-multi-path-test
|
(defspec param-multi-path-test
|
||||||
(for-all+
|
(for-all+
|
||||||
[k1 gen/keyword
|
[k1 gen/keyword
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue