|
13 | 13 | [cljs.reader]
|
14 | 14 | [cljs.spec.gen.alpha :as gen]]))
|
15 | 15 | (:import
|
16 |
| - #?@(:clj |
17 |
| - [(clojure.lang AFn IFn Var) |
18 |
| - (java.io Writer)]))) |
| 16 | + #?@(:clj [(clojure.lang IFn) |
| 17 | + (java.io Writer)]))) |
19 | 18 |
|
20 | 19 | ;;
|
21 | 20 | ;; helpers
|
|
47 | 46 | if no spec was found."
|
48 | 47 | [name-or-spec]
|
49 | 48 | (or
|
50 |
| - (and (spec? name-or-spec) name-or-spec) |
51 |
| - (get-spec name-or-spec) |
52 |
| - (throw |
53 |
| - (ex-info |
54 |
| - (str "can't coerce to spec: " name-or-spec) |
55 |
| - {:name-or-spec name-or-spec})))) |
| 49 | + (and (spec? name-or-spec) name-or-spec) |
| 50 | + (get-spec name-or-spec) |
| 51 | + (throw |
| 52 | + (ex-info |
| 53 | + (str "can't coerce to spec: " name-or-spec) |
| 54 | + {:name-or-spec name-or-spec})))) |
56 | 55 |
|
57 | 56 | (defn ^:skip-wiki serialize
|
58 | 57 | "Writes specs into a string that can be read by the reader.
|
|
144 | 143 | (def json-transformer
|
145 | 144 | "Transformer that transforms data between JSON and EDN."
|
146 | 145 | (type-transformer
|
147 |
| - {:name :json |
148 |
| - :decoders stt/json-type-decoders |
149 |
| - :encoders stt/json-type-encoders |
150 |
| - :default-encoder stt/any->any})) |
| 146 | + {:name :json |
| 147 | + :decoders stt/json-type-decoders |
| 148 | + :encoders stt/json-type-encoders |
| 149 | + :default-encoder stt/any->any})) |
151 | 150 |
|
152 | 151 | (def string-transformer
|
153 | 152 | "Transformer that transforms data between Strings and EDN."
|
154 | 153 | (type-transformer
|
155 |
| - {:name :string |
156 |
| - :decoders stt/string-type-decoders |
157 |
| - :encoders stt/string-type-encoders |
158 |
| - :default-encoder stt/any->any})) |
| 154 | + {:name :string |
| 155 | + :decoders stt/string-type-decoders |
| 156 | + :encoders stt/string-type-encoders |
| 157 | + :default-encoder stt/any->any})) |
159 | 158 |
|
160 | 159 | (def strip-extra-keys-transformer
|
161 | 160 | "Transformer that drop extra keys from `s/keys` specs."
|
162 | 161 | (type-transformer
|
163 |
| - {:name ::strip-extra-keys |
164 |
| - :decoders stt/strip-extra-keys-type-decoders})) |
| 162 | + {:name ::strip-extra-keys |
| 163 | + :decoders stt/strip-extra-keys-type-decoders})) |
165 | 164 |
|
166 | 165 | (def strip-extra-values-transformer
|
167 | 166 | "Transformer that drop extra values from `s/tuple` specs."
|
168 | 167 | (type-transformer
|
169 |
| - {:name ::strip-extra-values |
170 |
| - :decoders stt/strip-extra-values-type-decoders})) |
| 168 | + {:name ::strip-extra-values |
| 169 | + :decoders stt/strip-extra-values-type-decoders})) |
171 | 170 |
|
172 | 171 | (def fail-on-extra-keys-transformer
|
173 | 172 | "Transformer that fails on extra keys in `s/keys` specs."
|
174 | 173 | (type-transformer
|
175 |
| - {:name ::fail-on-extra-keys |
176 |
| - :decoders stt/fail-on-extra-keys-type-decoders})) |
| 174 | + {:name ::fail-on-extra-keys |
| 175 | + :decoders stt/fail-on-extra-keys-type-decoders})) |
177 | 176 |
|
178 | 177 | ;;
|
179 | 178 | ;; Transforming
|
|
291 | 290 |
|
292 | 291 | (defmethod walk :or [{:keys [::parse/items]} value accept options]
|
293 | 292 | (reduce
|
294 |
| - (fn [v item] |
295 |
| - (let [transformed (accept item v options) |
296 |
| - valid? (some-> item :spec (s/valid? transformed))] |
297 |
| - (if valid? |
298 |
| - (reduced transformed) |
299 |
| - transformed))) |
300 |
| - value items)) |
| 293 | + (fn [v item] |
| 294 | + (let [transformed (accept item v options) |
| 295 | + valid? (some-> item :spec (s/valid? transformed))] |
| 296 | + (if valid? |
| 297 | + (reduced transformed) |
| 298 | + transformed))) |
| 299 | + value items)) |
301 | 300 |
|
302 | 301 | (defmethod walk :and [{:keys [::parse/items]} value accept options]
|
303 | 302 | (reduce
|
304 |
| - (fn [v item] |
305 |
| - (let [transformed (accept item v options)] |
306 |
| - transformed)) |
307 |
| - value items)) |
| 303 | + (fn [v item] |
| 304 | + (let [transformed (accept item v options)] |
| 305 | + transformed)) |
| 306 | + value items)) |
308 | 307 |
|
309 | 308 | (defmethod walk :nilable [{:keys [::parse/item]} value accept options]
|
310 | 309 | (accept item value options))
|
|
334 | 333 | (defmethod walk :map [{:keys [::parse/key->spec]} value accept options]
|
335 | 334 | (if (map? value)
|
336 | 335 | (reduce-kv
|
337 |
| - (fn [acc k v] |
338 |
| - (let [spec (if (qualified-keyword? k) (s/get-spec k) (s/get-spec (get key->spec k))) |
339 |
| - value (if spec (accept spec v options) v)] |
340 |
| - (assoc acc k value))) |
341 |
| - value |
342 |
| - value) |
| 336 | + (fn [acc k v] |
| 337 | + (let [spec (if (qualified-keyword? k) (s/get-spec k) (s/get-spec (get key->spec k))) |
| 338 | + value (if spec (accept spec v options) v)] |
| 339 | + (assoc acc k value))) |
| 340 | + value |
| 341 | + value) |
343 | 342 | value))
|
344 | 343 |
|
345 | 344 | (defmethod walk :map-of [{:keys [::parse/key ::parse/value]} data accept options]
|
346 | 345 | (if (map? data)
|
347 | 346 | (reduce-kv
|
348 |
| - (fn [acc k v] |
349 |
| - (let [k' (accept key k options) |
350 |
| - v' (accept value v options)] |
351 |
| - (assoc acc k' v'))) |
352 |
| - (empty data) |
353 |
| - data) |
| 347 | + (fn [acc k v] |
| 348 | + (let [k' (accept key k options) |
| 349 | + v' (accept value v options)] |
| 350 | + (assoc acc k' v'))) |
| 351 | + (empty data) |
| 352 | + data) |
354 | 353 | data))
|
355 | 354 |
|
356 | 355 | (defmethod walk :multi-spec [{:keys [::parse/key ::parse/dispatch]} data accept options]
|
|
367 | 366 | (defn- extra-spec-map [data]
|
368 | 367 | (->> (dissoc data :form :spec)
|
369 | 368 | (reduce
|
370 |
| - (fn [acc [k v]] |
371 |
| - (if (= "spec-tools.parse" (namespace k)) acc (assoc acc k v))) |
372 |
| - {}))) |
| 369 | + (fn [acc [k v]] |
| 370 | + (if (= "spec-tools.parse" (namespace k)) acc (assoc acc k v))) |
| 371 | + {}))) |
373 | 372 |
|
374 | 373 | (defn- fail-on-invoke [spec]
|
375 | 374 | (throw
|
376 |
| - (ex-info |
377 |
| - (str |
378 |
| - "Can't invoke spec with a non-function predicate: " spec) |
379 |
| - {:spec spec}))) |
| 375 | + (ex-info |
| 376 | + (str |
| 377 | + "Can't invoke spec with a non-function predicate: " spec) |
| 378 | + {:spec spec}))) |
380 | 379 |
|
381 | 380 | (defn- leaf? [spec]
|
382 | 381 | (:leaf? (into-spec spec)))
|
|
460 | 459 | spec-reason (:reason this)
|
461 | 460 | with-reason (fn [problem]
|
462 | 461 | (cond-> problem
|
463 |
| - (and spec-reason (not (:reason problem))) |
464 |
| - (assoc :reason spec-reason)))] |
| 462 | + (and spec-reason (not (:reason problem))) |
| 463 | + (assoc :reason spec-reason)))] |
465 | 464 | (if problems
|
466 | 465 | (map with-reason problems))))
|
467 | 466 |
|
468 | 467 | (gen* [this overrides path rmap]
|
469 | 468 | (if-let [gen (:gen this)]
|
470 | 469 | (gen)
|
471 | 470 | (or
|
472 |
| - (gen/gen-for-pred spec) |
473 |
| - (s/gen* (or (s/spec? spec) (s/specize* spec)) overrides path rmap)))) |
| 471 | + (gen/gen-for-pred spec) |
| 472 | + (s/gen* (or (s/spec? spec) (s/specize* spec)) overrides path rmap)))) |
474 | 473 |
|
475 | 474 | (with-gen* [this gfn]
|
476 | 475 | (assoc this :gen gfn))
|
|
488 | 487 | [^Spec t ^Writer w]
|
489 | 488 | (.write w (str "#Spec"
|
490 | 489 | (clojure.core/merge
|
491 |
| - (select-keys t [:form]) |
492 |
| - (if (:type t) (select-keys t [:type])) |
493 |
| - (extra-spec-map t)))))) |
| 490 | + (select-keys t [:form]) |
| 491 | + (if (:type t) (select-keys t [:type])) |
| 492 | + (extra-spec-map t)))))) |
494 | 493 |
|
495 | 494 | (defn spec? [x]
|
496 | 495 | (if (instance? Spec x) x))
|
|
550 | 549 | type (if (contains? m :type) type (:type info))
|
551 | 550 | name (-> spec meta ::s/name)
|
552 | 551 | record (map->Spec
|
553 |
| - (clojure.core/merge m info {:spec spec :form form :type type :leaf? (parse/leaf-type? type)}))] |
| 552 | + (clojure.core/merge m info {:spec spec :form form :type type :leaf? (parse/leaf-type? type)}))] |
554 | 553 | (cond-> record name (with-meta {::s/name name}))))
|
555 | 554 |
|
556 | 555 | #?(:clj
|
|
575 | 574 | form# '~(impl/resolve-form &env pred)]
|
576 | 575 | (assert (map? info#) (str "spec info should be a map, was: " info#))
|
577 | 576 | (create-spec
|
578 |
| - (clojure.core/merge |
579 |
| - info# |
580 |
| - {:form form# |
581 |
| - :spec ~pred})))))) |
| 577 | + (clojure.core/merge |
| 578 | + info# |
| 579 | + {:form form# |
| 580 | + :spec ~pred})))))) |
582 | 581 |
|
583 | 582 | (defn- into-spec [x]
|
584 | 583 | (cond
|
|
613 | 612 | (gen* [_ overrides path rmap]
|
614 | 613 | (s/gen* merge-spec overrides path rmap)))]
|
615 | 614 | (create-spec
|
616 |
| - (clojure.core/merge |
617 |
| - {:spec spec |
618 |
| - :form spec-form |
619 |
| - :type :map} |
620 |
| - (apply merge-with set/union form-keys))))) |
| 615 | + (clojure.core/merge |
| 616 | + {:spec spec |
| 617 | + :form spec-form |
| 618 | + :type :map} |
| 619 | + (apply merge-with set/union form-keys))))) |
621 | 620 |
|
622 | 621 | #?(:clj
|
623 | 622 | (defmacro merge [& forms]
|
|
0 commit comments