Skip to content

Commit 59dc04b

Browse files
committed
Changed the behavior of :cat in the describe function.
1 parent def4b3f commit 59dc04b

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

src/minimallist/core.cljc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@
134134
:let (-valid? (into context (:bindings model)) (:body model) data)
135135
:ref (-valid? context (get context (:key model)) data)))
136136

137-
(defn- with-default-keys [entries]
138-
(into []
139-
(map-indexed (fn [index entry]
140-
(assoc entry :key (:key entry index))))
141-
entries))
142-
143137
(declare -describe)
144138

145139
(defn- sequence-descriptions
@@ -148,22 +142,34 @@
148142
(if (and (#{:alt :cat :repeat :let :ref} (:type model))
149143
(:inlined model true))
150144
(case (:type model)
151-
:alt (mapcat (fn [entry]
145+
:alt (mapcat (fn [index entry]
152146
(->> (sequence-descriptions context (:model entry) seq-data)
153147
(map (fn [seq-description]
154148
{:rest-seq (:rest-seq seq-description)
155-
:desc [(:key entry) (:desc seq-description)]}))))
156-
(with-default-keys (:entries model)))
157-
:cat (reduce (fn [seq-descriptions entry]
158-
(mapcat (fn [acc]
159-
(->> (sequence-descriptions context (:model entry) (:rest-seq acc))
160-
(map (fn [seq-description]
161-
{:rest-seq (:rest-seq seq-description)
162-
:desc (assoc (:desc acc) (:key entry) (:desc seq-description))}))))
163-
seq-descriptions))
164-
[{:rest-seq seq-data
165-
:desc {}}]
166-
(with-default-keys (:entries model)))
149+
:desc [(:key entry index) (:desc seq-description)]}))))
150+
(range)
151+
(:entries model))
152+
:cat (if (every? #(not (contains? % :key)) (:entries model))
153+
(reduce (fn [seq-descriptions entry]
154+
(mapcat (fn [acc]
155+
(->> (sequence-descriptions context (:model entry) (:rest-seq acc))
156+
(map (fn [seq-description]
157+
{:rest-seq (:rest-seq seq-description)
158+
:desc (conj (:desc acc) (:desc seq-description))}))))
159+
seq-descriptions))
160+
[{:rest-seq seq-data
161+
:desc []}]
162+
(:entries model))
163+
(reduce-kv (fn [seq-descriptions index entry]
164+
(mapcat (fn [acc]
165+
(->> (sequence-descriptions context (:model entry) (:rest-seq acc))
166+
(map (fn [seq-description]
167+
{:rest-seq (:rest-seq seq-description)
168+
:desc (assoc (:desc acc) (:key entry index) (:desc seq-description))}))))
169+
seq-descriptions))
170+
[{:rest-seq seq-data
171+
:desc {}}]
172+
(:entries model)))
167173
:repeat (->> (iterate (fn [seq-descriptions]
168174
(mapcat (fn [acc]
169175
(->> (sequence-descriptions context (:elements-model model) (:rest-seq acc))

test/minimallist/core_test.cljc

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
(h/alt [:number (h/fn int?)]
339339
[:sequence (h/cat (h/fn string?))])
340340
[1 [:number 1]
341-
["1"] [:sequence {0 "1"}]
341+
["1"] [:sequence ["1"]]
342342
[1] :invalid
343343
"1" :invalid]
344344

@@ -349,15 +349,9 @@
349349
[:option3 (h/cat (h/fn string?)
350350
(h/fn keyword?))])
351351
(h/fn int?))
352-
[[1 "2" 3] {0 1
353-
1 [:option1 "2"]
354-
2 3}
355-
[1 :2 3] {0 1
356-
1 [:option2 :2]
357-
2 3}
358-
[1 "a" :b 3] {0 1
359-
1 [:option3 {0 "a", 1 :b}]
360-
2 3}
352+
[[1 "2" 3] [1 [:option1 "2"] 3]
353+
[1 :2 3] [1 [:option2 :2] 3]
354+
[1 "a" :b 3] [1 [:option3 ["a" :b]] 3]
361355
[1 ["a" :b] 3] :invalid]
362356

363357
;; alt - inside a cat, but with :inline false on its cat entry
@@ -367,30 +361,24 @@
367361
[:option3 (h/not-inlined (h/cat (h/fn string?)
368362
(h/fn keyword?)))])
369363
(h/fn int?))
370-
[[1 "2" 3] {0 1
371-
1 [:option1 "2"]
372-
2 3}
373-
[1 :2 3] {0 1
374-
1 [:option2 :2]
375-
2 3}
364+
[[1 "2" 3] [1 [:option1 "2"] 3]
365+
[1 :2 3] [1 [:option2 :2] 3]
376366
[1 "a" :b 3] :invalid
377-
[1 ["a" :b] 3] {0 1
378-
1 [:option3 {0 "a", 1 :b}]
379-
2 3}]
367+
[1 ["a" :b] 3] [1 [:option3 ["a" :b]] 3]]
380368

381369
;; cat of cat, the inner cat is implicitly inlined
382370
(h/cat (h/fn int?)
383371
(h/cat (h/fn int?)))
384-
[[1 2] {0 1, 1 {0 2}}
372+
[[1 2] [1 [2]]
385373
[1] :invalid
386374
[1 [2]] :invalid
387375
[1 2 3] :invalid]
388376

389377
;; cat of cat, the inner cat is explicitly not inlined
390378
(h/cat (h/fn int?)
391379
(h/not-inlined (h/cat (h/fn int?))))
392-
[[1 [2]] {0 1, 1 {0 2}}
393-
[1 '(2)] {0 1, 1 {0 2}}
380+
[[1 [2]] [1 [2]]
381+
[1 '(2)] [1 [2]]
394382
[1] :invalid
395383
[1 2] :invalid
396384
[1 [2] 3] :invalid]
@@ -436,8 +424,8 @@
436424
;; repeat - of a cat
437425
(h/repeat 1 2 (h/cat (h/fn int?)
438426
(h/fn string?)))
439-
[[1 "a"] [{0 1, 1 "a"}]
440-
[1 "a" 2 "b"] [{0 1, 1 "a"} {0 2, 1 "b"}]
427+
[[1 "a"] [[1 "a"]]
428+
[1 "a" 2 "b"] [[1 "a"] [2 "b"]]
441429
[] :invalid
442430
[1] :invalid
443431
[1 2] :invalid
@@ -446,9 +434,9 @@
446434
;; repeat - of a cat with :inlined false
447435
(h/repeat 1 2 (h/not-inlined (h/cat (h/fn int?)
448436
(h/fn string?))))
449-
[[[1 "a"]] [{0 1, 1 "a"}]
450-
[[1 "a"] [2 "b"]] [{0 1, 1 "a"} {0 2, 1 "b"}]
451-
['(1 "a") [2 "b"]] [{0 1, 1 "a"} {0 2, 1 "b"}]
437+
[[[1 "a"]] [[1 "a"]]
438+
[[1 "a"] [2 "b"]] [[1 "a"] [2 "b"]]
439+
['(1 "a") [2 "b"]] [[1 "a"] [2 "b"]]
452440
[] :invalid
453441
[1] :invalid
454442
[1 2] :invalid

0 commit comments

Comments
 (0)