问题描述
我有一个EDN配置文件,其中的条目引用了现有功能,例如:
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}}
使用clojure.edn/read-edn这些条目被读为Symbols,但是我希望能够在运行时调用它们.这样做的目的是为用户提供一种提供自己的功能的方法.
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.
我该如何实现?
推荐答案
您可以使用 解决
.
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])))
您可能需要限制用户可访问的允许符号集,或者对提供edn的用户高度信任,以防止他们在您不希望的运行环境中执行任何功能他们可以访问.
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.
这篇关于从EDN文件读取的调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!