how to customise the print function for native objects in clojurescript - clojure

I'm looking to customize how a react element is displayed, this is the code:
(require ["react" :as react])
(defn react-element? [obj]
(if-let [sym (aget obj "$$typeof")]
(= js/Symbol
(type sym))))
(extend-protocol IPrintWithWriter
object
(-pr-writer [obj writer _]
(js/console.log "hello")
(cond (react-element? obj)
(write-all writer
(-> (js->clj obj)
(dissoc "$$typeof")
str))
:else
(write-all writer (str obj)))))
(react/createElement "h2" nil "Hello, World!")
I'm expecting the output to be
{:type "h2", :key nil, :ref nil, :props {:children "Hello, World!"}}}
but it still prints out the normal #js element:
#js {"$$typeof" Symbol(react.element),
:type "h2", :key nil, :ref nil,
:props #js {:children "Hello, World!"}, :_owner nil}
Is there a way to customize this behaviour?

The root cause is that in the standard lib printing code, implements? is used and satisfies? is not used. This effectively limits printing overrides to things that are not native types. (implements? doesn't doesn't work on native types).
This may not be intentional and may be fixed with https://dev.clojure.org/jira/browse/CLJS-2812

Related

clojure string null check failed with string/blank

(defn get-coll-id [^String coll_id]
(log/info "coll_id: " coll_id)
(if (string/blank? coll_id)
(let [collVal (get-coll-val)]
(log/info "collVal: " collSeqVal)
(format "C%011.0f" collVal))
coll_id))
The log shows "coll_id: null". However, string/blank did not detect the null, and thus log of collVal is skipped. What is the method to check null string?
Something (perhaps a DB?) is giving you the string "null" for coll_id, or perhaps (log/info ...) converts a Clojure nil into the string "null".
Consider this code:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[clojure.string :as str]
))
(defn get-coll-id [^String coll_id]
(println "coll_id: " coll_id)
(if (str/blank? coll_id)
(println :blank)
coll_id))
(dotest
(newline)
(println :v1)
(spyx (get-coll-id nil))
(newline)
(println :v2)
(spyx (get-coll-id (pr-str nil)))
)
with output:
:v1
coll_id: nil
:blank
(get-coll-id nil) => nil
:v2
coll_id: nil
(get-coll-id (pr-str nil)) => "nil"
No matter what you do, you get either the value nil or the string "nil" printed.
Since I haven't used Java for a while, I tried to force it to generate the string "null", but calling o.toString() for a null value creates a
NullPointerException, so that is not the answer.
Update
As amalloy points out, String.valueOf() will convert a Java null into the string "null":
package demo;
public class Demo {
public static String go() {
Object o = null;
return String.valueOf( o );
}
}
when run:
(newline)
(spyx :v3 (demo.Demo/go))
with result
:v3 (demo.Demo/go) => "null"
As to your original question, the nil? function could be used:
(defn blank-or-nil?
[s]
(or (nil? s)
(str/blank? s)))
(defn get-coll-id [^String coll_id]
(println "coll_id: " coll_id)
(if (blank-or-nil? coll_id)
(println "found blank-or-nil coll_id")
coll_id))
which then prints found blank-or-nil coll_id when passed a nil value. However, this could confuse things if your are passed the string "null" or the string "nil".
You need to clarify which value is the input, then track down the source.
The above code is based on my favorite template project.

How can I use with-redefs to mock multiple calls to the same function?

