-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_utils.clj
98 lines (84 loc) · 2.45 KB
/
core_utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(ns b12n.swiza.commons.core-utils
(:require
[me.raynes.fs :as fs :refer [expand-home normalized]]
[aero.core :refer [read-config]]
[clojure.java.io :as io]
[jsonista.core :refer [read-value write-value object-mapper]]))
(defn expand-path
[filename]
(-> filename
fs/expand-home
fs/normalized
str))
(defn load-edn-config
"Load the edn config from a given file."
[config]
(read-config (expand-path config)))
(defn remove-nil
"Remove nil value from a given map"
[input-map]
(reduce (fn [m [k v]]
(if (nil? v)
m
(assoc m k v)))
{}
input-map))
(defn config-for-env
"Get a base config for a given env.
Example:
(config-for-env \"./config.edn\" :test)
or
(config-for-env \"./config.edn\" \"test\")"
[config env]
(let [{:keys [url token]}
(select-keys ((load-edn-config config) (keyword env))
[:url :token])]
{:url url
:options {:headers {:authorization token}
:accept :json, :content-type :json}}))
(defn ^:private token-for-env
"Token for a given env.
(token-for-env base-config :test)"
[config env]
(-> (config-for-env config env)
:options
:headers
:authorization))
(defn default-options
[config env & [opts]]
(let [default-opts {:timeout 1000 ;; ms
:oauth-token (token-for-env config env)}]
(if opts
(merge default-opts opts)
default-opts)))
(defn assoc-some
;; From weavejester/medley
"Associates a key with a value in a map, if and only if the value is not nil."
([m k v]
(if (nil? v) m (assoc m k v)))
([m k v & kvs]
(reduce (fn [m [k v]] (assoc-some m k v))
(assoc-some m k v)
(partition 2 kvs))))
(defn write-json
"Write data as JSON to file"
[file data]
(write-value (io/file file) data (object-mapper {:pretty true})))
(defn read-json
"Read data as JSON from string or file"
[file]
(read-value file (object-mapper {:decode-key-fn true})))
(defn map-keys
"Given a function and a map, returns the map resulting from applying
the function to each key.
e.g. (map-keys name {:a 1 :b 2 :c 3}) ;;=> {\"a\" 1, \"b\" 2, \"c\" 3}
"
[f m]
(zipmap (map f (keys m)) (vals m)))
(defn map-vals
"Given a function and a map, returns the map resulting from applying
the function to each value.
e.g. (map-vals inc {:a 1 :b 2 :c 3}) ;;=> {:a 2, :b 3, :c 4}
"
[f m]
(zipmap (keys m) (map f (vals m))))