schema.macros
Macros and macro helpers used in schema.core.
*compile-fn-validation*
dynamic
-instrument-protocol-method
(-instrument-protocol-method pvar method-var instrument-method)
Given a protocol Var pvar, its method method-var and instrument-method,
instrument the protocol method.
apply-prepost-conditions
(apply-prepost-conditions body)
Replicate pre/postcondition logic from clojure.core/fn.
assert!
macro
(assert! form & format-args)
Like assert, but throws a RuntimeException (in Clojure) and takes args to format.
cljs-env?
(cljs-env? env)
Take the &env from a macro, and tell whether we are expanding into cljs.
compile-fn-validation?
(compile-fn-validation? env fn-name)
Returns true if validation should be included at compile time, otherwise false.
Validation is elided for any of the following cases:
* function has :never-validate metadata
* *compile-fn-validation* is false
* *assert* is false AND function is not :always-validate
defrecord-schema
macro
(defrecord-schema n & args)
Like defrecord for schema primitives, and also registers cross-platform print methods.
emit-defrecord
(emit-defrecord defrecord-constructor-sym env name field-schema & more-args)
error!
macro
(error! s)
(error! s m)
Generate a cross-platform exception appropriate to the macroexpansion context
if-bb
macro
(if-bb then else)
if-cljs
macro
(if-cljs then else)
maybe-split-first
(maybe-split-first pred s)
normalized-defn-args
(normalized-defn-args env macro-args)
Helper for defining defn-like macros with schemas. Env is &env
from the macro body. Reads optional docstring, return type and
attribute-map and normalizes them into the metadata of the name,
returning the normalized arglist. Based on
clojure.tools.macro/name-with-attributes.
parse-arity-spec
(parse-arity-spec spec)
Helper for schema.core/=>*.
parse-defprotocol-sig
(parse-defprotocol-sig env pname name+sig+doc)
process-arrow-schematized-args
(process-arrow-schematized-args env args)
Take an arg vector, in which each argument is followed by an optional :- schema,
and transform into an ordinary arg vector where the schemas are metadata on the args.
process-defprotocol
(process-defprotocol env name+opts+sigs)
process-fn-
(process-fn- env name fn-body)
Process the fn args into a final tag proposal, schema form, schema bindings, and fn form
process-fn-arity
(process-fn-arity env fn-name output-schema-sym bind-meta arity-form)
(process-fn-arity {[bind & body] :arity-form, :keys [env fn-name output-schema-sym bind-meta ufv-sym]})
Process a single (bind & body) form, producing an output tag, schema-form,
and arity-form which has asserts for validation purposes added that are
executed when turned on, and have very low overhead otherwise.
tag? is a prospective tag for the fn symbol based on the output schema.
schema-bindings are bindings to lift eval outwards, so we don't build the schema
every time we do the validation.
:ufv-sym should name a local binding bound to `schema.utils/use-fn-validation`.
5-args arity is deprecated.
resolve-tag
(resolve-tag env tag)
Given a Symbol, attempt to return a valid Clojure tag else nil.
Symbols not contained in `primitive-sym?` will be resolved. Symbols
resolved to Vars have their values checked in an attempt to provide
type hints when possible.
A valid tag is a primitive, Class, or Var containing a Class.
safe-get
macro
(safe-get m k)
Like get but throw an exception if not found. A macro for historical reasons (to
work around cljx function placement restrictions).
set-compile-fn-validation!
(set-compile-fn-validation! on?)
Globally turn on or off function validation from being compiled into s/fn and s/defn.
Enabled by default.
See (doc compile-fn-validation?) for all conditions which control fn validation compilation
split-rest-arg
(split-rest-arg env bind)
try-catchall
macro
(try-catchall & body)
A cross-platform variant of try-catch that catches all* exceptions.
Does not (yet) support finally, and does not need or want an exception class.
*On the JVM certain fatal exceptions are not caught.
validation-error
macro
(validation-error schema value expectation & [fail-explanation])