问题
I have an EDN configuration file in which the entries refer to existing functions, e.g.:
:attribute-modules {:content {:class lohan.extractors.content/process}
:schema {:class lohan.extractors.schema/process}
:label {:class lohan.extractors.label/process}
:user {:class lohan.extractors.user/process}
:env {:class lohan.extractors.env/process}}
Using clojure.edn/read-edn these entries are read as Symbols, but I want to be able to call them at runtime. The purpose of this is to provide a way for the user to supply his own set of functions.
How can I achieve this?
回答1:
You can invoke a function held in a var referenced by a Symbol by using resolve.
For example, if you wanted to invoke +
by using its Symbol you can use:
((resolve '+) 1 2)
;=> 3
Therefore, using your example you can do:
((resolve (get-in (clojure.edn/read-string "{:content {:class ohan.extractors.content/process}
:schema {:class lohan.extractors.schema/process}
:label {:class lohan.extractors.label/process}
:user {:class lohan.extractors.user/process}
:env {:class lohan.extractors.env/process}}")
[:content :class])))
You would either need to limit the set of allowed symbols accessible to users or have a high level of trust in the users that are providing the edn in order to prevent them from executing any function in the running environment that you do not wish them to have access to.
来源:https://stackoverflow.com/questions/27920520/call-functions-read-from-edn-files