-
Notifications
You must be signed in to change notification settings - Fork 10
/
sheldon_config.erl
111 lines (92 loc) · 3.62 KB
/
sheldon_config.erl
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
%%% @doc sheldon's module for config handling.
%%%
%%% Copyright Erlang Solutions Ltd. 2017 <hello@inaka.net>
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%% @end
%%% @copyright Erlang Solutions Ltd. <hello@inaka.net>
%%%
-module(sheldon_config).
-author("Felipe Ripoll <felipe@inakanetworks.com>").
%% API
-export([default/0, normalize/1, apply_adapters/2]).
-export_type([config/0, ignore_block/0, regex/0]).
-type regex() :: iodata().
-type ignore_block() :: #{open := regex(), close := regex()}.
-type adapter() :: module().
-type config() ::
#{lang => sheldon_dictionary:language(),
ignore_words => [string()],
ignore_patterns => [regex()],
ignore_blocks => [ignore_block()],
adapters => [adapter()]}.
%%%===================================================================
%%% API
%%%===================================================================
%% @doc returns a default config
-spec default() -> config().
default() ->
#{lang => default_lang(),
ignore_words => default_ignore_words(),
ignore_patterns => default_ignore_patterns(),
ignore_blocks => default_ignore_blocks()}.
%% @doc given a config map this method fills the missing keys
%% and convert to lowercase the ignore_words
-spec normalize(config()) -> config().
normalize(Config) ->
#{lang => normalize_lang(Config),
ignore_words => normalize_ignore_words(Config),
ignore_patterns => normalize_ignore_patterns(Config),
ignore_blocks => normalize_ignore_blocks(Config),
adapters => normalize_adapters(Config)}.
%% @doc Apply adapters to a given iodata() line.
-spec apply_adapters(iodata(), [adapter()]) -> iodata().
apply_adapters(Line, []) ->
Line;
apply_adapters(Line, [Adapter | Rest]) ->
Result = apply(Adapter, adapt, [Line]),
apply_adapters(Result, Rest).
%%%===================================================================
%%% Internal Functions
%%%===================================================================
-spec normalize_lang(config()) -> sheldon_dictionary:language().
normalize_lang(Config) ->
maps:get(lang, Config, default_lang()).
-spec normalize_ignore_words(config()) -> [string()].
normalize_ignore_words(Config) ->
IgnoredWords = maps:get(ignore_words, Config, default_ignore_words()),
lists:map(fun string:to_lower/1, IgnoredWords).
-spec normalize_ignore_patterns(config()) -> [string()].
normalize_ignore_patterns(Config) ->
maps:get(ignore_patterns, Config, default_ignore_patterns()).
-spec normalize_ignore_blocks(config()) -> [ignore_block()].
normalize_ignore_blocks(Config) ->
maps:get(ignore_blocks, Config, default_ignore_blocks()).
-spec normalize_adapters(config()) -> [adapter()].
normalize_adapters(Config) ->
maps:get(adapters, Config, default_adapters()).
-spec default_lang() -> sheldon_dictionary:language().
default_lang() ->
eng.
-spec default_ignore_words() -> [].
default_ignore_words() ->
[].
-spec default_ignore_patterns() -> [].
default_ignore_patterns() ->
[].
-spec default_ignore_blocks() -> [].
default_ignore_blocks() ->
[].
-spec default_adapters() -> [].
default_adapters() ->
[].