I've been hacking on a (currently partial) implementation of a spec 
combinator I'm currently calling 'encoding-of'. It's pretty common to have 
some kind of encoded version of a data structure kicking around, in json, 
xml, or whatever, and it's handy to be able to describe it with spec. 
That's what this is for. You give it a pair of functions for encoding and 
decoding and the spec of the 'inner' data, and it glues them together into 
a spec.  

  (s/def :t/nat-or-float (s/or :nat :t/nat, :float :t/float))
  (s/def :t/test-map (s/keys :req [:t/nat-or-float]))
  (s/def :t/stringy-map (encoding-of :t/test-map pr-str read-string))

  (s/conform :t/stringy-map "{:t/nat-or-float 42}")
  ;; => #t{:nat-or-float [:nat 42]}

  (gen/sample (s/gen :t/stringy-map))
  ;; => ("#:t{:nat-or-float -1.0}" "#:t{:nat-or-float 0.75}" 
"#:t{:nat-or-float 2}" "#:t{:nat-or-float -3.0}" "#:t{:nat-or-float 8}" 
"#:t{:nat-or-float 0}" "#:t{:nat-or-float -0.65625}" "#:t{:nat-or-float 1}" 
"#:t{:nat-or-float 0}" "#:t{:nat-or-float 3}")


The partial implementation is here: 
https://gist.github.com/e7f8230920225496370ae38fc55f2175

If there's any interest in this, I can clean it up and release it as a 
standalone library. It necessarily reaches into some of the guts of 
core.spec, so it may be more suitable as a patch to spec itself, if the 
maintainers have an appetite for it. 

- Russell

-- 
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