Skip to content

Commit

Permalink
Fix merge for an Instance (workaround for clj-commons/potemkin#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Jan 4, 2023
1 parent 6241460 commit c4656fd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
(eval . (put 'p/def-map-type 'clojure-doc-string-elt 2))
(eval . (put-clojure-indent 'p/defprotocol+ '(1 (:defn))))
(eval . (put-clojure-indent 'p/def-map-type '(2 nil nil (:defn))))
(eval . (put-clojure-indent 'p/deftype+ '(2 nil nil (:defn))))
(eval . (put-clojure-indent 'with-meta '(:form)))
(eval . (put-clojure-indent 'with-bindings* '(:form))))))
16 changes: 16 additions & 0 deletions src/toucan2/instance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@
(Instance. model orig m new-meta)))

clojure.lang.IPersistentCollection
(cons [this o]
(cond
(map? o)
(reduce #(apply assoc %1 %2) this o)

(clojure.core/instance? java.util.Map o)
(reduce
#(apply assoc %1 %2)
this
(into {} o))

:else
(if-let [[k v] (seq o)]
(assoc this k v)
this)))

(equiv [_this another]
(cond
(clojure.core/instance? toucan2.protocols.IModel another)
Expand Down
2 changes: 1 addition & 1 deletion src/toucan2/magic_map.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"The default magic map transform function. Converts things to `kebab-case`, preserving namespaces."
[k]
(when k
(if (and (clojure.core/instance? clojure.lang.Named k) (namespace k))
(if (and (instance? clojure.lang.Named k) (namespace k))
(keyword (->kebab-case (namespace k)) (->kebab-case (name k)))
(keyword (->kebab-case (name k))))))

Expand Down
7 changes: 7 additions & 0 deletions test/toucan2/instance_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,10 @@
empty-instance))
(is (instance/instance? empty-instance))
(is (instance/instance-of? ::venues empty-instance))))))

(deftest ^:parallel merge-test
(let [m (instance/instance ::birds {:name "Parroty"})]
(are [m2 expected] (= expected
(merge m m2))
{:type :parakeet} {:name "Parroty", :type :parakeet}
nil {:name "Parroty"})))

0 comments on commit c4656fd

Please sign in to comment.