This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-17
Channels
- # aleph (3)
- # announcements (12)
- # beginners (80)
- # boot (3)
- # braveandtrue (16)
- # calva (3)
- # cider (82)
- # clojure (100)
- # clojure-art (3)
- # clojure-dev (79)
- # clojure-estonia (1)
- # clojure-europe (4)
- # clojure-finland (15)
- # clojure-indonesia (1)
- # clojure-italy (20)
- # clojure-nl (4)
- # clojure-spec (24)
- # clojure-sweden (2)
- # clojure-switzerland (1)
- # clojure-uk (99)
- # clojurescript (145)
- # cursive (8)
- # data-science (7)
- # datomic (26)
- # emacs (4)
- # figwheel-main (20)
- # fulcro (8)
- # graphql (3)
- # hoplon (2)
- # jobs (1)
- # kaocha (5)
- # leiningen (2)
- # liberator (19)
- # off-topic (16)
- # pathom (9)
- # perun (1)
- # portkey (2)
- # re-frame (17)
- # reitit (1)
- # shadow-cljs (26)
- # spacemacs (7)
- # vim (49)
Is it possible to (s/merge (s/keys :req-un [:foo/foo]) (s/keys :req-un [:bar/foo]))
such that the rightmost stomps on keys to the left? i.e. looking for a way to combine s/keys
with merge
semantics for merging and precedence
define “stomps on”
I guess replaces?
seems like you want some kind of operation on specs themselves rather than composite specs
Yes, by stomps on I mean replaces. And yes I’m looking for an operation on specs themselves. Essentially I want two different specs that share the majority of their keys, but have a different spec for one or two keys. If such a thing doesn’t exist I guess I can rework it so the commonanility is together in a shared spec, and merge that with the extra different keys… rather than trying to merge the replacement keys over the top.
I guess it’s just a little confusing that s/merge
has different semantics WRT keys than merge
- but I get why s/merge
is like this.
I guess I’m after an s/merge-keys
for merging s/keys
specs.
or maybe s/keys-merge
would be a better name.
Having different semantics for the same key is generally a smell, unless your override is a subset (rather than a different set). I think there may be some new options in spec 2 but I’d say right now I would separate the non-different keys into one spec, then merge with different partial map specs as needed.
Thanks, I’d already done what you suggest…
It’s 3rd party data — so I don’t get to define the keys… but yes the override is actually a subset. Also I’m not sure if this a smell or not; but the superset spec really just exists to represent “noise” that I don’t care about but have to ignore gracefully… I’m not using those specs as specs so much as I’m using the spec API as a way to write generators of noise I need to ignore not barf on.
e.g. I have a spec :raw/Body
representing all message bodys I should just skip over and a spec :specific/Body
for ones I care about. I could use s/or
to model this but Body
is nested in some other specs e.g. a Message
and essentially I don’t want to have to redefine all the intermediates. I think this is essentially similar to the “Maybe Not” problems.
well you can use s/and now to combine your generic spec and (when necessary) the more restrictive spec
so it doesn’t replace, but only the more precise thing will pass both
yeah I did consider that but how do you do so when the spec isn’t the root spec?
yeah, that’s tricky - you need to rebuild everything back to the root
this is the kind of thing that will be easier in the spec 2 select stuff
that was my feeling too
thanks for confirming my intentions were good, at least! 🙂
FWIW I have rebuilt to the root… and rearchitectured the commonality into another spec etc… so it’s not so bad this time 🙂
for example, this won’t conform if :bar
value isn’t a string (or if any of the keys aren’t keywords, or if :foo
is missing, etc.):
(s/conform
(s/merge (s/keys :req-un [::foo])
(s/map-of keyword? string?))
{:foo "" :bar ""})
Thanks. You can do this - but this is not the effect I want. https://clojurians.slack.com/archives/C1B1BB2Q3/p1547804648581400?thread_ts=1547749697.578800&cid=C1B1BB2Q3
dunno it may have been 🙂