clojure - 调用从 EDN 文件中读取的函数

标签 clojure edn

我有一个 EDN 配置文件,其中的条目引用现有函数,例如:

: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,这些条目被读取为符号,但我希望能够在运行时调用它们。这样做的目的是为用户提供一种方式来提供他自己的一套功能。

我怎样才能做到这一点?

最佳答案

您可以使用 resolve 调用由 Symbol 引用的 var 中保存的函数.

例如,如果你想通过使用它的 Symbol 来调用 +,你可以使用:

((resolve '+) 1 2)
;=> 3

因此,使用您的示例,您可以:

((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 的用户,以防止他们在运行环境中执行您不希望的任何功能他们可以访问。

关于clojure - 调用从 EDN 文件中读取的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27920520/

相关文章:

clojure - 如何将Java字符串转换为EDN对象?

function - 我有一个包含 3 个函数的 Clojure seq。为什么 (rest my-seq) 给出 "cannot be cast as"异常?

macros - 口齿不清/克洛朱尔 : Is it a good idea to write function generating macros?

clojure - 为什么要将 ClojureScript 源目录放在 :source-paths? 中

Clojure 命名空间将 - 转换为 _

xml - 如何在 Clojure 中将 XML 转换为 edn?

clojure - yogthos/config 在 CIDER 中获取我的环境,但在 lein with-profile 中运行时则不然

parsing - Clojure - 解析 Elasticsearch 查询响应并提取值

python - 使用 :symbols 从 Python 生成 Clojure EDN

macros - 如何向类 defn 的 Clojure 宏添加文档字符串支持?