Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader literal to split a value #55

Open
pmonks opened this issue Oct 10, 2017 · 4 comments
Open

Reader literal to split a value #55

pmonks opened this issue Oct 10, 2017 · 4 comments

Comments

@pmonks
Copy link
Contributor

pmonks commented Oct 10, 2017

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?
@jerger
Copy link

jerger commented Jun 4, 2018

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

@igrishaev
Copy link

+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?

@pmonks
Copy link
Contributor Author

pmonks commented Jun 1, 2020

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

@igrishaev
Copy link

@pmonks #93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants