Skip to content

Commit

Permalink
Merge pull request #17 from liquidz/dev
Browse files Browse the repository at this point in the history
Next release
  • Loading branch information
liquidz authored Dec 30, 2024
2 parents 38d435e + a76d88f commit a6125eb
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dev/analysis.edn

Large diffs are not rendered by default.

53 changes: 42 additions & 11 deletions src/elin/interceptor/tap.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,53 @@
(def ^:private tapped-atom-sym
(gensym "elin-tapped"))

(defn- convert-to-edn-compliant-data-fn-code
"Return a code for converting data to EDN-compliant data
cf. https://github.com/edn-format/edn?tab=readme-ov-file#built-in-elements"
[]
`(fn datafy# [x#]
(clojure.walk/prewalk
(fn [v#]
(cond
;; no conversion
(or
(nil? v#)
(boolean? v#)
(char? v#)
(symbol? v#)
(keyword? v#)
(number? v#)
(inst? v#)
(uuid? v#))
v#
;; vectors
(vector? v#)
(mapv datafy# v#)
;; maps
(map? v#)
(update-vals v# datafy#)
;; sets
(set? v#)
(set (map datafy# v#))
;; lists
(sequential? v#)
(map datafy# v#)

:else
(let [datafied# (clojure.core.protocols/datafy v#)]
(if (not= datafied# v#)
datafied#
(str v#)))))
x#)))

(defn- initialize-code
[{:keys [http-server-port max-store-size]}]
(str
`(do (in-ns '~ns-sym)
(refer-clojure)

(defn datafy#
[x#]
(clojure.walk/prewalk
(fn [v#]
(cond
(map? v#) (update-vals v# datafy#)
(list? v#) (map datafy# v#)
(vector? v#) (mapv datafy# v#)
:else (clojure.core.protocols/datafy v#)))
x#))
(def convert-to-edn-compliant-data#
~(convert-to-edn-compliant-data-fn-code))

(defn tap-handler-request#
[value#]
Expand All @@ -57,7 +88,7 @@

(defn ~tap-fn-sym
[x#]
(let [value# (datafy# x#)]
(let [value# (convert-to-edn-compliant-data# x#)]
(tap-handler-request# value#)
(when (<= ~max-store-size (count (deref ~tapped-atom-sym)))
(swap! ~tapped-atom-sym (fn [v#] (drop-last 1 v#))))
Expand Down
77 changes: 77 additions & 0 deletions test/elin/interceptor/tap_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(ns elin.interceptor.tap-test
(:require
[clojure.string :as str]
[clojure.test :as t]
[elin.interceptor.tap :as sut]
[elin.test-helper :as h]))

(t/use-fixtures :once h/malli-instrument-fixture)
(t/use-fixtures :once h/warn-log-level-fixture)

(defmacro define-convert-to-edn-compliant-data-fn []
(#'sut/convert-to-edn-compliant-data-fn-code))

(t/deftest convert-to-edn-compliant-data-test
;; https://github.com/edn-format/edn?tab=readme-ov-file#built-in-elements
(let [convert (define-convert-to-edn-compliant-data-fn)]
(t/testing "nil"
(t/is (= nil (convert nil))))

(t/testing "boolean"
(t/is (= true (convert true)))
(t/is (= false (convert false))))

(t/testing "strings"
(t/is (= "foo" (convert "foo"))))

(t/testing "characters"
(t/is (= \c (convert \c)))
(t/is (= \newline (convert \newline))))

(t/testing "symbols"
(t/is (= 'symbol (convert 'symbol)))
(t/is (= 'ns/symbol (convert 'ns/symbol))))

(t/testing "keywords"
(t/is (= :keyword (convert :keyword)))
(t/is (= :ns/keyword (convert :ns/keyword))))

(t/testing "integers"
(t/is (= 10 (convert 10)))
(t/is (= -10 (convert -10))))

(t/testing "floating point numbers"
(t/is (= 1.2 (convert 1.2)))
(t/is (= -1.2 (convert -1.2))))

(t/testing "lists"
(t/is (= '(1 "foo") (convert '(1 "foo"))))
(t/testing "lazy sequences"
(t/is (= '(1 2 3) (convert (map inc (range 3)))))))

(t/testing "vectors"
(t/is (= [1 "foo"] (convert [1 "foo"]))))

(t/testing "maps"
(t/is (= {:a 1 :b "foo"} (convert {:a 1 :b "foo"}))))

(t/testing "sets"
(t/is (= #{1 "foo"} (convert #{1 "foo"}))))

(t/testing "built-in tagged elements"
(t/testing "#inst"
(let [v #inst "1985-04-12T23:20:50.52Z"]
(t/is (= v (convert v)))))

(t/testing "#uuid"
(let [v #uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"]
(t/is (= v (convert v))))))

(t/testing "object"
(t/is (str/starts-with? (convert inc)
"clojure.core$inc@")
"Should be stringified"))

(t/testing "datafiable"
(t/testing "Exception"
(t/is (contains? (convert (Exception. "foo")) :via))))))

0 comments on commit a6125eb

Please sign in to comment.