-
Notifications
You must be signed in to change notification settings - Fork 4
/
inspect.cljc
52 lines (44 loc) · 1.23 KB
/
inspect.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns exoscale.coax.inspect
(:require [clojure.spec.alpha :as s]))
(defn- accept-keyword [x]
(when (qualified-keyword? x)
x))
(defn- accept-symbol [x]
(when (qualified-symbol? x)
x))
(defn- accept-set [x]
(when (set? x)
x))
(defn- accept-symbol-call [spec]
(when (and (seq? spec)
(symbol? (first spec)))
spec))
(defn spec-form
"Return the spec form or nil."
[spec]
(some-> spec s/get-spec s/form))
(defn spec-root
"Determine the main spec root from a spec form."
[spec]
(let [spec-def (or (spec-form spec)
(accept-symbol spec)
(accept-symbol-call spec)
(accept-set spec))]
(cond-> spec-def
(qualified-keyword? spec-def)
recur)))
(defn parent-spec
"Look up for the parent coercer using the spec hierarchy."
[k]
(or (accept-keyword (s/get-spec k))
(accept-keyword (spec-form k))))
(s/fdef registry-lookup
:args (s/cat :registry map? :k qualified-keyword?)
:ret any?)
(defn registry-lookup
"Look for the key in registry, if not found try key spec parent recursively."
[registry k]
(if-let [c (get registry k)]
c
(when-let [parent (-> (parent-spec k) accept-keyword)]
(recur registry parent))))