This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-20
Channels
- # admin-announcements (1)
- # announcements (9)
- # aws (11)
- # babashka (33)
- # beginners (125)
- # calva (20)
- # cider (18)
- # clj-kondo (7)
- # cljs-dev (73)
- # clojure (72)
- # clojure-europe (18)
- # clojure-italy (13)
- # clojure-nl (13)
- # clojure-uk (9)
- # clojurescript (22)
- # core-async (7)
- # cursive (1)
- # data-science (25)
- # datomic (22)
- # duct (32)
- # emacs (13)
- # graalvm (5)
- # hoplon (16)
- # juxt (6)
- # kaocha (8)
- # leiningen (3)
- # malli (11)
- # meander (12)
- # off-topic (18)
- # pathom (109)
- # pedestal (5)
- # rdf (10)
- # reagent (1)
- # reitit (12)
- # shadow-cljs (27)
- # spacemacs (5)
- # sql (9)
- # tools-deps (7)
General consensus on whether this should work or not?
(require '[malli.util :as mu])
(mu/get-in [:map [keyword? int?]] [:a])
;=> returns nil, but I expected int?
I have nested maps with a few generic bits (my use case doesn’t allow composing schemas from reusable parts, yet) and noticed this behavior. Considering it’s supposed to mirror https://clojuredocs.org/clojure.core/get-in in functionality I’m hesitant to call this a bug, just wondering out loud… 🙂(m/validate [:map [keyword? int?]] {:a 1})
; => false
(m/validate [:map [keyword? int?]] {keyword? 1})
; => true
This of course changes the nature of the error, although I’m still not willing to call this a bug; I’m kind of hoping for magic which obviously isn’t there… :)
=> (mu/get-in [:map-of keyword? int?] [:a])
Execution error (IllegalArgumentException) at malli.core/fn$G (core.cljc:26).
No implementation of method: :-get of protocol: #'malli.core/LensSchema found for class: malli.core$_map_of_schema$reify$reify__692
So had a quick stab at it, -get is literally just
(-get [_ key default] (if (key-valid? key) value-schema default))
but -set
is a lot harder as semantically it doesn’t really make sense as the result should probably be some kind of weirdo map schema, eg.
(mu/assoc [:map-of keyword? int?] :foo boolean?)
;=> should probably create
[:or
[:map [:foo boolean?]]
[:map-of keyword? int?]]
and that wouldn’t work for subsequent calls, assoc-in
etc…
One way to do this would be to allow :map
to have default validators for kv pairs, effectively :map-of
within :map
, but that’s way too much work and decisions to do at this point of week 😅Coming back to this, would it make sense to have it as an option for open maps that one could specify default k/v validators for :map
schema? This way the -get
could work if the given validator matches and -set
would switch the default k/v validator to whatever is given if it doesn't match any of the actual keys.