-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoverview.edoc
More file actions
187 lines (146 loc) · 6.38 KB
/
overview.edoc
File metadata and controls
187 lines (146 loc) · 6.38 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
@author Gregoire Lejeune <gregoire.lejeune@botsunit.com>
@copyright 2015, 2016 Bots Unit
@version 1.0.0
@title Dōteki
@doc
[](https://hex.pm/packages/doteki)
[](https://hex.pm/packages/doteki)
[](https://hex.pm/packages/doteki)
<h2>About</h2>
<b>Dōteki</b> allow you to use dynamic configuration in your erlang application.
<h2>Using environment variables</h2>
In your configuration file, you can declare an explicit usage of en environment variable.
To do so, you can use and atom, prefixed by <tt>env.</tt> :
Erlang :
<pre>
[
{app, [
{key, 'env.ENV_VAR'}
]}
].
</pre>
Elixir :
<pre>
use Mix.Config
config :app,
key: :"env.ENV_VAR"
</pre>
You can also use a tuple where the first element is the atom <tt>system</tt> or <tt>env</tt>,
the second element is a string giving the environment variable name and the third element is
the default value to use if the environment variable is not set. The third element is optional
and if the environment variable is not set, Doteki will return <tt>undefined</tt>.
Erlang :
<pre>
[
{app, [
{key, {system, "ENV_VAR"}}
]}
].
</pre>
Elixir :
<pre>
use Mix.Config
config :app,
key: {:system, "ENV_VAR"}
</pre>
With those notations, when you get the value for <tt>[app, key]</tt>, Dōteki will return the value of the environment variable <tt>ENV_VAR</tt>.
Erlang :
<pre>
export ENV_VAR=123
1> doteki:get_env([app, key]).
% => 123
</pre>
Elixir :
<pre>
export ENV_VAR=123
iex(1)> Doteki.get_env([:app, :key])
# => 123
</pre>
You can also overwrite every value in the configuration, using an environment variable. Example: if you have the following configuration :
Erlang :
<pre>
[
{app, [
{key1, 'env.ENV_VAR'},
{key2, "hello world"}
]}
].
</pre>
Elixir :
<pre>
use Mix.Config
config :app,
key1: :"env.ENV_VAR",
key2: "hello world"
</pre>
If you set an environment variable <tt>APP_KEY2</tt> and get the value for <tt>[app, key2]</tt> Dōteki will return defined by <tt>APP_KEY2</tt>.
<b>WARNING</b> : The environment variables overwriting a key by its path are interpreted before those declares in the configuration. So if you export <tt>APP_KEY1</tt>, the value returned for <tt>[app, key1]</tt> will be the value exported for this variable.
When Dōteki find an environment it will try to interpret it.
<table width="100%" border="0">
<tr><th>Type</th><th>export</th><th>Erlang</th><th>Elixir</th></tr>
<tr><td>Atom</td><td><tt>"hello"</tt><br /><tt>"hello:atom"</tt><br /><tt>"hello:term"</tt></td><td><tt>hello</tt></td><td><tt>:hello</tt></td></tr>
<tr><td>String</td><td><tt>"\"hello world\""</tt><br /><tt>"hello world:string"</tt></td><td><tt>"hello world"</tt></td><td><tt>'hello world'</tt></td></tr>
<tr><td>Binary</td><td><tt>"<<\"hola mundo\">>"</tt><br /><tt>"hola mundo:binary"</tt></td><td><tt><<"hola mundo">></tt></td><td><tt>"hola mundo"</tt></td></tr>
<tr><td>Integer</td><td><tt>"123"</tt><br /><tt>"123:integer"</tt></td><td><tt>123</tt></td><td><tt>123</tt></td></tr>
<tr><td>Float</td><td><tt>"123.45"</tt><br /><tt>"123.45:integer"</tt></td><td><tt>123.45</tt></td><td><tt>123.45</tt></td></tr>
<tr><td>Tuple</td><td><tt>"{a, \"b\", 123}"</tt><br /><tt>"{a, \"b\", 123}:term"</tt></td><td><tt>{a, "b", 123}</tt></td><td><tt>{:a, 'b', 123}</tt></td></tr>
<tr><td>List</td><td><tt>"[a, \"b\", 123]"</tt><br /><tt>"[a, \"b\", 123]:term"</tt></td><td><tt>[a, "b", 123]</tt></td><td><tt>[:a, 'b', 123]</tt></td></tr>
</table>
In some case, you can want to force Dōteki to interpret a value. For example, if you declare an environment variable <tt>MY_NUMBER</tt> and want it to be interpreted as a string, you can use the following notation :
Erlang :
<pre>
{system, "MY_NUMBER", as, string}
</pre>
Elixir :
<pre>
{:system, "MY_NUMBER", :as, :string}
</pre>
You can also add a default value in case where <tt>MY_NUMBER</tt> is not set :
Erlang :
<pre>
{system, "MY_NUMBER", as, string, "1234"}
</pre>
Elixir :
<pre>
{:system, "MY_NUMBER", :as, :string, '1234'}
</pre>
<h2>Using functions</h2>
If you want to use a function, you can use those notations :
<ul>
<li><tt>{fn, Module, Function}</tt></li>
<li><tt>{fn, Module, Function, [Args]}</tt></li>
<li><tt>{fn, Module, Function, Arity, [Args]}</tt></li>
<li><tt>{'fun', Module, Function}</tt></li>
<li><tt>{'fun', Module, Function, [Args]}</tt></li>
<li><tt>{'fun', Module, Function, Arity, [Args]}</tt></li>
<li><tt>'fun.Module:Function'</tt></li>
<li><tt>{'fun.Module:Function', [Args]}</tt></li>
<li><tt>{'fun.Module:Function/Arity', [Args]}</tt></li>
<li><tt>'fn.Module:Function'</tt></li>
<li><tt>{'fn.Module:Function', [Args]}</tt></li>
<li><tt>{'fn.Module:Function/Arity', [Args]}</tt></li>
</ul>
Erlang :
<pre>
[
{app, [
{key, {fn, module, function, 2, [{system, "ENV_VAR"}, 2]}}
]}
].
</pre>
Elixir :
<pre>
use Mix.Config
config :app,
key: {:fn, Module, :function, 2, [{:system, "ENV_VAR"}, 2]}
</pre>
<h2>Licence</h2>
<p>Copyright (c) 2015, 2016, Bots Unit<br />
All rights reserved.</p>
<p>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
<ol>
<li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
</ol>
<p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
@end