Skip to content

Instantly share code, notes, and snippets.

@fmnoise
Created December 30, 2018 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmnoise/56fdd0951f593e668f4a5e40dc771577 to your computer and use it in GitHub Desktop.
Save fmnoise/56fdd0951f593e668f4a5e40dc771577 to your computer and use it in GitHub Desktop.
patch spec to allow multiple registries
(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