Skip to content

Commit

Permalink
[nop] [ctx-filter] Drop obsolete ns-pattern concept
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Aug 29, 2023
1 parent 6b73d50 commit 23b4441
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/taoensso/encore/ctx_filter.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,36 @@

;;;; Filter

(let [ns-compile (fn [nsp] (enc/compile-str-filter (or nsp :any)))
ns-conform? (fn [nsp ns] ((ns-compile nsp) ns))
(let [ns-compile (fn [nsf] (enc/compile-str-filter (or nsf :any)))
ns-conform? (fn [nsf ns] ((ns-compile nsf) ns))
ns->min-level
(fn [spec ns]
;; Spec: [[<ns-pattern> <level>] ... [\"*\" <level>]]
;; Spec: [[<ns-filter> <level>] ... [\"*\" <level>]]
(enc/rsome
(fn [[ns-pattern level]]
(when (ns-conform? ns-pattern ns)
(fn [[nsf level]]
(when (ns-conform? nsf ns)
(valid-level-int level)))
spec))]

(defn valid-ns-pattern
"Returns valid ns-pattern, or throws."
(defn valid-ns-filter
"Returns valid ns-filter, or throws."
[x]
(if-let [t (enc/catching (do (ns-compile x) nil) t t)]
(throw
(ex-info "[encore/ctx-filter] Invalid namespace pattern"
{:ns-pattern {:value x, :type (type x)}}
(ex-info
(if (fn? x)
"[encore/ctx-filter] Invalid namespace filter (fn filters no longer supported)"
"[encore/ctx-filter] Invalid namespace filter")
{:ns-filter {:value x, :type (type x)}}
t))
x))

(defn ^:no-doc #?(:clj -filtered? :cljs ^boolean -filtered?)
"Low-level uncached util. Prefer `filtered?`."
[ns-pattern min-level ns level]
[ns-filter min-level ns level]
(or
;; Check ns
(if (nil? ns-pattern) false (if ^boolean (ns-conform? ns-pattern ns) false true))
(if (nil? ns-filter) false (if ^boolean (ns-conform? ns-filter ns) false true))

;; Check level
(if (nil? min-level)
Expand Down Expand Up @@ -128,48 +131,47 @@
(if (vector? x)
(do
(enc/run!
(fn [[ns-pattern level]]
(valid-ns-pattern ns-pattern)
(valid-level level))
(fn [[ns-filter level]]
(valid-ns-filter ns-filter)
(valid-level level))
x)
x)
(valid-level x)))

(defn update-min-level
"TODO Docstring
ns, old, new may be nil"
([old-level new-level] (update-min-level old-level nil new-level))
([old-level ns-pattern new-level]
(if-not ns-pattern
([old-level new-level] (update-min-level old-level nil new-level))
([old-level ns-filter new-level]
(if (nil? ns-filter)
(when new-level (valid-min-level new-level))

;; Update namespace-specific level
(let [new-level (when new-level (valid-level new-level))
ns-pattern (valid-ns-pattern ns-pattern)
(let [new-level (when new-level (valid-level new-level))
ns-filter (valid-ns-filter ns-filter)

old-vec (if (vector? old-level) old-level (if old-level [["*" (valid-level old-level)]] []))
new-vec
(reduce ; Remove any pre-existing [<ns> _] or [#{<ns>} _] entries
(fn [acc [ns-pattern* _level :as entry]]
(fn [acc [ns-filter* _level :as entry]]
(if-let [exact-match?
(or
(= ns-pattern* ns-pattern)
(= ns-pattern* #{ns-pattern}))]

(= ns-filter* ns-filter)
(= ns-filter* #{ns-filter}))]
(do acc) ; Remove entry
(conj acc entry) ; Retain entry
))

(if new-level
[[ns-pattern new-level]] ; Insert new-level entry at head
[[ns-filter new-level]] ; Insert new-level entry at head
[])

old-vec)]

(if-let [simplified ; [["*" :info]] -> :info
(when (= (count new-vec) 1)
(let [[[ns-pattern level]] new-vec]
(when (contains? #{"*" :any} ns-pattern)
(let [[[ns-filter level]] new-vec]
(when (contains? #{"*" :any} ns-filter)
level)))]
simplified
(not-empty new-vec))))))
Expand Down

0 comments on commit 23b4441

Please sign in to comment.