Skip to content
Permalink
Browse files

added shallow ast conversion

  • Loading branch information...
awkay committed Jun 9, 2019
1 parent ed744ee commit 40e8d31c07a2342d5d45d9f73a71896d019e44c0
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 @@
.idea

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

(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."
false)

;; 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*)
(cond
(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."
[query]
(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."
[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]}]}])
-61421281)))


(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]}}]}
ast)))))

0 comments on commit 40e8d31

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