Skip to content

Commit

Permalink
refactor: mv dynamic loading to reflection file
Browse files Browse the repository at this point in the history
  • Loading branch information
J0sueTM committed Aug 7, 2024
1 parent 47ff1dc commit bec5f04
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
50 changes: 50 additions & 0 deletions src/com/moclojer/internal/reflection.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns com.moclojer.internal.reflection
(:require
[camel-snake-kebab.core :as csk]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]))

(defn unpack-parameter
[parameter]
{:type (-> (.. parameter getType getName)
(str/split #"\.")
(last)
(csk/->kebab-case))
:name (csk/->kebab-case (.getName parameter))})

(defn unpack-method
[method]
{:name (csk/->kebab-case (.getName method))
:parameters (map unpack-parameter (.getParameters method))})

(defn reduce-method-overloads
[methods]
(reduce
(fn [overloaded-methods {:keys [name parameters]}]
(let [overload-count (count
(filter
#(str/starts-with? (key %) name)
overloaded-methods))
cur-overload-id (when (> overload-count 0) overload-count)]
(assoc overloaded-methods (str name cur-overload-id) parameters)))
{} methods))

(defn get-klazz-methods
[klazz allowlist]
(->> (.getMethods klazz)
(map unpack-method)
(filter #(contains? allowlist (:name %)))
(reduce-method-overloads)))

(def ^:private jedis-cmd-allowlist
(-> (io/resource "command-allowlist.edn")
(slurp)
(edn/read-string)))

(comment
(get-klazz-methods
redis.clients.jedis.JedisPooled
jedis-cmd-allowlist)
;;
)
38 changes: 0 additions & 38 deletions src/com/moclojer/rq.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,3 @@
"Disconnect and close redis client"
([] (close-client *redis-pool*))
([client] (.close @client)))

(comment
;; (import [java.util Arrays])
(require '[clojure.string :as str]
'[clojure.java.io :as io]
'[clojure.edn :as edn]
'[camel-snake-kebab.core :as csk])
(import [redis.clients.jedis JedisPooled])

(def allowlist
(-> (io/resource "command-allowlist.edn")
(slurp)
(edn/read-string)))

(->> (.getMethods JedisPooled)
(map
(fn [class]
{:name (csk/->kebab-case (.getName class))
:parameters (map
#(identity
{:type (-> (.. % getType getName)
(str/split #"\.")
(last)
(csk/->kebab-case))
:name (csk/->kebab-case (.getName %))})
(.getParameters class))}))
(filter #(contains? allowlist (:name %)))
(reduce
(fn [methods {:keys [name parameters]}]
(let [overload-count (count
(filter
#(str/starts-with? (key %) name)
methods))
cur-overload-id (when (> overload-count 0) overload-count)]
(assoc methods (str name cur-overload-id) parameters)))
{}))
;;
)

0 comments on commit bec5f04

Please sign in to comment.