_site/cover/pgmp_uri.COVER.html

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_uri).
17
18
19 -export([key_words/1]).
20 -export([parse/1]).
21
22
23 parse(#{query := Query} = URI) ->
24 4 lists:foldl(
25 fun
26 ({K, V}, A) ->
27 6 Parameter = binary_to_existing_atom(K),
28
29 6 case lists:member(
30 Parameter,
31 key_words(parameter)) of
32
33 true ->
34 6 A#{Parameter => parameter(Parameter, V)};
35
36 false ->
37
:-(
A
38 end
39 end,
40 maps:remove(query, URI),
41 uri_string:dissect_query(Query));
42
43 parse(#{} = URI) ->
44 8 URI;
45
46 parse(URI) when is_binary(URI) ->
47 12 ?FUNCTION_NAME(
48 maps:fold(
49 fun
50 (userinfo, UserInfo, A) ->
51 4 case string:split(UserInfo, ":") of
52 [User, Password] ->
53 1 A#{user => User,
54 password => fun
55 () ->
56 1 Password
57 end};
58
59 [User] ->
60 3 A#{user => User}
61 end;
62
63 (scheme, Scheme, A) when Scheme == <<"postgresql">>;
64 Scheme == <<"postgres">> ->
65 12 A;
66
67 (_, <<>>, A) ->
68 8 A;
69
70 (path, <<"/", Name/bytes>>, A) ->
71 7 A#{database => Name};
72
73 (K, V, A) ->
74 15 A#{K => V}
75 end,
76 #{},
77 uri_string:parse(URI))).
78
79
80 parameter(port, V) ->
81 1 binary_to_integer(V);
82
83 parameter(connect_timeout, V) ->
84 1 binary_to_integer(V);
85
86 parameter(keepalives, V) ->
87
:-(
binary_to_integer(V);
88
89 parameter(keepalives_idle, V) ->
90
:-(
binary_to_integer(V);
91
92 parameter(keepalives_interval, V) ->
93
:-(
binary_to_integer(V);
94
95 parameter(keepalives_count, V) ->
96
:-(
binary_to_integer(V);
97
98 parameter(tcp_user_timeout, V) ->
99
:-(
binary_to_integer(V);
100
101 parameter(requiressl, V) ->
102
:-(
binary_to_integer(V);
103
104 parameter(sslcompression, V) ->
105
:-(
binary_to_integer(V);
106
107 parameter(sslsni, V) ->
108
:-(
binary_to_integer(V);
109
110 parameter(_, V) ->
111 4 V.
112
113
114 key_words(parameter) ->
115 6 [host,
116 hostaddr,
117 port,
118 dbname,
119 user,
120 password,
121 passfile,
122 channel_binding,
123 connect_timeout,
124 client_encoding,
125 options,
126 application_name,
127 fallback_application_name,
128 keepalives,
129 keepalives_idle,
130 keepalives_interval,
131 keepalives_count,
132 tcp_user_timeout,
133 replication,
134 gssencmode,
135 sslmode,
136 requiressl,
137 sslcompression,
138 sslcert,
139 sslkey,
140 sslpassword,
141 sslrootcert,
142 sslcrl,
143 sslcrldir,
144 sslsni,
145 requirepeer,
146 ssl_min_protocol_version,
147 ssl_max_protocol_version,
148 krbsrvname,
149 gsslib,
150 service,
151 target_session_attrs].
Line Hits Source