1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgmp_error_notice_fields). |
17 |
|
|
18 |
|
|
19 |
|
-export([callback_mode/0]). |
20 |
|
-export([init/1]). |
21 |
|
-export([map/1]). |
22 |
|
-export([start_link/0]). |
23 |
|
-include_lib("stdlib/include/ms_transform.hrl"). |
24 |
|
|
25 |
|
|
26 |
|
start_link() -> |
27 |
10 |
gen_statem:start_link(?MODULE, [], envy_gen:options(?MODULE)). |
28 |
|
|
29 |
|
|
30 |
|
map({Tag, ErrorNoticeFields}) when Tag == error_response; |
31 |
|
Tag == notice_response -> |
32 |
13 |
{Tag, |
33 |
|
lists:foldl( |
34 |
|
fun |
35 |
|
({K, V}, A) -> |
36 |
106 |
case ets:lookup(?MODULE, K) of |
37 |
|
[{_, Name, Mapping}] -> |
38 |
29 |
A#{Name => Mapping(V)}; |
39 |
|
|
40 |
|
[{_, Name}] -> |
41 |
77 |
A#{Name => V}; |
42 |
|
|
43 |
|
[] -> |
44 |
:-( |
A#{K => V} |
45 |
|
end |
46 |
|
end, |
47 |
|
#{}, |
48 |
|
ErrorNoticeFields)}. |
49 |
|
|
50 |
|
|
51 |
|
init([]) -> |
52 |
10 |
process_flag(trap_exit, true), |
53 |
10 |
ets:insert_new( |
54 |
|
ets:new(?MODULE, [named_table]), |
55 |
|
pgmp:priv_consult("error-notice-fields.terms")), |
56 |
10 |
{ok, ready, #{}}. |
57 |
|
|
58 |
|
|
59 |
|
callback_mode() -> |
60 |
10 |
handle_event_function. |