Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use :orn instead of :altn for map destructuring #1019

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ You can use `:sequential` to describe homogeneous sequential Clojure collections
;; => false
```

Malli also supports sequence regexes (also called sequence expresions) like [Seqexp](https://github.com/cgrand/seqexp) and Spec.
Malli also supports sequence regexes (also called sequence expressions) like [Seqexp](https://github.com/cgrand/seqexp) and Spec.
The supported operators are `:cat` & `:catn` for concatenation / sequencing

```clojure
Expand Down Expand Up @@ -454,7 +454,7 @@ while `:cat` and `:alt` just use numeric indices for paths:
;; {:path [0 1 1], :in [3], :schema boolean?, :value 11})}
```

As all these examples show, the sequence experssion (seqex) operators take any non-seqex child schema to
As all these examples show, the sequence expression (seqex) operators take any non-seqex child schema to
mean a sequence of one element that matches that schema. To force that behaviour for
a seqex child `:schema` can be used:

Expand Down
6 changes: 3 additions & 3 deletions src/malli/destructure.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@
->arg (fn [[k t]] [:cat [:= k] (if (and references (qualified-keyword? k)) k t)])
schema (cond-> [:map] closed-maps (conj {:closed true}) :always (into (map ->entry keys)))]
(if (or rest sequential-maps)
[:altn [:map schema] [:args (-> (into [:alt] (map ->arg) keys)
(cond-> (not closed-maps) (conj [:cat [:not (into [:enum] (map first) keys)] :any]))
(cond->> :always (conj [:*]) (not rest) (conj [:schema])))]]
[:orn [:map schema] [:args (-> (into [:alt] (map ->arg) keys)
(cond-> (not closed-maps) (conj [:cat [:not (into [:enum] (map first) keys)] :any]))
(cond->> :always (conj [:*]) (not rest) (conj [:schema])))]]
schema)))

(defn -transform [{[k v] :arg schema :schema :as all} options rest]
Expand Down
20 changes: 10 additions & 10 deletions test/malli/destructure_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:as map}]
:schema [:cat
:any
[:altn
[:orn
[:map [:map
[:b {:optional true} :any]
["c" {:optional true} :any]
Expand All @@ -71,7 +71,7 @@
:bind '[{:keys [a :demo/b] :demo/keys [c]}]
:options {::md/required-keys true}
:schema [:cat
[:altn
[:orn
[:map [:map
[:a :any]
:demo/b
Expand All @@ -86,7 +86,7 @@
:options {::md/required-keys true
::md/closed-maps true}
:schema [:cat
[:altn
[:orn
[:map [:map {:closed true}
[:a :any]
:demo/b
Expand All @@ -101,7 +101,7 @@
::md/closed-maps true
::md/references false}
:schema [:cat
[:altn
[:orn
[:map [:map {:closed true}
[:a :any]
[:demo/b :any]
Expand Down Expand Up @@ -130,7 +130,7 @@
:options {::md/sequential-maps false} ;; no effect here
:schema [:cat
:any
[:altn
[:orn
[:map [:map
[:b {:optional true} :any]
["c" {:optional true} :any]
Expand All @@ -151,15 +151,15 @@
:schema [:cat
[:maybe
[:cat
[:altn
[:orn
[:map [:map
[:a {:optional true} :any]
[:b {:optional true} :any]]]
[:args [:* [:alt
[:cat [:= :a] :any]
[:cat [:= :b] :any]
[:cat [:not [:enum :a :b]] :any]]]]]]]
[:altn
[:orn
[:map [:map
[:a {:optional true} :any]
[:b {:optional true} :any]]]
Expand All @@ -170,11 +170,11 @@
{:name "Nest right-to-left map syntax"
:bind '[{{inner :inner} :outer}]
:schema [:cat
[:altn
[:orn
[:map [:map
[:outer
{:optional true}
[:altn
[:orn
[:map [:map
[:inner {:optional true} :any]]]
[:args [:schema
Expand All @@ -185,7 +185,7 @@
[:* [:alt
[:cat
[:= :outer]
[:altn
[:orn
[:map [:map [:inner {:optional true} :any]]]
[:args [:schema [:* [:alt
[:cat [:= :inner] :any]
Expand Down
8 changes: 6 additions & 2 deletions test/malli/experimental_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@
[[1 2 3 4] 10]
[[-1 2 3 4] ::throws]]}
{:var #'inner-outer-no-schema
:calls [[[(list :outer [:not-inner])] nil]]
:instrumented [[[(list :outer [:not-inner])] ::throws]]}])
:calls [[[(list :outer [:not-inner])] nil]
[[{:outer {:inner "here"}}] "here"]
[[{:outer {:not-inner 'foo}}] nil]]
:instrumented [[[(list :outer [:not-inner])] ::throws]
[[{:outer {:inner "here"}}] "here"]
[[{:outer {:not-inner 'foo}}] nil]]}])

(defn -strument! [mode v]
(with-out-str
Expand Down