Skip to content

Commit b4ca7c2

Browse files
authored
Merge pull request #1223 from igrishaev/tag-nth-friendly
Provide a function to convert new parsing result to the old one
2 parents 874d768 + 7107a3d commit b4ca7c2

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,23 @@ Parsing returns tagged values for `:orn`, `:catn`, `:altn` and `:multi`.
25812581
;; => #malli.core.Tag{:key :malli.core/default, :value {:type "sized", :size 1}}
25822582
```
25832583

2584+
The function `old-parse-format` transforms the new parsing format to the old
2585+
one prior to version 0.18.0:
2586+
2587+
```clojure
2588+
(-> [:* [:catn
2589+
[:prop :string]
2590+
[:val [:altn
2591+
[:s :string]
2592+
[:b :boolean]]]]]
2593+
(m/parse ["-server" "foo" "-verbose" true "-user" "joe"])
2594+
(m/old-parse-format))
2595+
2596+
[{:prop "-server" :val [:s "foo"]}
2597+
{:prop "-verbose" :val [:b true]}
2598+
{:prop "-user" :val [:s "joe"]}]
2599+
```
2600+
25842601
## Unparsing values
25852602

25862603
The inverse of parsing, using `m/unparse` and `m/unparser`:

src/malli/core.cljc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns malli.core
22
(:refer-clojure :exclude [eval type -deref deref -lookup -key assert])
33
#?(:cljs (:require-macros malli.core malli.impl.util))
4-
(:require #?(:clj [clojure.walk :as walk])
4+
(:require [clojure.walk :as walk]
55
[clojure.core :as c]
66
[malli.impl.regex :as re]
77
[malli.impl.util :as miu]
@@ -174,6 +174,18 @@
174174
"Is this a value constructed with `tags`?"
175175
[x] (instance? Tags x))
176176

177+
(defn old-parse-format
178+
"Transform the new parsing format to the old one by
179+
replacing Tag and Tags objects with their content."
180+
[parsed]
181+
(walk/postwalk
182+
(fn [x]
183+
(cond
184+
(tag? x) [(:key x) (:value x)]
185+
(tags? x) (:values x)
186+
:else x))
187+
parsed))
188+
177189
;;
178190
;; impl
179191
;;

test/malli/core_test.cljc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,14 @@
12361236

12371237
(is (true? (m/validate (over-the-wire schema) valid1)))
12381238

1239+
(testing "old parse format"
1240+
(let [tag (m/parse [:orn
1241+
[:foo [:catn [:i :int] [:s :string]]]
1242+
[:bar :uuid]]
1243+
[1 "a"])
1244+
old (m/old-parse-format tag)]
1245+
(is (= [:foo {:i 1 :s "a"}] old))))
1246+
12391247
(testing "ast"
12401248
(is (= {:type :multi,
12411249
:keys {:sized {:order 0,

0 commit comments

Comments
 (0)