Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ parse_ini_file(IniFile) ->
";" ++ _Comment ->
{AccSectionName, AccValues};
Line2 ->
case re:split(Line2, "\s?=\s?", [{return, list}]) of
Copy link
Member

@rnewson rnewson Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would re:split(Line2, "\s?(?<!\\\\)=\s?", [{return, list}]) work here? (that is, split the string only on = when not immediately preceded by \.

[Value] ->
MatchResult = re:run(Line2,<<"(.*(\\\\=)?\\S)(\\s?=\\s?)(.*)">>, [{capture,[1,4],list}]),
case MatchResult of
{match,[""|Value]} ->
MultiLineValuePart = case re:run(Line, "^ \\S", []) of
{match, _} ->
true;
Expand All @@ -426,19 +427,18 @@ parse_ini_file(IniFile) ->
_ ->
{AccSectionName, AccValues}
end;
[""|_LineValues] -> % line begins with "=", ignore
nomatch -> % line begins with "=", ignore
{AccSectionName, AccValues};
[ValueName|LineValues] -> % yeehaw, got a line!
RemainingLine = config_util:implode(LineValues, "="),
{match,[ValueName|LineValues]} -> % yeehaw, got a line
% removes comments
case re:split(RemainingLine, " ;|\t;", [{return, list}]) of
case re:split(LineValues, " ;|\t;", [{return, list}]) of
[[]] ->
% empty line means delete this key
ets:delete(?MODULE, {AccSectionName, ValueName}),
{AccSectionName, AccValues};
[LineValue | _Rest] ->
{AccSectionName,
[{{AccSectionName, ValueName}, LineValue} | AccValues]}
[{{AccSectionName, re:replace(ValueName, "\\\\=", "=", [{return,list}])}, LineValue} | AccValues]}
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions test/config_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
-define(CONFIG_FIXTURE_2,
filename:join([?CONFIG_FIXTURESDIR, "config_tests_2.ini"])).

-define(CONFIG_FIXTURE_ESCAPING,
filename:join([?CONFIG_FIXTURESDIR, "config_tests_escaping.ini"])).

-define(CONFIG_DEFAULT_D,
filename:join([?CONFIG_FIXTURESDIR, "default.d"])).

Expand Down Expand Up @@ -89,6 +92,8 @@ setup_config_notifier(Subscription) ->
Pid = spawn_config_notifier(Subscription),
{Apps, Pid}.

setup_with_escaped_chars() ->
setup(?CONFIG_CHAIN ++ [?CONFIG_FIXTURE_ESCAPING]).

teardown({Apps, Pid}) when is_pid(Pid) ->
catch exit(Pid, kill),
Expand Down Expand Up @@ -130,7 +135,7 @@ config_get_test_() ->
"Config get tests",
{
foreach,
fun setup/0,
fun setup_with_escaped_chars/0,
fun teardown/1,
[
fun should_load_all_configs/0,
Expand All @@ -139,7 +144,8 @@ config_get_test_() ->
fun should_return_custom_default_value_on_missed_option/0,
fun should_only_return_default_on_missed_option/0,
fun should_fail_to_get_binary_value/0,
fun should_return_any_supported_default/0
fun should_return_any_supported_default/0,
fun should_return_values_from_escaped_keys/0
]
}
}.
Expand Down Expand Up @@ -374,6 +380,9 @@ should_return_undefined_atom_on_missed_option() ->
should_return_custom_default_value_on_missed_option() ->
?assertEqual("bar", config:get("httpd", "foo", "bar")).

should_return_values_from_escaped_keys() ->
?assertEqual("somepass", config:get("admins", "sample=user")),
?assertEqual("somevalue", config:get("jwt_tokens","kid:somekey=")).

should_only_return_default_on_missed_option() ->
?assertEqual("0", config:get("httpd", "port", "bar")).
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/config_tests_escaping.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; Licensed to the Apache Software Foundation (ASF) under one
; or more contributor license agreements. See the NOTICE file
; distributed with this work for additional information
; regarding copyright ownership. The ASF licenses this file
; to you 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.

[admins]
test=value
sample\=user = somepass

[jwt_tokens]
kid:somekey\= = somevalue