Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IOverride cache #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/fipp/edn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Provides a pretty document serializer and pprint fn for Clojure/EDN forms.
See fipp.clojure for pretty printing Clojure code."
(:require [clojure.string :as str]
[fipp.ednize :refer [edn record->tagged]]
[fipp.ednize :refer [edn record->tagged IOverride]]
[fipp.visit :refer [visit visit*]]
[fipp.engine :refer (pprint-document)]))

Expand All @@ -19,10 +19,22 @@
[:span sep "..."])]
[:group open [:align ys ellipsis] close]))

(defrecord EdnPrinter [symbols print-meta print-length print-level]
(defn- cached-override?
[cache x]
(let [clazz (type x)
ret (get @cache clazz ::not-found)]
(if (identical? ::not-found ret)
(let [ret (satisfies? IOverride x)]
(vswap! cache assoc clazz ret)
ret)
ret)))

(defrecord EdnPrinter [symbols print-meta print-length print-level cache]

fipp.visit/IVisitor

(visit-override? [_ x]
(cached-override? cache x))

(visit-unknown [this x]
(visit this (edn x)))
Expand Down Expand Up @@ -96,6 +108,7 @@
([x] (pretty x {}))
([x options]
(let [defaults {:symbols {}
:cache (volatile! {})
:print-length *print-length*
:print-level *print-level*
:print-meta *print-meta*}
Expand Down
7 changes: 4 additions & 3 deletions src/fipp/visit.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns fipp.visit
"Convert to and visit edn structures."
(:require [fipp.util :as util]
[fipp.ednize :refer [override?]]))
(:require [fipp.util :as util]))

;;;TODO Stablize public interface

Expand All @@ -27,14 +26,16 @@
(visit-var [this x])
(visit-pattern [this x])
(visit-record [this x])

(visit-override? [this x])
)

(defn visit*
"Visits objects, ignoring metadata."
[visitor x]
(cond
(nil? x) (visit-nil visitor)
(override? x) (visit-unknown visitor x)
(visit-override? visitor x) (visit-unknown visitor x)
(util/boolean? x) (visit-boolean visitor x)
(string? x) (visit-string visitor x)
(util/char? x) (visit-character visitor x)
Expand Down