-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathetic.cljc
442 lines (390 loc) · 16.1 KB
/
pathetic.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
(ns com.yetanalytics.pathetic
(:require [clojure.spec.alpha :as s]
[com.yetanalytics.pathetic.json :as json]
[com.yetanalytics.pathetic.json-path :as json-path]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; opts-map specs
(s/def ::first? boolean?)
(s/def ::strict? boolean?)
(s/def ::return-missing? boolean?)
(s/def ::return-duplicates? boolean?)
(s/def ::prune-empty? boolean?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- assert-valid-parse
"Throw exception if result of json-path/parse is error data."
[res]
(when (json-path/is-parse-failure? res)
(throw (ex-info "Cannot parse JSONPath string"
(assoc res :type ::invalid-path)))))
(defn- assert-strict-valid
"Throw exception if `strict?` is true and vector of parsed
paths fails strict mode."
[strict? paths]
(when strict?
(when-let [strict-elem (some json-path/test-strict-path paths)]
(throw (ex-info "Illegal path element in strict mode"
{:type ::invalid-strict-path
:paths paths
:element strict-elem})))))
(defn- filter-missing
"If filter? is true, then return only path sequences where the
entire path has been exhausted, i.e. a value can be found at
the JSONPath location."
[filter? path-seqs]
(if filter?
(filter (complement :fail) path-seqs)
path-seqs))
(defn- int-maps->vectors [m]
(cond (or (not (coll? m)) (empty? m))
m
(vector? m)
(mapv int-maps->vectors m)
:else
(let [ks (keys m)]
(if (int? (first ks)) ;; Optimization: assume keys are homogenous
;; Turn int-to-val maps back into vectors
(->> (sort ks)
(reduce (fn [acc k] (conj acc (get m k))) [])
(mapv int-maps->vectors))
;; Regular map; recurse over values
(reduce-kv (fn [acc k v] (assoc acc k (int-maps->vectors v)))
{}
m)))))
(defn- prune
"Remove empty vectors and arrays, as well as key-val pairs where the
val is empty."
[node]
(letfn [(empty-coll? [x] (and (coll? x) (empty? x)))]
(cond (map? node)
(reduce-kv (fn [acc k v]
(let [v' (prune v)]
(if-not (empty-coll? v') (assoc acc k v') acc)))
{}
node)
(vector? node)
(filterv (complement empty-coll?)
(map prune node))
:else
node)))
(defn- remove-nth
[coll n]
(if-not (empty? coll)
(vec (concat (subvec coll 0 n) (subvec coll (inc n))))
coll))
(defn- cmp-seqs
"Sort sequences lexicographically, such that:
- shorter seqs come after longer seqs
- strings come after ints
This is the reverse of the default `compare` fn on vectors."
[seq-1 seq-2]
(loop [s1 seq-1 s2 seq-2]
(let [x1 (first s1)
x2 (first s2)]
(cond
(and (some? x1) (some? x2))
(cond (and (string? x1) (int? x2)) 1
(and (int? x1) (string? x2)) -1
(= x1 x2) (recur (rest s1) (rest s2))
:else (* -1 (compare x1 x2)))
(and (nil? x1) (nil? x2)) 0
(some? x1) -1
(some? x2) 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; API functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Originally "parse"
(defn parse-paths
"Given a JSONPath string `paths`, parse the string. Each parsed
path is a vector of the following elements:
'.. recursive descent operator
'* wildcard operator
[...] a vector of strings (keys), integers (array indices), or
maps (array slicing operations).
The following `opts-map` fields are supported:
:first? Return a singleton vector containing the first path
when multiple paths are joined using \"|\". Default
false.
:strict? If true, disallows recursive descent, array slicing,
and negative indices. Conformant to the xAPI Profile
spec. Default false."
([paths]
(parse-paths paths {}))
([paths opts-map]
(let [{:keys [first? strict?]
:or {first? false strict? false}}
opts-map
res
(json-path/parse paths)]
(assert-valid-parse res)
(assert-strict-valid strict? res)
(if first? (subvec res 0 1) res))))
;; Originally "enumerate"
(s/fdef get-paths*
:args (s/cat :json ::json/json
:paths ::json-path/paths
:opts-map (s/? (s/keys :opt-un [::return-missing?])))
:ret (s/every ::json/path))
(defn get-paths*
"Like `get-paths` except that the `paths` argument is a vector
of already-parsed JSONPaths.
The following `opts-map` fields are supported:
:return-missing? Return partial paths for paths that cannot
match any location in `json`. Default false."
([json paths]
(get-paths* json paths {}))
([json paths opts-map]
(let [{:keys [return-missing?] :or {return-missing? false}}
opts-map
enum-paths
(fn [path]
(->> (json-path/path-seqs json path)
(filter-missing (not return-missing?))
(mapv :path)))]
(->> paths (mapcat enum-paths) distinct vec))))
(defn get-paths
"Given `json` and a JSONPath string `paths`, return a vector of
definite key paths. Each key path is a vector of strings (keys)
or integers (array indices); non-deterministic path entries like
recursive descent and wildcards are removed. If the string
contains multiple JSONPaths, we return the key paths for all
strings.
The following `opts-map` fields are supported:
:first? Only apply the first \"|\"-separated path.
:strict? Disallow recursive descent, array slicing,
and negative array indices.
:return-missing? Return partial paths for paths that cannot
match any location in `json`. Default false."
([json paths]
(get-paths* json (parse-paths paths)))
([json paths opts-map]
(get-paths* json (parse-paths paths opts-map) opts-map)))
;; Originally "get-at"
(s/fdef get-values*
:args (s/cat :json ::json/json
:paths ::json-path/paths
:opts-map (s/? (s/keys :opt-un [::return-missing?
::return-duplicates?])))
:ret (s/every ::json/json :kind vector?))
(defn get-values*
"Like `get-values` except that the `paths` argument is a vector
of already-parsed JSONPaths.
The following `opts-map` fields are supported:
:return-missing? Return values that cannot be found in `json`
data as nil. Default false.
:return-duplicates? Return duplicate values in the result.
Default true."
([json paths]
(get-values* json paths {}))
([json paths opts-map]
(let [{:keys [return-missing? return-duplicates?]
:or {return-missing? false return-duplicates? true}}
opts-map
enum-jsons
(fn [path]
(->> (json-path/path-seqs json path)
(filter-missing (not return-missing?))
(mapv :json)))
remove-dupes
(if return-duplicates? identity distinct)]
(->> paths (mapcat enum-jsons) remove-dupes vec))))
(defn get-values
"Given `json` and a JSONPath string `paths`, return a vector of
JSON values. If the string contains multiple JSONPaths, we return
the union of all these values.
The following `opts-map` fields are supported:
:first? Only apply the first \"|\"-separated path.
:strict? Disallow recursive descent, array slicing,
and negative array indices.
:return-missing? Return values that cannot be found in `json`
as nil. Default false.
:return-duplicates? Return duplicate values in the result.
Default true."
([json paths]
(get-values* json (parse-paths paths)))
([json paths opts-map]
(get-values* json (parse-paths paths opts-map) opts-map)))
;; Formerly "path->data"
(s/fdef get-path-value-map*
:args (s/cat :json ::json/json
:paths ::json-path/paths
:opts-map (s/? (s/keys :opt-un [::return-missing?])))
:ret (s/every-kv ::json/path ::json/json))
(defn get-path-value-map*
"Like `get-path-value-map` except that the `paths` argument is a
vector of already-parsed JSONPaths.
The following `opts-map` fields are supported:
:return-missing? Return path-value pairs where the path cannot
match any location in the `json` The result val
is returned as nil. Default false."
([json paths]
(get-path-value-map* json paths {}))
([json paths opts-map]
(let [{:keys [return-missing?] :or {return-missing? false}}
opts-map
enum-json-kv
(fn [path]
(->> path
(json-path/path-seqs json)
(filter-missing (not return-missing?))
(reduce (fn [acc {jsn :json pth :path}] (assoc! acc pth jsn))
(transient {}))
persistent!))]
(->> paths
(map enum-json-kv)
(reduce (fn [acc m] (merge acc m)) {})))))
(defn get-path-value-map
"Given `json` nd a JSONPath string `paths`, return a map associating
JSON paths to JSON values. Does not return duplicates.
The following `opts-map` fields are supported:
:first? Only apply the first \"|\"-separated path.
:strict? Disallow recursive descent, array slicing,
and negative array indices.
:return-missing? Return path-value pairs where the path cannot
match any location in the `json` The result val
is returned as nil. Default false."
([json paths]
(get-path-value-map* json (parse-paths paths)))
([json paths opts-map]
(get-path-value-map* json (parse-paths paths opts-map) opts-map)))
;; select-keys-at
(s/fdef select-keys-at*
:args (s/cat :json ::json/json
:paths ::json-path/paths)
:ret ::json/json)
(defn select-keys-at*
"Like `select-keys-at` except that the `paths` argument is a vector
of already-parsed JSONPaths.
Does not support an `opts-map` argument."
[json paths]
(letfn [(enum-maps
[path]
(reduce (fn [json {jsn :json pth :path}]
(if (nil? jsn)
(json/jassoc-in json (butlast pth) {})
(json/jassoc-in json pth jsn)))
{}
(json-path/path-seqs json path)))]
(->> paths (map enum-maps) (mapv int-maps->vectors))))
(defn select-keys-at
"Given `json` and a JSONPath string `paths`, return a map or
vector of maps representing the key path into `json`. If the string
contains multiple JSONPaths, we return the maps for all strings.
If no value exists at the selection, return a truncated map with
\"{}\" as the innermost possible value.
The following `opts-map` fields are supported:
:first? Returns only the map corresponding to the first
\"|\"-separated path. Default false.
:strict? Disallow recursive descent, array slicing, and negative
array indices. Default false."
([json paths]
(select-keys-at json paths {}))
([json paths opts-map]
(let [res (select-keys-at* json (parse-paths paths opts-map))]
(if (:first? opts-map) (first res) res))))
;; excise
(s/fdef excise*
:args (s/cat :json ::json/json
:paths ::json-path/paths
:opts-map (s/? (s/keys :opt-un [::prune-empty?])))
:ret ::json/json)
(defn excise*
"Like `excise` except that the `paths` argument is a vector of
already-parsed JSONPaths.
The following `opts-map` fields are supported:
:prune-empty? Removes empty maps and vectors, as well as
key-value pairs where values are empty, after the
elements are excised. Default false."
([json paths]
(excise* json paths {}))
([json paths opts-map]
(let [{:keys [prune-empty?] :or {prune-empty? false}}
opts-map
prune-fn
(if prune-empty? prune identity)
rm-fn
(fn [coll k]
(if (int? k)
(remove-nth coll k)
(dissoc coll k)))
paths'
(->> paths
(map (partial json-path/path-seqs json))
(apply concat)
;; Don't excise failed paths
(filterv (complement :fail))
(map :path)
;; Remove identical paths to avoid out-of-bounds errors
distinct
;; Sort so higher-index vector entries are removed before low-index ones
(sort cmp-seqs))]
(prune-fn
(reduce (fn [json path]
(when-not (empty? path) ;; Return nil for "$"
(let [last-key (last path)
rem-keys (butlast path)]
(if (empty? rem-keys)
(rm-fn json last-key) ;; update-in fails on empty key-paths
(update-in json rem-keys rm-fn last-key)))))
json
paths')))))
(defn excise
"Given `json` and a JSONPath string `paths`, return the JSON value with
the elements at the location removed.
The following `opts-map` fields are supported:
:first? Only apply the first \"|\"-separated path.
:strict? Disallow recursive descent, array slicing, and
negative array indices.
:prune-empty? Removes empty maps and vectors, as well as
key-value pairs where values are empty, after the
elements are excised. Default false."
([json paths]
(excise* json (parse-paths paths)))
([json paths opts-map]
(excise* json (parse-paths paths opts-map) opts-map)))
;; Changed from "apply-values" to only accept one value
(s/fdef apply-value*
:args (s/cat :json ::json/json
:paths ::json-path/strict-paths
:value ::json/json)
:ret ::json/json)
(defn apply-value*
"Like `apply-value` except that the `paths` argument is a vector of
already-parsed JSONPaths.
Does not support an `opts-map` argument."
[json paths value]
(let [paths' (->> paths
(map (partial json-path/speculative-path-seqs json))
(apply concat)
(mapv :path))]
(reduce (fn [json path] (json/jassoc-in json path value))
json
paths')))
(defn apply-value
"Given `json`, a JSONPath string `paths`, and the JSON data
`value`, apply `value` to the location given by `paths` If
the location exists, update the pre-existing value. Otherwise,
create the necessary data structures needed to contain `value`.
The following caveats apply:
- If only the root \"$\" is provided, `json` is overwritten in
its entirety.
- If an array index skips over any vector entries, those skipped
entries will be assigned nil.
- If a path contains a wildcard and the location up to that
point does not exist, create a new vector.
- If a path contains a wildcard and the location is a collection,
append it to the coll. In the case of maps, the key is its
current size, e.g. {\"2\" : \"foo\"}.
- Recursive descent, array slicing, and negative array indices
are disallowed (as per strict mode).
The following `opts-map` fields are supported:
:first? Apply only the first \"|\"-separated path. Default
false.
:strict? If provided, always overrides to true."
([json paths value]
(apply-value* json (parse-paths paths {:strict? true}) value))
([json paths value opts-map]
(apply-value* json (parse-paths paths (assoc opts-map :strict? true)) value)))