djblue / portal

A clojure tool to navigate through your data.
https://djblue.github.io/portal/
MIT License
872 stars 81 forks source link

Default viewer for values based on key for associative structures #200

Closed MrEbbinghaus closed 9 months ago

MrEbbinghaus commented 10 months ago

It would be great to be able to assign default viewers to values of associative structures like maps, without using metadata, which often is a pain.

Example: I use µlog for logging and tracing, a log value may look like this:

{:user-agent nil,
 :mulog/duration 2173538,
 :mulog/namespace "my.namespace",
 :mulog/outcome :ok,
 :mulog/parent-trace nil,
 :http-status 200,
 :mulog/root-trace #mulog/flake "4t1B0IuXNLj7lPIClpVFiSU-MwnVGYs1",
 :mulog/timestamp 1698011013823,
 :uri "/",
 :mulog/trace-id #mulog/flake "4t1B0IuXNLj7lPIClpVFiSU-MwnVGYs1",
 :request-method :get,
 :mulog/event-name :my.namespace/http-request}

It would be great if you could add a key → viewer config to portal, that declares the default viewer for the value of a key.

{:mulog/duration  :portal.viewer/duration-ns
 :mulog/timestamp :portal.viewer/relative-time

 ; maybe even a custom view function...
 :mulog/outcome (fn [value] [:span {:style {:color (if (#{:ok} value) "green" "red")}} value])}

I have code that solves this for me, but I think this could be a feature that would be really handy to have in portal itself, as it allows for simple customization, without the hassle of adding metadata to everything.

I would like feedback before I decide if this is something I even could contribute on my own. 🙂

djblue commented 10 months ago

@MrEbbinghaus Gotcha, I think this is mostly already supported in Portal via :portal.viewer/for {} in metadata. For example, here is how it's used for portal.nrepl.