Hi,

I am writing a function that transforms Specs to another formats (similar 
to the JSON Schema). Assuming from this post 
<http://www.metosin.fi/blog/clojure-spec-as-a-runtime-transformation-engine/>, 
I am not the only one. There is no surprise that I am using 
clojure.spec/form. Unfortunately I am not fully satisfied with its 
output. To illustrate the problem let's suppose that my transformer will be 
used by a third person who like to write its own spec-generating helpers:

(require '[clojure.spec.alpha :as spec])

(spec/def ::forgivable-keyword (spec/conformer (fn [x] (cond (keyword? x) x
                                                             (string? x) 
(keyword x)
                                                             true 
::spec/invalid))))

(defn kw-enum [& enum-values] (spec/and ::forgivable-keyword (apply 
hash-set enum-values)))

Cool! Let's look how can my transformer cope with it!

(spec/form (kw-enum :a :b :c))
    ; (clojure.spec.alpha/and :my-ns/forgivable-keyword (clojure.core/apply 
clojure.core/hash-set enum-values))

Ouch! Have I just seen a local symbol in the form? I am sorry third person, 
I won't be able to transform the output of your spec-generating functions. 
Unless they are macros:

(defmacro kw-enum [& enum-values] `(spec/and ::forgivable-keyword ~(apply 
hash-set enum-values)))

(spec/form (kw-enum :a :b :c))
    ; (clojure.spec.alpha/and :my-ns/forgivable-keyword #{:c :b :a})

This approach looks better. How does it work in combination with other 
specs?

(spec/form (spec/nilable (kw-enum :a :b :c)))
    ; (clojure.spec.alpha/nilable (my-ns/kw-enum :a :b :c))

There we are. A third-person's function is in the form. We could use eval to 
resolve it, but do we want to? It has been already evaluated once. What if 
there are some side effects?

Practically, one has no option to write spec-generating function and 
maintain usability of the spec/form at the same time. Therefore one of four 
situations must have happened. Either I got something wrong, Specs are not 
meant to be introspected, Specs are not meant to be generated or spec/form 
is badly designed. Which one is it?

(In the case of the fourth one I have some suggestions, but lets keep them 
for later conversation)

Thanks for your time

Marian

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to