Skip to content

Commit e97a0f6

Browse files
committed
fix: dissoc-in creating maps for missing paths
Fix an issue where `dissoc-in` could create nested maps on the way down. Now, if the path doesn't exist we don't traverse further.
1 parent 08288f4 commit e97a0f6

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/wing/core.cljc

+6-6
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@
407407
(defn dissoc-in
408408
"Like `clojure.core/dissoc` but takes a path like `clojure.core/get-in`"
409409
[m path]
410-
(let [path-count (count path)]
411-
(if (= 1 path-count)
412-
(dissoc m (first path))
413-
(let [[update-tgt [dissoc-tgt]] (split-at (- path-count 1) path)]
414-
(update-in m update-tgt dissoc dissoc-tgt)))))
410+
(let [[k & ks] path]
411+
(cond
412+
(or (empty? path)
413+
(not (contains? m k))) m
414+
(empty? ks) (dissoc m k)
415+
:else (update m k dissoc-in ks))))
415416

416417
(defn index-by
417418
"Indexes a `coll` by the value returned by `f`. Returns a map.
@@ -570,7 +571,6 @@
570571
(#?(:clj .removeFirst :cljs .shift) a))
571572
(rf result v))
572573
result)))))))
573-
574574
(comment
575575
(toggle #{:a :b} :c)
576576
(toggle #{:a :b} :b)

test/wing/core_test.cljc

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88

99
(def gen-simple-map
1010
"Generator for simple map"
11-
(let [simple-gen (gen/one-of [gen/keyword gen/int gen/string gen/boolean])
11+
(let [simple-gen (gen/one-of [gen/keyword gen/small-integer gen/string gen/boolean])
1212
simple-val (gen/frequency [[5 simple-gen]
1313
[1 (gen/map simple-gen simple-gen)]
1414
[1 (gen/list simple-gen)]
1515
[1 (gen/vector simple-gen)]
1616
[1 (gen/set simple-gen)]])]
1717
(gen/map simple-gen simple-val)))
1818

19+
(deftest assert-info-test
20+
(is (thrown?
21+
#?(:clj clojure.lang.ExceptionInfo :cljs js/Error)
22+
(sut/assert-info (= 1 2) "Non-sense error" {:foo :bar}))))
23+
1924
(deftest deep-merge-test
2025
(testing "is identity for single arguments"
2126
(is (= 3 (sut/deep-merge 3)))
@@ -46,7 +51,7 @@
4651

4752
(defspec apply-if-applies-if-pred-returns-true
4853
5
49-
(prop/for-all [x gen/int]
54+
(prop/for-all [x gen/small-integer]
5055
(is (= (inc x)
5156
(sut/apply-if x (constantly true) inc)))))
5257

@@ -58,7 +63,7 @@
5863

5964
(defspec apply-when-applies-if-pred-returns-true
6065
5
61-
(prop/for-all [x gen/int]
66+
(prop/for-all [x gen/small-integer]
6267
(is (= (inc x)
6368
(sut/apply-when x (constantly true) inc)))))
6469

@@ -457,7 +462,10 @@
457462
:h 9}}]
458463
(is (= {:a 1 :b 2 :c 3 :d {:e {:g true}
459464
:h 9}}
460-
(sut/dissoc-in m [:d :e :f]))))))
465+
(sut/dissoc-in m [:d :e :f])))))
466+
(testing "does not alter maps for paths that don't exist"
467+
(let [m {:a 1 :b 2}]
468+
(is (= m (sut/dissoc-in m [:c :d]))))))
461469

462470
(deftest index-by-test
463471
(let [data [{:name "Bob" :age 42}
@@ -510,6 +518,7 @@
510518
(is (= '(1
511519
3
512520
5)
521+
#_{:clj-kondo/ignore [:deprecated-var]}
513522
(sut/unfold f 1))))))
514523

515524
(deftest arity-test
@@ -538,6 +547,7 @@
538547
(sut/make-map "a" a b))))))
539548

540549
(deftest random-uuid-test
550+
#_{:clj-kondo/ignore [:deprecated-var]}
541551
(is (uuid? (sut/random-uuid))))
542552

543553
(deftest compr-test
@@ -578,3 +588,6 @@
578588
coll (gen/vector gen/small-integer 1 50)]
579589
(is (= (partition size step nil coll)
580590
(sequence (sut/sliding size step) coll)))))
591+
592+
(deftest deep-merge*-test
593+
(is (= 1 1)))

0 commit comments

Comments
 (0)