-
Notifications
You must be signed in to change notification settings - Fork 160
/
config.clj
64 lines (53 loc) · 1.84 KB
/
config.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
(ns crux.config
(:require [clojure.spec.alpha :as s]
[clojure.java.io :as io]
[clojure.edn :as edn]
[clojure.string :as str])
(:import java.util.Properties
(java.time Duration)
(java.util.concurrent TimeUnit)))
(s/def ::boolean
(s/and (s/conformer (fn [x] (cond (boolean? x) x, (string? x) (Boolean/parseBoolean x), :else x)))
boolean?))
(s/def ::string string?)
(s/def ::int
(s/and (s/conformer (fn [x] (cond (int? x) x, (string? x) (Integer/parseInt x), :else x)))
int?))
(s/def ::nat-int
(s/and (s/conformer (fn [x] (cond (nat-int? x) x, (string? x) (Integer/parseInt x), :else x)))
nat-int?))
(s/def ::string-map (s/map-of string? string?))
(s/def ::string-list (s/coll-of string?))
(s/def ::duration
(s/and (s/conformer (fn [d]
(cond
(instance? Duration d) d
(nat-int? d) (Duration/ofMillis d)
(string? d) (Duration/parse d))))
#(instance? Duration %)))
(s/def ::time-unit
(s/and (s/conformer (fn [t]
(cond
(instance? TimeUnit t) t
(string? t) (TimeUnit/valueOf (str/upper-case t)))))
#(instance? TimeUnit %)))
(s/def ::type
(s/conformer (fn [t]
(if (or (s/get-spec t) (s/spec? t))
t
any?))))
(s/def ::doc string?)
(s/def ::default any?)
(s/def ::required? boolean?)
(defn load-properties [f]
(with-open [rdr (io/reader f)]
(let [props (Properties.)]
(.load props rdr)
(into {}
(for [[k v] props]
[(keyword k) v])))))
(defn load-edn [f]
(with-open [rdr (io/reader f)]
(into {}
(for [[k v] (edn/read-string (slurp rdr))]
[(keyword k) v]))))