-
-
Save jlongster/1c661626a0860022ef26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn path-meta | |
([x path] | |
(path-meta x path nil)) | |
([x path query] | |
(if (and query (or (map? query) (vector? query))) | |
(letfn [(strip-params [q] | |
(cond-> q (seq? q) first)) | |
(descend-into-map-entry [k v] | |
(path-meta | |
v | |
(conj path k) | |
(let [q (strip-params | |
;; TODO: check if `query` is a map which | |
;; happens for union queries. How do we | |
;; know which subquery to descend with? | |
(if (vector? query) | |
(if (= (first query) '*) '* (nth query idx))))] | |
(if (and (map? v) (map? q)) | |
;; If the value and the query is a map, it was a | |
;; join that returned a single value. Use the join | |
;; query specifically | |
(first (vals q)) | |
q))))] | |
(let [x' (cond->> x | |
(map? x) | |
(into {} (map-indexed | |
(fn [idx [k v]] | |
[k (descend-into-map-entry k v)]))) | |
(vector? x) | |
(into [] (map-indexed | |
#(path-meta %2 | |
(conj path %1) | |
(if (map? query) | |
(first (vals (strip-params query))))))))] | |
(cond-> x' | |
#?(:clj (instance? clojure.lang.IObj x') | |
:cljs (satisfies? IWithMeta x')) | |
(vary-meta assoc :om-path path))))))) |
anmonteiro
commented
Mar 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment