Created
October 19, 2016 21:30
-
-
Save rbxbx/f93a540195c6ec786ec8946d7bfc2aaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns foobar.config) | |
(defn request? | |
"True if the supplied value is a 'valid' ring request map. | |
We assume validity based off of attributes for a minimal ring request from | |
https://ring-clojure.github.io/ring-mock/ring.mock.request.html#var-request" | |
[m] | |
(and (contains? m :server-port) | |
(integer? (:server-port m)) | |
(contains? m :scheme) | |
(contains? m :uri) | |
(contains? m :headers))) | |
(defn getx | |
"Like two-argument get, but throws an exception if the key is | |
not found. Stolen from simulant.util | |
https://github.com/Datomic/simulant/blob/master/src/simulant/util.clj" | |
[m k] | |
(let [e (get m k ::sentinel)] | |
(if-not (= e ::sentinel) | |
e | |
(throw (ex-info "Missing required key" {:map m :key k}))))) | |
(defn getx-in | |
"Like get-in but will throw an exception if any key is not found" | |
[m ks] | |
(reduce getx m ks)) | |
(defn safe-get-in-req-or-map | |
"Like get but will look inside of the :config map if m is a ring request. | |
By convention, we inject our config into every request via middleware." | |
[m k] | |
(if (request? m) | |
(getx-in m [:config k]) | |
(getx m k))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; default accessor creation ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(defn- create-safe-accessor-fn | |
"Creates a safe accessor fn named the same as :config-key which will look | |
up :config-key in a map with `safe-get-in-req-or-map`. | |
Not for human usage." | |
[config-key] | |
`(defn ~(symbol (name config-key)) [m#] | |
(safe-get-in-req-or-map m# ~config-key))) | |
(def default-accessors | |
'[:aws-client-opts | |
:gamma-events-stream | |
:fanout-events-stream | |
:events-json-stream | |
:telemetry-json-stream | |
:end-users-dynamo-table | |
:end-user-badges-dynamo-table | |
:end-user-prefs-table | |
:end-user-devices-table | |
:end-user-tags-table | |
:app-preferences-table]) | |
(defmacro ^:private gen-default-accessor-fns! | |
"When invoked this will define `default-accessor` functions in the namespace. | |
These functions are in the form of: | |
``` | |
(defn aws-client-opts [m] | |
(safe-get-in-req-or-map m :aws-client-opts)) | |
``` | |
See: create-safe-accessor-fn | |
" | |
[] | |
`(do | |
~@(clojure.core/map create-safe-accessor-fn default-accessors))) | |
(gen-default-accessor-fns!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment