avoiding repetion while declaring maps

98 views Asked by At

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?

2

There are 2 answers

3
amalloy On BEST ANSWER

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.

0
Alan Thompson On

Here is one way to do it by defining a helper function:

(def env {:consumer-key   1
          :ssl-key        2
          :mp-private-key 3})

(def key-list (keys env))

(defn extract-from
  [src-fn keys]
  (into (sorted-map)
    (for [key keys]
      {key (src-fn key)} )))

(println "result:" (extract-from env key-list))

=> result: {:consumer-key 1, :mp-private-key 3, :ssl-key 2}

Note that for testing purposes I used a trick and substituted a map env for the function env from the environ library. This works since a map can act like a function when looking up its keys. It will still work for an actual function like environ.core/env.