This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-19
Channels
- # announcements (5)
- # beginners (68)
- # boot (1)
- # cider (27)
- # clara (11)
- # cljdoc (10)
- # clojure (129)
- # clojure-europe (2)
- # clojure-italy (16)
- # clojure-nl (15)
- # clojure-spec (74)
- # clojure-uk (31)
- # clojurescript (62)
- # core-async (17)
- # cursive (28)
- # datomic (22)
- # duct (29)
- # emacs (10)
- # fulcro (65)
- # hoplon (2)
- # hyperfiddle (16)
- # instaparse (3)
- # kaocha (2)
- # lein-figwheel (3)
- # leiningen (1)
- # mount (1)
- # nrepl (21)
- # off-topic (23)
- # re-frame (59)
- # reitit (18)
- # ring-swagger (2)
- # shadow-cljs (2)
- # spacemacs (16)
- # timbre (2)
- # tools-deps (22)
how do people practically deal with entity maps that have required keys in one situation, but don't in another situation?
I understand you'll probably have to create a separate spec, but that might require duplicating the entire tree of references to other entities, no?
Hi, I'm trying to spec a map (over which I dont' have control) which uses an inconvenient idiom: for a number of members it accepts either a :membername or :memberid key, but at least one must be present. If there's a single member like this, spec/or
is convenient.
I naively started producing a number of spec/or
specs (of the sort: (spec/def :member (spec/or :by-id (spec/keys :req-un [:member/memberid]) :by-name (spec/keys :req-un [:member/membername])))
) but if I merge those with spec/merge
or spec/and
validation becomes impossible
Let's say that I have (spec/merge ::member1 ::member2)
The input for validation of member2 seems to be the output of conform for member1, which becomes a tuple [:by-id val] instead of plain val. Beyond doing some sort of pre processing of the map, any idea of how to address this from spec?
from the docstring: (s/keys :req [::x ::y (or ::secret (and ::user ::pwd))] :opt [::z])
I had typos in my example, I am fairly sure it works with both req-un or req (not in front of laptop atm)
@borkdude example would be: a Person entity in one case has required fields :person/name
:person/birth-date
:person/address
, and Address has required fields :address/street
:address/city
, but in another case I want a person with required keys :person/social-security-number
and :person/address
, but the Address has no required fields. How do you model that? I can define a spec for Person type 1 and one for Person type 2, but both will have a required key :person/address
, but the spec for :person/address
is different in these two entity map specs.
Been discussing the exact same issue just now in #graphql: https://clojurians.slack.com/archives/C4DLZKR9T/p1543316717071700
@alexmiller I have ran code with a spec for assoc
instrumented. it went from 3s to 15 minutes (approx.). I narrowed it down to this:
https://gist.github.com/borkdude/54baa8504064779094d7dd35f10865d7#file-patch-clj-L64
it’s about 400x slower than running without instrumentation
well, assoc is used a lot
so, don’t do that?
I’m not sure what you’re looking for from me
it seems to be expensive to validate specs in general: https://gist.github.com/borkdude/54baa8504064779094d7dd35f10865d7#file-patch-clj-L62
I think it’s not expensive in general, it’s expensive to have spec for things used by Clojure to do everything else
because the validation is then also checked during validation
given that you’re doing these a million times each, they seem pretty fast to me? you’re talking about microseconds per?
if you could provide a narrative rather than just a wall of data, that would help
right. I instrumented an Advent of Code puzzle and the time went from 3s to 15 minutes. That seemed a little but much for instrumentation overhead.
well, it depends what it’s doing and what you spec’ed
if you instrument something in a hot loop…. then that will be slow
was an effort to compare different fws
at a micro level and spec did pretty well there
@borkdude 's benchmark is showing that instrument is much slower then valid? The benchmark you linked used valid? Have an idea why instrument would be slower then valid?
Right, from the impl, it does seem it uses conform to validate. Even though it doesn't seem to do anything with the conformed value. I wonder why its not just using valid?
Oh, valid also uses conform. Ok, well I'm learning a few things looking at the impl 😛
What are the big changes in spec-alpha2
? I’m assuming there are breaking changes, given the artifact has changed names.
at the moment, the public api is the same
there are some additions and likely breaking changes down the line
ok so the repackaging is mostly precautionary? i.e. in case you were to push a breaking change?
it is currently a work in progress, we’ll have more to talk about it as we get to a release point
Understood.
cool, thanks. yes, the code was a hot loop. I also tested with spec-alpha2 and saw about the same numbers.
it’s not compiled
there’s some trickiness in the circularity between clojure and spec.alpha
and compilation makes that trickier than it’s worth