-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_component.clj
64 lines (53 loc) · 2.03 KB
/
config_component.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
(ns eduardoeof.config-component
(:require [com.stuartsierra.component :as component]
[cheshire.core :as json]
[clj-yaml.core :as yaml]
[clojure.string :as str]
[clojure.edn :as edn]))
(def ^:private format->parser {:json #(json/parse-string % true)
:yaml yaml/parse-string
:edn edn/read-string})
(defn- yml? [type]
(= type :yml))
(defn- get-file-format [file-name]
(let [splits (str/split file-name #"\.")]
(if (= 1 (count splits))
:unknown
(-> splits
last
keyword
((fn [format] (if (yml? format) :yaml format)))))))
(defn- file-format-not-supported-exception
[file-name format]
(ex-info "File format not supported"
{:exception-type ::file-format-not-supported-exception
:file-name file-name
:format format
:tip (str "Check if the file name has the format explicited (e.g. \".json\") "
"or it is in an unsupported format.")}))
(defn- parse [parser file-name]
(-> file-name
slurp
parser))
(defn- load-config [file-name]
(let [format (get-file-format file-name)]
(if-let [parser (format->parser format)]
(parse parser file-name)
(throw (file-format-not-supported-exception file-name
format)))))
(defrecord Config [file-name]
component/Lifecycle
(start [this]
"Returns a keyword map with all keys/values from the file in file-name. The file type
in file-name (e.g. json) defines the parser used by config-component"
(let [config (load-config file-name)]
(merge this config)))
(stop [this]
"Returns nil. To reload the file, will be necessary to recreate the component
with new-config function."
nil))
(defn new-config
"Returns a empty config-component with only file-name. Config file's keys/values are
loaded when called the component's start function."
[file-name]
(map->Config {:file-name file-name}))