Skip to content

Commit d7f3a20

Browse files
authored
Fix merge for an Instance (workaround for clj-commons/potemkin#70) (#46)
1 parent 6241460 commit d7f3a20

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

.dir-locals.el

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
(eval . (put 'p/def-map-type 'clojure-doc-string-elt 2))
1010
(eval . (put-clojure-indent 'p/defprotocol+ '(1 (:defn))))
1111
(eval . (put-clojure-indent 'p/def-map-type '(2 nil nil (:defn))))
12+
(eval . (put-clojure-indent 'p/deftype+ '(2 nil nil (:defn))))
1213
(eval . (put-clojure-indent 'with-meta '(:form)))
1314
(eval . (put-clojure-indent 'with-bindings* '(:form))))))

src/toucan2/instance.clj

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@
6767
(Instance. model orig m new-meta)))
6868

6969
clojure.lang.IPersistentCollection
70+
(cons [this o]
71+
(cond
72+
(map? o)
73+
(reduce #(apply assoc %1 %2) this o)
74+
75+
(clojure.core/instance? java.util.Map o)
76+
(reduce
77+
#(apply assoc %1 %2)
78+
this
79+
(into {} o))
80+
81+
:else
82+
(if-let [[k v] (seq o)]
83+
(assoc this k v)
84+
this)))
85+
7086
(equiv [_this another]
7187
(cond
7288
(clojure.core/instance? toucan2.protocols.IModel another)

src/toucan2/magic_map.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"The default magic map transform function. Converts things to `kebab-case`, preserving namespaces."
3131
[k]
3232
(when k
33-
(if (and (clojure.core/instance? clojure.lang.Named k) (namespace k))
33+
(if (and (instance? clojure.lang.Named k) (namespace k))
3434
(keyword (->kebab-case (namespace k)) (->kebab-case (name k)))
3535
(keyword (->kebab-case (name k))))))
3636

test/toucan2/instance_test.clj

+7
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,10 @@
411411
empty-instance))
412412
(is (instance/instance? empty-instance))
413413
(is (instance/instance-of? ::venues empty-instance))))))
414+
415+
(deftest ^:parallel merge-test
416+
(let [m (instance/instance ::birds {:name "Parroty"})]
417+
(are [m2 expected] (= expected
418+
(merge m m2))
419+
{:type :parakeet} {:name "Parroty", :type :parakeet}
420+
nil {:name "Parroty"})))

0 commit comments

Comments
 (0)