juxt / aero

A small library for explicit, intentful configuration.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reader literal to split a value

pmonks opened this issue · comments

I have a need to split (as per clojure.string/split) a single value (coming from #env) into a sequence, and implemented the following reader literal in my own code:

(defmethod aero.core/reader 'split
  [opts tag value]
  (let [[s re] value]
    (if (and s re)
      (s/split s (re-pattern re)))))

It seems to work well, and I thought it might be useful in aero core? Happy to prepare a PR if the authors are on board and that would be more helpful (or just copy this code directly - I'm happy either way).

Two disclaimers:

  1. I have no experience with ClojureScript so this solution may not work in that environment.
  2. I'm not sure if opening up the regex to the EDN layer might represent a risk (security, DoS, remote code execution, etc.). If so, it may be better to hardcode the regex inside the reader method (e.g. as "\\s*,\\s*" for comma delimited data), perhaps offering a fixed set of predefined regexes selectable via keywords in the second position of value?

I implemented sth. quite similar in context of mach (which is using aero). I vote for your feature request :-)

Bdw. maybe a filter for whitespace caused artifacts would be useful ...

(defn resolve-value [raw-path value-map]
  (let [path (if (cs/starts-with? raw-path "[")
                     (filter
                       (fn [element] (not (cs/blank? element)))
                       (cs/split raw-path #"[\[\]\s]"))
                     (vector raw-path))]
  (get-in value-map path)))

+1 for having a built-in #split tag, as these kinds of values often come from env variables. Would you mind if I prepare a PR?

@igrishaev not sure if your question is directed to me or the aero maintainers, but just to clarify that I would be very appreciative if you prepared a PR for this!