From 6a911fc54f574c9acd2581e7045837448b781afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilker=20L=C3=BAcio?= Date: Sat, 13 Mar 2021 12:37:51 -0300 Subject: [PATCH] Augment proxy to fulfill custom maps interfaces (#755) --- src/babashka/impl/proxy.clj | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/babashka/impl/proxy.clj b/src/babashka/impl/proxy.clj index fd480745..9a68eaad 100644 --- a/src/babashka/impl/proxy.clj +++ b/src/babashka/impl/proxy.clj @@ -8,22 +8,45 @@ (or (get methods k) (throw (UnsupportedOperationException. "Method not implemented: " k)))) -(defn proxy-fn [{:keys [:class :methods]}] +(defn method-or [methods k default] + (or (get methods k) + default)) + +(defn proxy-fn [{:keys [class methods]}] (case (.getName ^Class class) "clojure.lang.APersistentMap" - (proxy [clojure.lang.APersistentMap] [] + (proxy [clojure.lang.APersistentMap clojure.lang.IMeta clojure.lang.IObj] [] (iterator [] ((method-or-bust methods 'iterator) this)) (containsKey [k] ((method-or-bust methods 'containsKey) this k)) (entryAt [k] ((method-or-bust methods 'entryAt) this k)) (valAt ([k] ((method-or-bust methods 'valAt) this k)) ([k default] ((method-or-bust methods 'valAt) this k default))) - (cons [v] ((method-or-bust methods 'cons) this v)) + (cons [v] + (if-let [m (get methods 'cons)] + (m this v) + (proxy-super cons v))) (count [] ((method-or-bust methods 'count) this)) (assoc [k v] ((method-or-bust methods 'assoc) this k v)) (without [k] ((method-or-bust methods 'without) this k)) - (seq [] ((method-or-bust methods 'seq) this))) + (seq [] ((method-or-bust methods 'seq) this)) + + (equiv [other] + (if-let [m (get methods 'equiv)] + (m this other) + (proxy-super equiv other))) + (empty [] ((method-or-bust methods 'empty) this)) + + (meta [] ((method-or methods 'meta nil) this)) + (withMeta [meta] ((method-or methods 'withMeta this) this meta)) + + (toString [] + (if-let [m (get methods 'toString)] + (m this) + (proxy-super toString)))) "clojure.lang.AMapEntry" (proxy [clojure.lang.AMapEntry] [] (key [] ((method-or-bust methods 'key) this)) - (val [] ((method-or-bust methods 'val) this))))) + (val [] ((method-or-bust methods 'val) this)) + (getKey [] ((method-or-bust methods 'getKey) this)) + (getValue [] ((method-or-bust methods 'getValue) this)))))