Browse files

Add query-id

  • Loading branch information...
wilkerlucio committed Nov 2, 2018
1 parent 41514fe commit f804216425efc59343ac631bc95f68c84fa42580
Showing with 49 additions and 0 deletions.
  1. +21 −0 src/edn_query_language/core.cljc
  2. +28 −0 test/edn_query_language/core_test.clj
@@ -584,3 +584,24 @@
(s/fdef merge-queries
:args (s/cat :qa (s/nilable ::query), :qb (s/nilable ::query))
:ret (s/nilable ::query))

(defn normalize-query-variables
"Converts ident values and param values to ::p/var."
(->> (query->ast query)
(map (fn [x]
(cond-> x
(ident? (:key x))
(assoc :key [(first (:key x)) ::var])

(:params x)
(update :params #(into {} (map (fn [[k _]] [k ::var])) %))))))

(defn query-id
"Generates a consistent hash from the query. The query first goes to a process to remove any
variables from idents and params, then we get the Clojure hash of it. You can use this to save
information about a query that can be used to correlate with the query later."
(hash (normalize-query-variables query)))
@@ -259,3 +259,31 @@
(is (= (eql/merge-queries ['(hello {:login "u1"})]
['(hello {:login "u1"})])

(deftest test-normalize-query-variables
(testing "blank query"
(is (= (eql/normalize-query-variables [])

(testing "simple query"
(is (= (eql/normalize-query-variables [:a :b :c])
[:a :b :c])))

(testing "normalize ident values"
(is (= (eql/normalize-query-variables [[:foo "bar"]])
[[:foo ::eql/var]])))

(testing "normalize params"
(is (= (eql/normalize-query-variables ['(:foo {:x 1 :y 2})])
['(:foo {:x ::eql/var :y ::eql/var})])))

(testing "all together"
(is (= (eql/normalize-query-variables '[:a :b {[:join "val"] [{(:c {:page 10}) [:d]}]}])
'[:a :b
{[:join ::eql/var]
[({:c [:d]}
{:page ::eql/var})]}]))))

(deftest test-query-id
(is (= (eql/query-id '[:a :b {[:join "val"] [{(:c {:page 10}) [:d]}]}])

0 comments on commit f804216

Please sign in to comment.