Skip to content

Commit 9eff74c

Browse files
committed
Make Tag object nth-friendly
1 parent 874d768 commit 9eff74c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/malli/core.cljc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,21 @@
153153
#?(:cljs (defn- pr-writer-schema [obj writer opts]
154154
(-pr-writer (-form ^Schema obj) writer opts)))
155155

156-
(defrecord Tag [key value])
156+
(defrecord Tag [key value]
157+
clojure.lang.Indexed
158+
(nth [_ i]
159+
(case (long i)
160+
0 key
161+
1 value
162+
(let [message "wrong tag index"]
163+
(throw
164+
#?(:clj (new IndexOutOfBoundsException message)
165+
:cljs (new js/Error message))))))
166+
(nth [_ i not-found]
167+
(case (long i)
168+
0 key
169+
1 value
170+
not-found)))
157171

158172
(defn tag
159173
"A tagged value, used eg. for results of `parse` for `:orn` schemas."

test/malli/core_test.cljc

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

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

1239+
(testing "tag nth support"
1240+
(let [tag (m/parse schema valid1)
1241+
[k v x] tag]
1242+
(is (= :sized k))
1243+
(is (= valid1 v))
1244+
(is (nil? x))
1245+
(try
1246+
(nth tag 99)
1247+
(is false "should throw")
1248+
(catch #?(:clj IndexOutOfBoundsException
1249+
:cljs js/Error) e
1250+
(is (= "wrong tag index"
1251+
(ex-message e)))))))
1252+
12391253
(testing "ast"
12401254
(is (= {:type :multi,
12411255
:keys {:sized {:order 0,

0 commit comments

Comments
 (0)