Skip to content

Commit

Permalink
Throw on referencing undefined rule (closes #319)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jun 24, 2020
1 parent a155fd1 commit cb58d72
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Support return maps (:keys/:syms/:strs) in query #322 #345
- Throw when init-db is used with anything but datoms #276
- Throw on using unindexed attribute in :avet index access #344
- Throw on referencing undefined rule #319

# 0.18.13

Expand Down
30 changes: 24 additions & 6 deletions src/datascript/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[clojure.set :as set]
[clojure.string :as str]
[clojure.walk :as walk]
[datascript.db :as db #?(:cljs :refer-macros :clj :refer) [raise]]
[datascript.db :as db #?(:cljs :refer-macros :clj :refer) [raise cond+]]
[me.tonsky.persistent-sorted-set.arrays :as da]
[datascript.lru]
[datascript.impl.entity :as de]
Expand Down Expand Up @@ -562,11 +562,29 @@
;;; RULES

(defn rule? [context clause]
(and (sequential? clause)
(contains? (:rules context)
(if (source? (first clause))
(second clause)
(first clause)))))
(cond+
(not (sequential? clause))
false

:let [head (if (source? (first clause))
(second clause)
(first clause))]

(not (symbol? head))
false

(free-var? head)
false

(contains? #{'_ 'or 'or-join 'and 'not 'not-join} head)
false

(not (contains? (:rules context) head))
(raise "Unknown rule '" head " in " clause
{:error :query/where
:form clause})

:else true))

(def rule-seqid (atom 0))

Expand Down
23 changes: 15 additions & 8 deletions test/datascript/test/query_rules.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@
[?y ?a]
[(>= ?a 18)]]])
#{["Oleg"]})))
)

(testing "Rule name validation #319"
(is (thrown-msg? "Unknown rule 'wat in (wat ?x)"
(d/q '[:find ?x
:in $ %
:where (wat ?x)]
[] [])))))

;; https://github.com/tonsky/datascript/issues/218
(deftest test-false-arguments
Expand All @@ -169,10 +175,11 @@
[:db/add 2 :attr false]])
rules '[[(is ?id ?val)
[?id :attr ?val]]]]
(is (= (d/q '[:find ?id :in $ %
:where (is ?id true)]
db rules)
#{[1]}))
(is (= (d/q '[:find ?id :in $ %
:where (is ?id false)] db rules)
#{[2]}))))
(is (= #{[1]}
(d/q '[:find ?id :in $ %
:where (is ?id true)]
db rules)))
(is (= #{[2]}
(d/q '[:find ?id :in $ %
:where (is ?id false)]
db rules)))))

0 comments on commit cb58d72

Please sign in to comment.