-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: mv dynamic loading to
reflection
file
- Loading branch information
Showing
2 changed files
with
50 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
;; | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters