Skip to content

Commit

Permalink
Add IOverride cache
Browse files Browse the repository at this point in the history
Closes #87
  • Loading branch information
bsless committed Mar 25, 2024
1 parent a4cb207 commit 45b6769
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
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

0 comments on commit 45b6769

Please sign in to comment.