【问题标题】:Call functions read from EDN files调用从 EDN 文件中读取的函数
【发布时间】:2015-03-11 07:31:00
【问题描述】:

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

我怎样才能做到这一点?

【问题讨论】:

    标签: clojure edn


    【解决方案1】:

    您可以使用resolve 调用符号引用的var 中保存的函数。

    例如,如果您想通过使用其符号来调用+,您可以使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      相关资源
      最近更新 更多