I have a number of namespaces each of which contains a function with the same name, e.g.:
(ns myns.resourceX
...)
(defn create
(println "resourceX/create"))
(ns test)
(myns.resourceX/create)
(myns.resourceY/create)
(You can imagine having resourceX, resourceY, resourceZ, etc. The actual create functions end up sending HTTP POSTs and returning the response, but this does not matter here.)
Now, in another namespace, I would like to define a function that takes two arguments: an array of resource names (i.e., namespace name) and function name, e.g.:
(defn do-verb
[verb res-type]
(??))
So I can write:
(do-verb :create :resourceX)
to the same effect as:
(myns.resourceX/create)
One thing I tried is using ns-resolve, e.g.:
(defn do-verb
[verb res-type & params]
(apply (ns-resolve
(symbol (clojure.string/join ["myns." (name res-type)]))
(symbol (name verb)))
params))
But I am unsure about using ns-resolve -- seems like a hack.
Another possibility I have explored is defining a map to associate symbols to functions:
(def convert-fns
{:resourceX {:create resourceX/create}
:resourceY {:create resourceY/create}
...})
(defn do-verb [verb res-type & params]
(apply (get-in convert-fns [res-type verb]) params))
But this has the downside, for me, of requiring to modify convert-fns each time a new resource is added.
Are there any alternative approaches to these?