-
-
Save fmnoise/56fdd0951f593e668f4a5e40dc771577 to your computer and use it in GitHub Desktop.
patch spec to allow multiple registries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns spec-patch | |
(:require [clojure.spec.alpha])) | |
(in-ns 'clojure.spec.alpha) | |
(defn ^:dynamic *reg* [] registry-ref) | |
(defn registry | |
"returns the registry map, prefer 'get-spec' to lookup a spec by name" | |
[] | |
@(*reg*)) | |
(defmacro with-registry [r & body] | |
`(binding [*reg* (fn [] ~r)] | |
~@body)) | |
(defn- reg-resolve | |
"returns the spec/regex at end of alias chain starting with k, nil if not found, k if k not ident" | |
[k] | |
(if (ident? k) | |
(let [reg (registry) | |
spec (get reg k)] | |
(if-not (ident? spec) | |
spec | |
(deep-resolve reg spec))) | |
k)) | |
(defn ^:skip-wiki def-impl | |
"Do not call this directly, use 'def'" | |
[k form spec] | |
(c/assert (c/and (ident? k) (namespace k)) "k must be namespaced keyword or resolvable symbol") | |
(let [reg (*reg*) | |
spec (if (c/or (spec? spec) (regex? spec) (get @reg spec)) | |
spec | |
(spec-impl form spec nil nil))] | |
(swap! reg assoc k (with-name spec k)) | |
k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment