This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-24
Channels
- # beginners (10)
- # boot (14)
- # cider (80)
- # clara (1)
- # cljs-dev (19)
- # cljsrn (7)
- # clojure (284)
- # clojure-france (4)
- # clojure-italy (57)
- # clojure-poland (8)
- # clojure-russia (10)
- # clojure-spec (65)
- # clojure-uk (155)
- # clojurescript (156)
- # code-reviews (6)
- # copenhagen-clojurians (16)
- # cursive (10)
- # datomic (10)
- # emacs (13)
- # euroclojure (1)
- # graphql (4)
- # jobs (2)
- # lein-figwheel (3)
- # luminus (4)
- # off-topic (2)
- # onyx (42)
- # parinfer (23)
- # pedestal (1)
- # protorepl (8)
- # re-frame (34)
- # reagent (17)
- # ring-swagger (5)
- # timbre (24)
- # vim (72)
- # yada (1)
continually bitten by not knowing if instrumentation is on during interactive dev (e.g. turn it on, then redefine a symbol - now it's off) and tests for a given symbol. i think it's on, then find out i had an invalid spec in a lib for weeks because the upstream lib turned on instrumentation and threw an exception. :thinking_face:
@devth a flag would be complicated, if you think about how instrumentation works, it actually overrides your var, much like a monkey patch in Ruby if you are familiar with, so it would have to re-trigger on every def situation. a simpler solution would be to find a way to handle it on your editor, make it automatically call s/instrument
on file save or something
familiar with how it works and monkey patching. that puts the onus of using this thing correctly on users, and the many tools/editors they use
i kinda like how schema had their own s/defn
. that would provide the means to flip a global switch.
the main issue on having a global toggle would be that it requires a conditional test overhead, imagine if you have to had this overhead on each function call to check if the parameters should be validated or not
that can be quite expensive, the way instrumentation works right now adds no overhead at all when you are not using it
currently can be annoying to use, but editors and other features on top of it can solve this issue, while having the global check maybe not have a way to solve from the user code base
Hello, I’m trying to gen some JSON for the Netflix Eureka API and it has @‘s in the keys
(spec/def :port-def/$ int-str-gen)
(def enabled (keyword (symbol "port-def" "enabled")))
(spec/def enabled boolean?)
(spec/def ::port-def (spec/keys :req-un [:port-def/$ :port-def/enabled]))
produces :port {:$ "9", :enabled false},
I’ve been trying
(spec/def :port-def/$ int-str-gen)
(def enabled (keyword (symbol "port-def" "@enabled")))
(spec/def enabled boolean?)
(spec/def ::port-def (spec/keys :req-un [:port-def/$ enabled]))
produces :port {:$ "9"}
@tetriscodes I think you should use a qualified keyword instead of a symbol for the enabled
spec
I thought :port-def/@enabled
was qualified
@tetriscodes ugly workaround:
(def enabled (keyword (symbol "port-def" "@enabled")))
(defmacro foo []
`(s/def ~enabled boolean?))
(foo)
still no enabled key
spec/keys takes keys that are registered in the catalog as a spec
hi all 👋 anyone happen to know why clojure.spec.gen
doesn’t include let
, and if there’s an idiomatic approach to doing what clojure.test.check.generators/let
does?
How are you using it?
(def ^:private gen-datetime
(tc-gen/let [year (gen/large-integer* {:min 2017 :max 2117}) ; we should be so lucky
month (gen/large-integer* {:min 1 :max 12})
day (gen/large-integer* {:min 1 :max 28}) ; damn you February
hour (gen/large-integer* {:min 0 :max 23})
minute (gen/large-integer* {:min 0 :max 59})
second (gen/large-integer* {:min 0 :max 59})]
(str (string/join "/" [year month day])
" "
(string/join ":" [hour minute second]))))
hmmm maybe there’s an altogether better approach here… I should probably just generate a unix timestamp integer within a certain range, then use clj-time
to convert it into the desired string format…
you could use spec/with-gen
to make your clause a regex and then have your generator create the date formatted as a string and then use clojure.spec.gen/sample
to generate data.
Thanks! Not sure I 100% follow but I’ll try to parse that (mentally) and eventually report back. Thank you!
Looks like you are trying to generate data
So writing the generator can create some random data for you, or the library test.chuck has a string-from-regex function that you can use but it may be harder to get the ranges on your numbers.
https://clojurians.slack.com/archives/C1B1BB2Q3/p1500927450713826 that still gives me errors because they vector needs to look up the keywords
That helps get the spec defined but the lookup of the spec’s keyword return nil
I had heard that in the future spec/keys
may take a function, but I don’t see that in the latest alphas
@tetriscodes well, anywhere you'd use that spec, you'll have to wrap that form in a macro to bypass the reader
But how does the definition of the s/keys vector find that key?
Because I still have to do :port-def/@enabled
which isn’t a valid keyword
(defmacro bar []
`(spec/def ::port-def (spec/keys :req-un [:port-def/$ ~enabled])))
(bar)
that worked
maybe you could do some preprocessing with spec/and
, afaik conformed values flow through
I should use macros everywhere
Thanks. Its in one spot, so I’ll keep it hidden.
Yes, i’ve been encouraged to avoid them, so I’m not familiar.
any chance anyone can see what’s wrong with this?
(s/def ::stream-of-string-rows
(s/with-gen
#(instance? InputStream)
(fn [] (gen/fmap (fn [v] (->> (map (partial string/join ",") v)
(string/join "\n")
.getBytes
io/input-stream))
(gen/vector (s/gen ::string-row))))))
I keep getting errors like this when I run stest/check
on the namespace this is in:
java.util.concurrent.ExecutionException: clojure.lang.ArityException: Wrong number of args (1) passed to: events/fn--4703
clojure.lang.ArityException: Wrong number of args (1) passed to: events/fn--4703
Not sure what’s happening, because when I define these specs in the REPL they work just fine with gen/generate
…