Fork me on GitHub
#clojure-spec
<
2017-11-28
>
otfrom10:11:50

I'm creating a look up hash map from a vector of records where the keys will be strings based on what I want to look up. Anyone seen an example of how to spec this? I can't think of how to do it with s/keys

otfrom10:11:42

[{:a "foo" :b "shiny"} {:a "bar" :b "dull"}]
=>
{"foo" {:a "foo" :b "shiny"} 
 "bar" {:a "bar" :b "dull"}}

guy10:11:42

I’m not sure but could you use map-of ?

guy10:11:19

(s/def ::scores (s/map-of string? int?))
(s/conform ::scores {"Sally" 1000, "Joe" 500})

guy10:11:39

taken from https://clojure.org/guides/spec if you ctrl+f map-of

guy11:11:59

hopefully thats helpfully! :thumbsup:

sekao21:11:46

is there any library that can "unroll" a spec so i can display a given spec with all its component specs in a single data structure?

taylor21:11:00

might be some interesting stuff in this project https://github.com/jpmonettas/inspectable

sekao21:11:12

hmm the spec browser is neat but does that lib allow you to just get the whole spec as data?

taylor21:11:31

you can already get that with s/form if I understand you correctly

sekao22:11:51

s/form doesn't recursively grab specs that a given spec refers to

taylor22:11:25

yeah you’ll have to do some additional lookup, but I think that should be pretty easy

taylor22:11:31

maybe using clojure.walk and s/get-spec?

taylor22:11:44

(walk/postwalk
  (fn [v]
    (if (keyword? v)
      (or (some-> v s/get-spec s/form)
          v)
      v))
  (s/form ::my-coll))
here’s an idea, maybe not a good one

taylor22:11:16

doesn’t cover specs registered to symbols, and probably other cases, etc.