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

feat: env vars as vectors #33

Merged
merged 11 commits into from
Apr 8, 2024
Merged

feat: env vars as vectors #33

merged 11 commits into from
Apr 8, 2024

Conversation

kdmoreira
Copy link
Contributor

@kdmoreira kdmoreira commented Apr 5, 2024

This PR enables configurations coming from environment variables that represent multiple values to be stored as a vector in config.edn, which is not supported by aero config as of now. An example of such variable:
ALLOWED_ORIGINS="https://domain.a.com, https://domain.b.com"

It's expected that env variables are comma-separated strings.
Their configs should have a #csv tag literal.
Trailing commas and whitespaces are handled.

@rafaeldelboni
Copy link
Member

rafaeldelboni commented Apr 6, 2024

Nice catch, that's definitely something I didn't thought when doing this hahaha
Thanks for your contribution!

I think since the config component is using aero under the hood, we can simplify your code a little bit using tag literals.

I did a small POC to check the idea to help you:

(ns codes.clj.docs.backend.config
  (:require [aero.core :as aero]
            [clojure.string :as string]))

(defn- str-var->vector-var
  "Converts a string config variable to a vector of strings, when applicable.
  Environment variables are expected to be set as comma-separated values."
  [value]
  (if (string? value)
    (let [split-configs (-> value
                            (string/split  #","))
          env-config (->> split-configs
                          (map string/trim)
                          (remove empty?))]
      env-config)
    value))

(defmethod aero/reader 'csv
  [opts tag value]
  (prn opts tag value)
  (str-var->vector-var value))

(aero/read-config "config.edn")

Where the config.edn is something like this:

{:debug #boolean #or [#env DEBUG "true"]
 :webserver {:port #long #or [#env PORT 8080]
             :factor #double #or [#env FACTOR "1.2"]
             :mode #keyword #or [#env MODE "my-mode"]
             :origins #csv #or [#env ORIGINS "my, domain"]}}

With this we don't need to wrap the config component in a function just remember to require the codes.clj.docs.backend.config before starting the component.

We can do this PR here, evaluate and later we can move this to the config component in parenthesin/components

WDYT?

@kdmoreira
Copy link
Contributor Author

Good suggestion, @rafaeldelboni, I agree this makes things simpler.
I've applied it and changed the tests to reflect the new approach.
Having this in parenthesin's components eventually would be nice, indeed.
Thanks for the improvement.

@rafaeldelboni rafaeldelboni merged commit 740f6cf into main Apr 8, 2024
4 checks passed
@rafaeldelboni rafaeldelboni deleted the feat/env-vars branch April 8, 2024 21:15
@rafaeldelboni
Copy link
Member

🔝 Thanks!

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 this pull request may close these issues.

2 participants