Liking cljdoc? Tell your friends :D

Default Viewer

If you would like to add some logic around which viewer to use by default in Portal, the easiest way is to provide a :portal.viewer/default key as part of a value's metadata. Unfortunately, not all values support metadata, such as strings. They way around this limitation is to wrap the value in a container that does support metadata, such as vectors. However, instead of managing this manually, we can leverage the portal.viewer ns to help provide default viewers for values.

Here is on such default viewer selector:

(require '[portal.viewer :as v])

(def defaults
  {string? v/text
   bytes?  v/bin})

(defn- get-viewer-f [value]
  (or (some (fn [[predicate viewer]]
              (when (predicate value)
                viewer))
            defaults)
      v/tree))

With a corresponding submit function:

(require '[portal.api :as p])

(defn submit [value]
  (let [f (get-viewer-f value)]
    (p/submit (f value))))

(add-tap #'submit)

Now by tapping the following values you can see the default viewer selector in action:

(tap> "hello, world")
(tap> (byte-array [0 1 2 3]))
(tap> (range 10))

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close