Skip to content
Browse files

Merge pull request #3 from edn-query-language/fulcro3-extensions

added shallow conversion for query to ast
  • Loading branch information...
awkay committed Jun 9, 2019
2 parents ed744ee + 40e8d31 commit 6df0f733c059b13edd93d0dac5d4dbc15365e455
Showing with 29 additions and 2 deletions.
  1. +2 −1 .gitignore
  2. +13 −1 src/edn_query_language/core.cljc
  3. +14 −0 test/edn_query_language/core_test.clj
@@ -1,2 +1,3 @@

@@ -5,6 +5,11 @@
[clojure.test.check.generators :as gen #?@(:cljs [:include-macros true])]

(def ^:dynamic *shallow-conversion*
"Dynamic var. When bound to true in the current thread calls to query->ast will no go past the
first level of children. This is useful when you just want the AST for one layer of a query."

;; query specs

(def generators
@@ -340,7 +345,7 @@
{:component component})
(when query-root?
{:query-root true})
(when-not (or (number? v) (= '... v))
(when-not (or (number? v) (= '... v) *shallow-conversion*)
(vector? v) {:children (into [] (map expr->ast) v)}
(map? v) {:children [(union->ast v)]}
@@ -605,3 +610,10 @@
information about a query that can be used to correlate with the query later."
(hash (normalize-query-variables query)))

(defn query->shallow-ast
"Like query->ast, but does not follow joins. Useful for efficiently getting just the top-level entries in
a large query."
(binding [*shallow-conversion* true]
(query->ast query)))
@@ -287,3 +287,17 @@
(deftest test-query-id
(is (= (eql/query-id '[:a :b {[:join "val"] [{(:c {:page 10}) [:d]}]}])

(deftest shallow-conversion
(testing "requesting shallow conversion will only convert the first layer of a query"
(let [ast (eql/query->shallow-ast [:x
{:y [{:z [:a]}]}
{[:table 1] [:z {:other [:m :n]}]}
{:ujoin {:u1 [:x] :u2 [:y]}}])]
(is (= {:type :root,
:children [{:type :prop, :dispatch-key :x, :key :x}
{:type :join, :dispatch-key :y, :key :y, :query [{:z [:a]}]}
{:type :join, :dispatch-key :table, :key [:table 1], :query [:z {:other [:m :n]}]}
{:type :join, :dispatch-key :ujoin, :key :ujoin, :query {:u1 [:x], :u2 [:y]}}]}

0 comments on commit 6df0f73

Please sign in to comment.
You can’t perform that action at this time.