Call functions read from EDN files

你说的曾经没有我的故事 提交于 2021-01-28 07:02:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!