Search code examples
clojureedn

Call functions read from EDN files


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?


Solution

  • 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.