-
Notifications
You must be signed in to change notification settings - Fork 1
/
配置文件.html
95 lines (92 loc) · 9.98 KB
/
配置文件.html
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
<!DOCTYPE html><html><head><meta charset="utf-8"><link rel="stylesheet" type="text/css" href="./css/github_new.css"><link rel="shortcut icon" href="favicon.png" type="image/x-icon"><!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-160306868-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-160306868-1');
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5809313774160994"
crossorigin="anonymous"></script>
<title>配置文件</title></head><body><h1>配置文件</h1>
<h2>validators</h2>
<p>在<code class="notranslate">schema</code>文件中可以为<code class="notranslate">mapping</code>编写<code class="notranslate">validator</code>函数,去执行一些更高级的校验。</p>
<p>例子1:</p>
<p>限制某个配置的取值范围</p>
<div class="highlight highlight-source-erlang"><pre class="notranslate">{<span class="pl-c1">mapping</span>, <span class="pl-s"><span class="pl-pds">"</span>platform_len_limit<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>msync.platform_len_limit<span class="pl-pds">"</span></span>, [
{<span class="pl-c1">default</span>, <span class="pl-c1">10</span>},
{<span class="pl-c1">datatype</span>, <span class="pl-c1">integer</span>},
{<span class="pl-c1">validators</span>, [<span class="pl-s"><span class="pl-pds">"</span>>=0<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span><=50<span class="pl-pds">"</span></span>]}
]}.
{<span class="pl-c1">validator</span>, <span class="pl-s"><span class="pl-pds">"</span>>=0<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>must be at least 0<span class="pl-pds">"</span></span>,
<span class="pl-k">fun</span>(<span class="pl-smi">V</span>) ->
<span class="pl-smi">V</span> <span class="pl-k">>=</span> <span class="pl-c1">0</span>
<span class="pl-k">end</span>}.
{<span class="pl-c1">validator</span>, <span class="pl-s"><span class="pl-pds">"</span><=50<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>50 and larger are not supported<span class="pl-pds">"</span></span>,
<span class="pl-k">fun</span>(<span class="pl-smi">V</span>) ->
<span class="pl-smi">V</span> <span class="pl-k">=<</span> <span class="pl-c1">50</span>
<span class="pl-k">end</span>}.</pre></div>
<p>如果把<code class="notranslate">platform_len_limit</code>的值设置为<code class="notranslate">-1</code>,生成配置文件会打印如下报错:<br>
<a target="_blank" rel="noopener noreferrer" href="../img/20230919/1.png"><img src="../img/20230919/1.png" alt="" style="max-width: 100%;"></a></p>
<p>例子2:</p>
<p>检查某个配置必须在<code class="notranslate">conf</code>文件配置</p>
<div class="highlight highlight-source-erlang"><pre class="notranslate">{<span class="pl-c1">mapping</span>, <span class="pl-s"><span class="pl-pds">"</span>node_type<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>msync.node_type<span class="pl-pds">"</span></span>, [
{<span class="pl-c1">default</span>, <span class="pl-c1">none</span>},
{<span class="pl-c1">datatype</span>, <span class="pl-c1">atom</span>},
{<span class="pl-c1">validators</span>, [<span class="pl-s"><span class="pl-pds">"</span>must_config<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>node_type<span class="pl-pds">"</span></span>]}
]}.
{<span class="pl-c1">validator</span>, <span class="pl-s"><span class="pl-pds">"</span>must_config<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>must be set in the config file<span class="pl-pds">"</span></span>,
<span class="pl-k">fun</span>(<span class="pl-smi">V</span>) ->
<span class="pl-smi">V</span> <span class="pl-k">=/=</span> <span class="pl-c1">none</span>
<span class="pl-k">end</span>}.</pre></div>
<p>如果在<code class="notranslate">conf</code>文件中没有配置,会打印如下报错:<br>
<a target="_blank" rel="noopener noreferrer" href="../img/20230919/2.png"><img src="../img/20230919/2.png" alt="" style="max-width: 100%;"></a></p>
<p>例子3:</p>
<p>检查一组配置是否都有配置</p>
<div class="highlight highlight-source-erlang"><pre class="notranslate"><span class="pl-c"><span class="pl-c">%</span>% easemob queues</span>
{<span class="pl-c1">mapping</span>, <span class="pl-s"><span class="pl-pds">"</span>easemob_queues<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>msync.easemob_queues<span class="pl-pds">"</span></span>, [
{<span class="pl-c1">default</span>, <span class="pl-s"><span class="pl-pds">"</span>queue_channel,queue_group_channel,queue_offline,queue_presence_session,queue_reaction,queue_ets<span class="pl-pds">"</span></span>},
{<span class="pl-c1">datatype</span>, <span class="pl-c1">string</span>}
]}.
{ <span class="pl-c1">translation</span>,
<span class="pl-s"><span class="pl-pds">"</span>msync.easemob_queues<span class="pl-pds">"</span></span>,
<span class="pl-k">fun</span>(<span class="pl-smi">Conf</span>) ->
<span class="pl-smi">AllQueues</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish</span>:<span class="pl-en">conf_get</span>(<span class="pl-s"><span class="pl-pds">"</span>easemob_queues<span class="pl-pds">"</span></span>, <span class="pl-smi">Conf</span>),
<span class="pl-smi">Queues</span> <span class="pl-k">=</span> <span class="pl-en">string</span>:<span class="pl-en">tokens</span>(<span class="pl-smi">AllQueues</span>, <span class="pl-s"><span class="pl-pds">"</span>,<span class="pl-pds">"</span></span>),
<span class="pl-en">lists</span>:<span class="pl-en">foreach</span>(<span class="pl-k">fun</span>(<span class="pl-smi">Queue</span>) ->
<span class="pl-en">cuttlefish</span>:<span class="pl-en">conf_get</span>(<span class="pl-smi">Queue</span> <span class="pl-k">++</span> <span class="pl-s"><span class="pl-pds">"</span>.pool_size<span class="pl-pds">"</span></span>, <span class="pl-smi">Conf</span>)
<span class="pl-k">end</span>, <span class="pl-smi">Queues</span>),
[<span class="pl-en">erlang</span>:<span class="pl-en">list_to_atom</span>(<span class="pl-smi">Queue</span>) || <span class="pl-smi">Queue</span> <span class="pl-k"><-</span> <span class="pl-en">string</span>:<span class="pl-en">tokens</span>(<span class="pl-smi">AllQueues</span>, <span class="pl-s"><span class="pl-pds">"</span>,<span class="pl-pds">"</span></span>)]
<span class="pl-k">end</span>
}.</pre></div>
<p>如果<code class="notranslate">easemob_queues</code> 依赖的 <code class="notranslate">pool_size</code> 没有配置,会打印如下报错:<br>
<a target="_blank" rel="noopener noreferrer" href="../img/20230919/3.png"><img src="../img/20230919/3.png" alt="" style="max-width: 100%;"></a></p>
<h3>一些注意事项</h3>
<ul>
<li><code class="notranslate">validator</code>函数仅有一个参数</li>
<li><code class="notranslate">validator</code>函数在一个schema文件定义后,在所有schema文件都可以使用</li>
<li>不要在多个<code class="notranslate">schema</code>文件中重复定义配置</li>
</ul>
<h2>单元测试</h2>
<div class="highlight highlight-source-erlang"><pre class="notranslate"> <span class="pl-c"><span class="pl-c">%</span>% 仅通过schema文件生成配置</span>
<span class="pl-smi">Config</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish_unit</span>:<span class="pl-en">generate_templated_config</span>(
<span class="pl-s"><span class="pl-pds">"</span>../../lib/msync/priv/msync.schema<span class="pl-pds">"</span></span>, [], []),
<span class="pl-c"><span class="pl-c">%</span>% assert validator 设置的错误信息</span>
<span class="pl-c1">ok</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish_unit</span>:<span class="pl-en">assert_error_message</span>(<span class="pl-smi">Config</span>, <span class="pl-s"><span class="pl-pds">"</span>platform_len_limit invalid, must be at least 0<span class="pl-pds">"</span></span>).
</pre></div>
<div class="highlight highlight-source-erlang"><pre class="notranslate"> <span class="pl-c"><span class="pl-c">%</span>% 通过 config 和 schema 共同生成配置</span>
<span class="pl-smi">LocalConfig</span> <span class="pl-k">=</span> [{[<span class="pl-s"><span class="pl-pds">"</span>platform_len_limit<span class="pl-pds">"</span></span>], <span class="pl-c1">10</span>}],
<span class="pl-smi">Config</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish_unit</span>:<span class="pl-en">generate_templated_config</span>(
<span class="pl-s"><span class="pl-pds">"</span>../../lib/msync/priv/msync.schema<span class="pl-pds">"</span></span>, <span class="pl-smi">LocalConfig</span>, []),
<span class="pl-c"><span class="pl-c">%</span>% assert 生成的配置是否符合预期</span>
<span class="pl-c1">ok</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish_unit</span>:<span class="pl-en">assert_config</span>(<span class="pl-smi">Config</span>, <span class="pl-s"><span class="pl-pds">"</span>msync.platform_len_limit<span class="pl-pds">"</span></span>, <span class="pl-c1">10</span>),
<span class="pl-c"><span class="pl-c">%</span>% assert 是否没有生成此配置</span>
<span class="pl-c1">ok</span> <span class="pl-k">=</span> <span class="pl-en">cuttlefish_unit</span>:<span class="pl-en">assert_not_configured</span>(<span class="pl-smi">Config</span>, <span class="pl-s"><span class="pl-pds">"</span>msync.roam_server<span class="pl-pds">"</span></span>).
</pre></div><hr><p>Wed Sep 20 17:21:29 2023</p><script src="https://utteranc.es/client.js"
repo="caixiangyue/caixiangyue.github.io"
issue-term="pathname"
theme="github-light"
crossorigin="anonymous"
async>
</script>
</body></html>