Skip to content

Commit 87b713e

Browse files
authored
Fix spec to argslist translation (#701)
* Fix spec to argslist translation. * Remove redundant test case. * Remove unused spec namespace.
1 parent 8a1d1f1 commit 87b713e

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/datahike/api/specification.cljc

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@
88
[name]
99
(.replace (str name) "?" ""))
1010

11-
(defn spec-args->argslist [args]
12-
(if-not (seq? args)
13-
args
14-
(cond (= (first args) 's/cat)
15-
[[(symbol (name (second args)))]]
16-
17-
(= (first args) 's/alt)
18-
(mapv (fn [s]
19-
(if (= s :nil)
20-
[]
21-
[(symbol (name s))]))
22-
(take-nth 2 (rest args))))))
23-
24-
(comment
25-
(spec-args->argslist '(s/alt :config (s/cat :config spec/SConfig)
26-
:nil (s/cat))))
11+
(defn spec-args->argslist
12+
"This function is a helper to translate a spec into a list of arguments. It is only complete enough to deal with the specs in this namespace."
13+
[s]
14+
(if-not (seq? s)
15+
(if (= :nil s) [] [(symbol (name s))])
16+
(let [[op & args] s]
17+
(cond
18+
(= op 's/cat)
19+
[(vec (mapcat (fn [[k v]]
20+
(if (and (seq? v) (= (first v) 's/*))
21+
(vec (concat ['&] (spec-args->argslist k)))
22+
(spec-args->argslist k)))
23+
(partition 2 args)))]
24+
25+
(= op 's/alt)
26+
(vec (mapcat (fn [[_k v]]
27+
(spec-args->argslist v))
28+
(partition 2 args)))
29+
:else
30+
[]))))
2731

2832
(def api-specification
2933
'{database-exists?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns datahike.test.specification-test
2+
(:require
3+
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
4+
:clj [clojure.test :as t :refer [is are deftest testing]])
5+
[datahike.api.specification :refer [spec-args->argslist]]))
6+
7+
(deftest spec-to-argslist-translation
8+
(testing "Testing core cases of spec to argslist translator."
9+
(is (= (spec-args->argslist '(s/alt :config (s/cat :config spec/SConfig)
10+
:nil (s/cat)))
11+
'[[config] []]))
12+
13+
(is (= (spec-args->argslist '(s/cat :conn spec/SConnection :txs spec/STransactions))
14+
'[[conn txs]]))
15+
16+
(is (= (spec-args->argslist '(s/alt :argmap (s/cat :map spec/SQueryArgs)
17+
:with-params (s/cat :q (s/or :vec vector? :map map?) :args (s/* any?))))
18+
'[[map] [q & args]]))
19+
20+
(is (= (spec-args->argslist '(s/alt :simple (s/cat :db spec/SDB :opts spec/SPullOptions)
21+
:full (s/cat :db spec/SDB :selector coll? :eid spec/SEId)))
22+
'[[db opts] [db selector eid]]))))

0 commit comments

Comments
 (0)