Fork me on GitHub
#clojure-spec
<
2017-11-15
>
tbaldridge08:11:50

@ajmagnifico something to consider, Spec also recommends flat maps:

tbaldridge08:11:04

{:cnx-info.source.db/host :env/DB_HOST :cnx-info.source.db/port :env/DB_PORT :cnx-info.dst.db/host "localhost" ... :job/dir "some-directory}

uwo14:11:44

@tbaldridge interesting, is that implicit in its design, or did I miss that recommendation somewhere?

slipset14:11:27

How would you spec a map of ints to maps, eg

slipset14:11:47

{1 {:foo "bar"} 2 {:foo "qux"}}

taylor14:11:09

(s/map-of int? map?) but you could get more specific (s/map-of int? ::some-complex-keys-spec)

slipset14:11:37

I did the second 🙂

ajmagnifico14:11:01

thanks @tbaldridge. That’s a really good idea, and it will actually solve a couple of other problems for me as well. I didn’t recall that keyword namespaces don’t actually need to correspond to real namespaces, even though that’s exactly what taylor told me! I guess I just needed to see it concretely for it to click.

ajmagnifico14:11:50

I guess the only thing you miss out on when you make up namespaces is use of the :: syntax

ajmagnifico14:11:59

but in my case, that’s a small price to pay

dpkp23:11:10

I'm trying to spec a variadic function that has a gnarly java-esque method signature like (f str int) (f str int bool) (f str int bool int) . I've tried to model this w/ s/cat but I'm having trouble building a spec that says (f str int int) is invalid but (f str int bool int) is valid. Example:

(s/def ::foo (s/cat :a string? :b int? :c (s/nilable boolean?) :d (s/nilable int?)))

dpkp23:11:45

this works if args are supplied as nil. but I can't figure out how to also support the smaller arg list. (s/valid? ::foo ["a" 1]) is false

dpkp23:11:48

any pointers?

Alex Miller (Clojure team)02:11:26

Other suggestions were fine but also could use nested s/? for optional stuff

dpkp05:11:37

Something like this?

(s/def ::foo (s/cat :a string? :b int? :opts (s/? (s/cat :c boolean? :opts (s/? (s/cat :d? int?))))))

dpkp23:11:58

ok, will do! thanks

xandrews23:11:36

like (s/def ::foo (s/alt :two-arg-version (s/cat :a string? :b int) :three-arg-version (s/cat :a string? :b int? :c boolean?) ...etc)

xandrews23:11:21

@U1NCSDGDQ in my haste I forgot to include s/cat in the mix

xandrews23:11:35

here was an example that works for me

xandrews23:11:49

(s/def ::foo
  (s/alt
   :two-args (s/cat :a string? :b string?)
   :three-args (s/cat :a string? :b int? :c int?)))

(s/conform
 ::foo
 '("hey" "foo"))

(s/conform
 ::foo
 '("hey" 1 2))