Created
July 26, 2021 11:51
-
-
Save souenzzo/cb37af999d2ab76e68e465427f1f3cd9 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 select-as | |
[value query] | |
(letfn [(selector [{:keys [children] | |
:as node} | |
value] | |
(cond | |
(map? value) (into {} | |
(keep (fn [{:keys [key params] | |
:as node}] | |
(when-let [[_ v] (find value key)] | |
(let [v (if (contains? node :children) | |
(selector node (get value key)) | |
v)] | |
(if (contains? params :as) | |
[(:as params) v] | |
[key v]))))) | |
children) | |
(coll? value) (mapv (partial selector node) | |
value) | |
:else value))] | |
(selector (eql/query->ast query) value))) | |
(select-as {:a 42 | |
:b [{:c 33}]} `[(:a {:as :b}) | |
{(:b {:as :c}) [(:c {:as :d})]}]) | |
=> {:b 42, :c [{:d 33}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another idea:
an
eql-xf
library