File tree Expand file tree Collapse file tree 2 files changed +42
-16
lines changed Expand file tree Collapse file tree 2 files changed +42
-16
lines changed Original file line number Diff line number Diff line change 8
8
[name]
9
9
(.replace (str name) " ?" " " ))
10
10
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
+ []))))
27
31
28
32
(def api-specification
29
33
'{database-exists?
Original file line number Diff line number Diff line change
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]]))))
You can’t perform that action at this time.
0 commit comments