-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusage.clj
66 lines (47 loc) · 2.13 KB
/
usage.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
(use 'oroboros.core)
;; by using simple template strings we can enjoy extremely flexible configuration
;; == examples/simple/config.yaml ==
;;
;; cat: tom
;; mouse: jerry
;; name: '{{ cat }} & {{ mouse }}'
;; best: '{{ favorite }}'
(load-config "examples/simple")
;; => {:cat "tom", :mouse "jerry", :name "tom & jerry", :best "{{favorite}}"}
;; name is templated, but since best is undefined it is ignored
;; we can change the context and also include files named 'tom' or 'jerry'
;; these files are allowed to provide overrides to be merged into the config
(load-config "examples/simple" "tom")
;; => {:favorite "tom", :cat "tom", :mouse "jerry", :name "tom & jerry", :best "tom"}
(load-config "examples/simple" "jerry")
;; => {:favorite "jerry", :cat "tom", :mouse "jerry", :name "tom & jerry", :best "jerry"}
;; load-config supports local template references within a single config file
;; load-config also supports global references across namespaces
;; $ tree examples/advanced
;; examples/advanced/
;; ├── config.yaml
;; ├── db
;; │ ├── config.yaml
;; │ └── production.yaml
;; └── web
;; ├── config.yaml
;; └── production.yaml
(load-config "examples/advanced")
;; => {:web {:port 1337, :protocol "http", :host "web.example.com:1337",
;; :api "http://web.example.com:1337/v/1.2.3",
;; :command "./bin/start --db db.example.com" },
;; :db {:host "db.example.com"},
;; :version "1.2.3"}
;; this becomes considerable when using templates across environment & app contexts
(load-config "examples/advanced" "production")
;; => {:web {:port 1337, :protocol "https", :host "expensive-server.example.com",
;; :api "https://expensive-server.example.com/v/1.2.3",
;; :command "./bin/start --db prod-db.example.com" },
;; :db {:host "prod-db.example.com"},
;; :version "1.2.3"}
;; in code, configuration is live and behaves like a regular map
(def config (load-config "examples/advanced" "production"))
(-> config
(assoc-in [:version] "1.2.4")
(get-in [:web :api]))
;; => "https://expensive-server.example.com/v/1.2.3"