[spec] Generic spec walking for clojure.spec
Description
Environment
[org.clojure/spec.alpha "0.1.134"]
Activity

Tommi TommiAugust 23, 2019 at 5:03 AM
Implement the 2, spec-visitor, into an experimental external data modelling party library that uses reified protocols like spec, allowing simple and generic model->model transformations using protocols. I think the spec implementation would look about the same - the protocol-based approach is really simple. I would most likely add the `op`
as first argument into the visitor callback for spec.
here’s the polished PR for the lib:https://github.com/metosin/malli/pull/46/files

Tommi TommiFebruary 27, 2019 at 8:16 AM
There are three different versions for ~this in spec-tools now:
1) spec-walker: using `s/form`, walks the specs and values and calls a callback function with both, returning a new value. It can be used for things like coercion. It doesn't validate, need to call `s/valid?` and `s/explain-data` separately (2-3 calls in total)
2) spec-visitor: just walks the forms and return new forms. Used in things like spec => json-schema transformations. Not transforming values.
3) wrapped specs with overridden `s/conform*`, returning new value or error. It's the saddest of the three, because everything needs to be wrapped, but still the only one that knows how to walk over properly regex specs. Bundled transform + validate, like with Schema.
...
https://cljdoc.org/d/metosin/spec-tools/0.9.0/doc/spec-coercion has examples of the first/coercion, hopefully highlighting why support for coercion is important.
Is this issue on a (near) roadmap? Can we help with it? If not on the roadmap, is there a suggested way to do runtime value transformations with spec?

Alex MillerJuly 11, 2018 at 12:20 AM
No plans to look at this before the next batch of implementation changes, so it will be a while.

importJuly 10, 2018 at 11:11 PM
Comment made by: marco.m
hello, any news ?

Tommi TommiApril 17, 2018 at 1:40 PM
Renamed the issue. Instead of Keyword argument, it should take a function to walk the spec to support arbitrary walking applications.
Problem
To do runtime coercion, specs need to be walked twice to strip away the branching information:
s/conform
+s/unform
. This introduced extra latency (see the sample below).Proposal
New versatile
s/walk*
to support generic spec walking.Current status
s/valid?
allows us to do quickly check if a value conforms to a spechttps://dev.clojure.org/jira/browse/CLJ-2115 could help to return the errors in a single sweep.
for coercion, we do
s/conform
+s/unform
/s/explain
https://dev.clojure.org/jira/browse/CLJ-2116 would separate conforming from specs
Still, when running
s/conform
+s/unform
, we walk the specs twice - which is performance-wise suboptimal. Below is a sample, with Late 2013 MacBook Pro with 2,5 GHz i7, with JVM running as-server
.(require '[clojure.spec.alpha :as s]) (s/def ::id int?) (s/def ::name string?) (s/def ::languages (s/coll-of #{:clj :cljs} :into #{})) (s/def ::street string?) (s/def ::zip string?) (s/def ::number int?) (s/def ::address (s/keys :req-un [::street ::zip ::number])) (s/def ::user (s/keys :req [::id] :req-un [::name ::address] :opt-un [::languages])) (def value {::id 1 :name "Liisa" :languages #{:clj :cljs} :address {:street "Hämeenkatu" :number 24 :zip "33200"}}) ; 2.0 µs (cc/quick-bench (s/conform ::user value)) ; 6.2 µs (cc/quick-bench (s/unform ::user (s/conform ::user value)))
Despite
s/conform
is relatively fast, we triple the latency in the sample when running alsos/unform
. As we know already that we are not interested in the branching info, we could just not emit those.Suggestion
s/walk*
to replace boths/confrom*
ands/unform*
, maybe evens/explain*
. It would take extramode
argument, which would be a Keyword of one of the following::validate
- return false on first failing spec:conform
- like the currents/conform*
, maybe also returns/explain
results?:unform
- like the currents/unform*
:coerce
-s/conform*
+s/unform*
, could be optimized (e.g. if no branching info, just return the value)The public apis could be remain the same (+ optional extra argument with CLJ-2116), and a new
s/coerce
to call thes/walk*
with:coerce
.Results
Single sweep validation & coercion. Happy runtime.