-
Notifications
You must be signed in to change notification settings - Fork 7
/
path.clj
220 lines (176 loc) · 6.21 KB
/
path.clj
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(ns com.yetanalytics.datasim.xapi.path
"Given a path into an xAPI structure, return a spec from xapi-schema"
(:require [com.yetanalytics.datasim.json :as json]
[clojure.spec.alpha :as s]
[xapi-schema.spec :as xs]))
(s/def :spec-map.map-spec/keys
qualified-keyword?)
(s/def :spec-map.map-spec/vals
qualified-keyword?)
(s/def :spec-map/map-spec
(s/keys :req-un [:spec-map.map-spec/keys
:spec-map.map-spec/vals]))
(s/def ::spec-map
(s/map-of
qualified-keyword?
(s/or :ns string?
:keyword qualified-keyword?
:fn fn?
:vector vector?
:map :spec-map/map-spec)))
(def spec-map
{::xs/statement "statement"
:statement/actor ::xs/actor
:statement/verb ::xs/verb
:statement/object
(fn [{:strs [objectType] :as object}]
(case objectType
"StatementRef" ::xs/statement-ref
"SubStatement" ::xs/sub-statement
"Agent" ::xs/agent
"Group" ::xs/group
::xs/activity))
:statement/result ::xs/result
:statement/context ::xs/context
:statement/authority ::xs/actor
:statement/attachments ::xs/attachments
::xs/attachments
[::xs/attachment]
::xs/attachment
"attachment"
:attachment/display ::xs/language-map
:attachment/description ::xs/language-map
::xs/actor
(fn [{:strs [objectType] :as actor}]
(if (= "Group" objectType)
::xs/group
::xs/agent))
::xs/agent
"agent"
:agent/account ::xs/account
::xs/group "group"
:group/account ::xs/account
:group/member [::xs/agent]
::xs/account "account"
::xs/verb "verb"
:verb/display ::xs/language-map
::xs/statement-ref "statement-ref"
::xs/sub-statement "sub-statement"
:sub-statement/actor ::xs/actor
:sub-statement/verb ::xs/verb
:sub-statement/result ::xs/result
:sub-statement/context ::xs/context
:sub-statement/attachments ::xs/attachments
:sub-statement/object
(fn [{:strs [objectType] :as object}]
(case objectType
"StatementRef" ::xs/statement-ref
"Agent" ::xs/agent
"Group" ::xs/group
::xs/activity))
::xs/activity "activity"
:activity/definition "definition"
:definition/name ::xs/language-map
:definition/description ::xs/language-map
:definition/choices ::xs/interaction-components
:definition/scale ::xs/interaction-components
:definition/source ::xs/interaction-components
:definition/target ::xs/interaction-components
:definition/steps ::xs/interaction-components
:definition/extensions ::xs/extensions
:definition/correctResponsesPattern [string?]
::xs/interaction-components
[::xs/interaction-component]
::xs/interaction-component "interaction-component"
:interaction-component/description ::xs/language-map
::xs/result "result"
:result/extensions ::xs/extensions
:result/score "score"
::xs/context "context"
:context/instructor ::xs/actor
:context/team ::xs/group
:context/contextActivities ::xs/context-activities
:context/statement ::xs/statement-ref
:context/extensions ::xs/extensions
::xs/context-activities "contextActivities"
:contextActivities/parent ::xs/context-activities-array
:contextActivities/grouping ::xs/context-activities-array
:contextActivities/category ::xs/context-activities-array
:contextActivities/other ::xs/context-activities-array
::xs/context-activities-array
[::xs/activity]
::xs/language-map {:keys ::xs/language-tag
:vals ::xs/language-map-text}
::xs/extensions {:keys ::xs/iri
:vals ::json/any}
})
(comment
(s/valid? ::spec-map spec-map)
)
(defn path->spec
"Given a root spec and a path into it, return the spec for
that path or nil if it is not possible. Accepts hint data to dispatch
multi-specs and the like"
([spec path]
(path->spec spec path nil))
([spec path hint-data]
(if (empty? path)
(do
(assert (or (s/get-spec spec)
(fn? spec)
(s/spec? spec)) "Must return a valid, registered spec or a function or a spec literal")
spec)
(if-let [spec-entry (get spec-map spec)]
(let [p-key (first path)]
(cond
;; direct ref to another spec, these should be traversed silently
(keyword? spec-entry)
(recur
spec-entry
path
hint-data)
;; an ns name, which gets used to speculatively form a keyword
(string? spec-entry)
(let [spec-ns spec-entry]
(assert (string? p-key) "Path key for a map must be a string")
(recur
(keyword spec-ns p-key)
(rest path)
(get hint-data p-key)))
;; A vector just specifies that there is a homogenous array
(vector? spec-entry)
(let [[element-spec] spec-entry]
(assert (number? p-key) "Path key for array must be a number")
(recur
element-spec
(rest path)
(get hint-data p-key)))
;; inference by dispatch function, must have data present or it uses
;; defaults
(fn? spec-entry)
(let [inferred-spec (spec-entry hint-data)]
(recur
inferred-spec
path
hint-data))
;; arbitrary maps
(map? spec-entry)
(let [{keys-spec :keys
vals-spec :vals} spec-entry]
(assert (string? p-key) "Path key for arbitrary map must be a string")
(if (s/valid? keys-spec p-key)
(if (= vals-spec ::json/any)
;; don't loop, any path under any-json is any-json
::json/any
(recur
vals-spec
(rest path)
(get hint-data p-key)))
(throw (ex-info "invalid key for string map"
{:type ::invalid-arbitrary-key
:key p-key
:spec spec
:keys-spec keys-spec}))))))
(throw (ex-info "No spec in map"
{:type ::no-spec-in-map
:spec spec}))))))