Fork me on GitHub
#clojure-spec
<
2019-02-25
>
misha07:02:04

@eoliphant ^^^ this is a bit simplified, as you probably will not have issuers as keyword, but it can be expanded with mapping of issuer to number format.

misha07:02:06

you'd also need to customize generators a bit, if you need them.

Audrius10:02:24

what if to use Spec with regular (non namespaced) keywords? should it work OK?

Audrius10:02:39

is there a too t generate spec from data structure?

borkdude10:02:02

@masta I believe spec-tools has such a tool

eoliphant14:02:18

Thanks @misha, @borkdude. One other bit of fun is that at the end of the day, the number of issuers/formats could be large and might end up in a db

rickmoynihan14:02:15

Is it possible to turn clojure/core.specs off during macroexpansion? So you can see the failing expansion?

rickmoynihan14:02:23

I’m assuming there’s a reason this couldn’t also be a set!able var? Not that restarting my REPL is that difficult! 🙂

alexmiller14:02:41

well it's in a system property as stuff is potentially checked while you're loading before you have a chance to set it

alexmiller14:02:22

it could probably also be a dynvar (although that would need to be checked on every macroexpansion, so probably some perf impacts)

👍 1
Audrius14:02:44

(spec/fdef data->graph
  :args ::data
  :ret ::graph)
why this does not work? I mean it will take for ever to start...

alexmiller14:02:40

"not work" == ?

Audrius14:02:24

process starts for ever

alexmiller14:02:39

is data->graph a macro or a function?

alexmiller14:02:09

if a function, it shouldn't do anything until you instrument

Audrius14:02:58

it is a function...

Audrius15:02:42

still running :thinking_face: maybe it is too complex for Spec to handle...

Audrius15:02:03

(spec/def ::graph (clojure.spec.alpha/def
                    :importer.datamodel/graph
                    (clojure.spec.alpha/coll-of
                     (clojure.spec.alpha/or
                      :collection
                      (clojure.spec.alpha/coll-of
                       (clojure.spec.alpha/keys
                        :req-un
                        [:importer.datamodel/label]
                        :opt-un
                        [:importer.datamodel/center
                         :importer.datamodel/edge_id
                         :importer.datamodel/global_id
                         :importer.datamodel/name
                         :importer.datamodel/source
                         :importer.datamodel/target
                         :importer.datamodel/typ]))
                      :simple
                      clojure.core/keyword?))))

(spec/def ::data (clojure.spec.alpha/def
                   :importer.datamodel/data
                   (clojure.spec.alpha/coll-of
                    (clojure.spec.alpha/or
                     :collection
                     (clojure.spec.alpha/coll-of
                      (clojure.spec.alpha/keys
                       :req
                       [:importer.datamodel/global-id]
                       :opt
                       [:importer.datamodel/center
                        :importer.datamodel/part-of
                        :importer.datamodel/type]
                       :opt-un
                       [:importer.datamodel/attributes
                        :importer.datamodel/datasource
                        :importer.datamodel/features
                        :importer.datamodel/ioi-slice
                        :importer.datamodel/name
                        :importer.datamodel/url]))
                     :simple
                     clojure.core/keyword?))))

(spec/fdef data->graph
  :args ::data
  :ret ::graph)

Audrius15:02:08

I have this...

Audrius15:02:21

and runs for ever to start...

guy15:02:53

Have you tried (exercise ::data 3) to see if outputs anything?

Audrius15:02:51

also runs for ever...

Audrius15:02:31

Found it...

Audrius15:02:45

(spec/def ::data (clojure.spec.alpha/def is recursive or something 😄

jrwdunham19:02:06

Hi here! I'm hoping someone can help me out with what is probably a basic question about clojure spec

jrwdunham19:02:12

I have this function:

jrwdunham19:02:13

(defn starts-with-any?
  "Return truthy if s starts with any character in chrs; otherwise nil."
  [s chrs]
  (seq (filter (fn [c] (string/starts-with? s (str c))) chrs)))

jrwdunham19:02:39

and this spec:

jrwdunham19:02:40

(s/fdef starts-with-any?
  :args (s/cat :s string? :chrs string?)
  :ret (s/nilable (s/coll-of char?))
  :fn (s/or :nil-case #(-> % nil?)
            :non-nil-case
            (s/and #(= (-> % :ret set first) (-> % :args :s first))
                   #(= 1 (-> % :ret set count))
                   (fn [x] (subset? (-> x :ret set) (-> x :args :chrs set))))))

jrwdunham19:02:24

if I run (stest/check starts-with-any?)` on this I get a failure

alexmiller19:02:30

#(-> % nil?) == nil? btw

jrwdunham19:02:56

yeah, thanks, I'll change that

borkdude19:02:21

> I get a failure. needs more detail.

jrwdunham19:02:23

{:clojure.spec.alpha/problems ({:path [:fn :nil-case], :pred (clojure.core/fn [%] (clojure.core/-> % clojure.core/nil?)), :val {:args {:s "", :chrs ""}, :ret nil}, :via [], :in []} {:path [:fn :non-nil-case], :pred (clojure.core/fn [%] (clojure.core/= 1 (clojure.core/-> % :ret clojure.core/set clojure.core/count))), :val {:args {:s "", :chrs ""}, :ret nil}, :via [], :in []}), :clojure.spec.alpha/spec #object[clojure.spec.alpha$or_spec_impl$reify__2046 0x3d686af1 "[email protected]"], :clojure.spec.alpha/value {:args {:s "", :chrs ""}, :ret nil}, :clojure.spec.test.alpha/args ("" ""), :clojure.spec.test.alpha/val {:args {:s "", :chrs ""}, :ret nil}, :clojure.spec.alpha/failure :check-failed}

jrwdunham19:02:57

it's failing on a nil ret, but shouldn't it pass on the first branch of s/or?

borkdude19:02:28

it’s failing on the path [:fn :nil-case], so that would be the first branch

jrwdunham19:02:47

oh right, and it needs :ret

jrwdunham19:02:26

haha, thanks. sorry for the typo question

robertfw22:02:52

Is there a recommended/succinct way to reuse a string regex spec in keyword form? That is, I have a spec that uses a regex to define a string format (in this case, it's an ID format). In some places, that value is used as a keyword in a map. I could break out the specifics, but was wondering if there is an easy way to do something in the spirit of (s/def ::id-as-keyword (as-keyword ::id-spec))