I would like to be able to mock MyFunction however I need the mock to return different values when MyFunction is called.
Is it possible to use with-redefs to return different values based on the call order of a function?
(testing "POST /foo/bar and return ok"
(with-redefs [->Baz (fn [_]
(reify MyProtocol (MyFunction [_] [{:something 1}]))
(reify MyProtocol (MyFunction [_] [{:something 2}])))]
(let [response (routes/foo {:request-method :post
:uri "/foo/bar"
:query-params {}
})]
(is (= (:status response) 200)))))
You could use a mutable collection of the return values, then return/remove values from it on each call.
(defn foo [x] (inc x)) ;; example fn to be mocked
If you wanted to mock three calls to foo returning 1, 2, and 3 respectively:
(with-redefs [foo (let [results (atom [1 2 3])]
(fn [_] (ffirst (swap-vals! results rest))))]
(prn (foo 0))
(prn (foo 0))
(prn (foo 0))
;; additional calls would return nil
(prn (foo 0)))
;; 1
;; 2
;; 3
;; nil
That uses swap-vals! to get the old/new values of the atom, but requires Clojure 1.9 or greater.
If you don't have swap-vals! you could do it (less atomically) like this:
(with-redefs [foo (let [results (atom [1 2 3])]
(fn [_]
(let [result (first #results)]
(swap! results rest)
result)))]
...)
We use Picomock for this, and to assert on the parameters for each call, and to assert on the number of calls. Recommended!

Append to an attribute in Enlive

Is it possible to append a value to an attribute using enlive?
example: I have this
edit
and would like this
edit
I am currently doing this:
(html/defsnippet foo "views/foo.html" [:#main]
[ctxt]
[:a] (html/set-attr :href (str "/item/edit/" (ctxt :id))))
But I would prefer not to embed the URL into my code, by just appending the id to the existing URL
(html/defsnippet foo "views/foo.html" [:#main]
[ctxt]
[:a#href] (html/append (ctxt :id)))
#ddk answer is spot on but you may prefer a more generic way to solve the problem
(defn update-attr [attr f & args]
(fn [node]
(apply update-in node [:attrs attr] f args))))
and then
(update-attr :href str "123")
You could always write your own append-attr in the same vein as set-attr. Here is my attempt
(defn append-attr
[& kvs]
(fn [node]
(let [in-map (apply array-map kvs)
old-attrs (:attrs node {})
new-attrs (into {} (for [[k v] old-attrs]
[k (str v (get in-map k))]))]
(assoc node :attrs new-attrs))))
Which gives the following, when appending "/bar" to href, on enlive's representation of A link
((append-attr :href "/bar")
{:tag :a, :attrs {:href "/foo"}, :content "A link"})
;=> {:tag :a, :attrs {:href "/foo/bar"}, :content "A link"}

traversing a vector tree

I want to traverse a vector tree that represents hiccup data structures:
[:div {:class "special"} [:btn-grp '("Hello" "Hi")]]
Then I want to dispatch on the keyword of the vector, if a multimethod has been defined for the keyword, then it would return another set of vectors, which will replace the original tag.
For example, the above structure will be transformed to:
[:div {:class "special"} [:div [:button "Hello"] [:button "Hi"]]]
The custom multimethod will receive the list ("hello" "hi") as parameters. It will then return the div containing the buttons.
How do I write a function that traverses the vector and dispatches on the keyword with everything else in the form as parameter, and then replaces the current form with the returned form ?
(ns customtags
(:require [clojure.walk :as walk]))
(def customtags (atom {}))
(defn add-custom-tag [tag f]
(swap! customtags assoc tag f))
(defn try-transform [[tag & params :as coll]]
(if-let [f (get #customtags tag)]
(apply f params)
coll))
(defmacro defcustomtag [tag params & body]
`(add-custom-tag ~tag (fn ~params ~#body)))
(defn apply-custom-tags [coll]
(walk/prewalk
(fn [x]
(if (vector? x)
(try-transform x)
x)) coll))
Using it:
(require '[customtags :as ct])
(ct/defcustomtag :btn-grp [& coll] (into [:div] (map (fn [x] [:button x]) coll)))
(ct/defcustomtag :button [name] [:input {:type "button" :id name}])
(def data [:div {:class "special"} [:btn-grp "Hello" "Hi"]])
(ct/apply-custom-tags data)
[:div {:class "special"} [:div [:input {:type "button", :id "Hello"}] [:input {:type "button", :id "Hi"}]]]

clojure: adding a debug trace to every function in a namespace?

just started using log4j in one of my home-projects and I was just about to break out the mouse and cut-and-paste (trace (str "entering: " function-name)) into every function in a large module. then the voice of reason caught up and said "there has simply got to be a better way"... I can think of making a macro that wraps a whole block of functions and adds the traces to them or something like that? Any advice from the wise Stack-overflowing-clojurians?
No need for a macro:
(defn trace-ns
"ns should be a namespace object or a symbol."
[ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (and (ifn? #v) (-> v meta :macro not))]
(intern ns
(with-meta s {:traced true :untraced #v})
(let [f #v] (fn [& args]
(clojure.contrib.trace/trace (str "entering: " s))
(apply f args))))))
(defn untrace-ns [ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (:traced (meta v))]
(alter-meta! (intern ns s (:untraced (meta v)))
#(dissoc % :traced :untraced))))
...or something similar. The most likely extra requirement would be to use filter so as not to call trace on things which aren't ifn?s. Update: edited in a solution to that (also handling macros). Update 2: fixed some major bugs. Update 4: added untrace functionality.
Update 3: Here's an example from my REPL:
user> (ns foo)
nil
foo> (defn foo [x] x)
#'foo/foo
foo> (defmacro bar [x] x)
#'foo/bar
foo> (ns user)
nil
user> (trace-ns 'foo)
nil
user> (foo/foo :foo)
TRACE: "entering: foo"
:foo
user> (foo/bar :foo)
:foo
user> (untrace-ns 'foo)
nil
user> (foo/foo :foo)
:foo