Skip to content

Commit

Permalink
Use :orn instead of :altn for map destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Mar 23, 2024
1 parent 8f240d7 commit ead1d01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
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
18 changes: 9 additions & 9 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,7 +170,7 @@
{:name "Nest right-to-left map syntax"
:bind '[{{inner :inner} :outer}]
:schema [:cat
[:altn
[:orn
[:map [:map
[:outer
{:optional true}
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

0 comments on commit ead1d01

Please sign in to comment.