clojureenviron

avoiding repetion while declaring maps


I'm setting up config map using environ for fetching up env variables. Since environ normalizes upper case to lowercase and "_" characters to "-", I ended up with repetitions of keywords

(def config {:consumer-key (env :consumer-key)
             :keystore-password (env :consumer-key)
             :ssl-keystore-password (env :ssl-keystore-password)
             :ssl-certificate-name (env :ssl-certificate-name)
             :callback-url (env :callback-url)
             :mp-private-key (env :mp-private-key)
             :merchant-checkout-id (env :merchant-checkout-id)
             :is-sandbox (env :is-sandbox)})

is there a way to prevent this repetition? maybe a function which receives a keyword and returns some kind of key value pair for the map?


Solution

  • As mentioned in the comments, since env is a map you can just use select-keys with a list of keys to copy:

    (def config
      (select-keys env [:consumer-key :is-sandbox
                        :keystore-password :ssl-keystore-password :ssl-certificate-name
                        :callback-url :mp-private-key :merchant-checkout-id]))
    

    Alan Thompson's approach is reasonable if you have an arbitrary function rather than specifically a map.