aero icon indicating copy to clipboard operation
aero copied to clipboard

Reader literal to split a value

Open pmonks opened this issue 7 years ago • 4 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?

pmonks avatar Oct 10 '17 23:10 pmonks

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)))

jerger avatar Jun 04 '18 08:06 jerger

+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 avatar Jun 01 '20 08:06 igrishaev

@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!

pmonks avatar Jun 01 '20 20:06 pmonks

@pmonks https://github.com/juxt/aero/pull/93

igrishaev avatar Jun 02 '20 08:06 igrishaev