You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/12/03 23:10:54 UTC

[GitHub] [couchdb] jaydoane commented on a diff in pull request #4287: Allow = in config key values

jaydoane commented on code in PR #4287:
URL: https://github.com/apache/couchdb/pull/4287#discussion_r1038871687


##########
src/config/src/config.erl:
##########
@@ -414,89 +415,73 @@ is_sensitive(Section, Key) ->
     end.
 
 parse_ini_file(IniFile) ->
+    IniBin = read_ini_file(IniFile),
+    ParsedIniValues = parse_ini(IniBin),
+    {ok, lists:filter(fun delete_keys/1, ParsedIniValues)}.
+
+parse_ini(IniBin) when is_binary(IniBin) ->
+    Lines0 = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]),
+    Lines1 = lists:map(fun remove_comments/1, Lines0),
+    Lines2 = lists:map(fun trim/1, Lines1),
+    Lines3 = lists:filter(fun(Line) -> Line =/= "" end, Lines2),
+    {_, IniValues} = lists:foldl(fun parse_fold/2, {"", []}, Lines3),
+    lists:reverse(IniValues).
+
+parse_fold("[" ++ Rest, {Section, KVs}) ->
+    % Check for end ] brackend, if not found or empty section skip the rest
+    case string:split(Rest, "]") of
+        ["", _] -> {Section, KVs};
+        [NewSection, ""] -> {NewSection, KVs};
+        _Else -> {Section, KVs}
+    end;
+parse_fold(_Line, {"" = Section, KVs}) ->
+    % Empty section don't parse any lines until we're in a section
+    {Section, KVs};
+parse_fold(Line, {Section, KVs}) ->
+    case string:split(Line, " = ") of
+        [K, V] when V =/= "" ->
+            % Key may have an "=" in it. Also, assert we'll never have a
+            % deletion case here since we're working with a stripped line
+            {Section, [{{Section, trim(K)}, trim(V)} | KVs]};
+        [Line] ->

Review Comment:
   I'm seeing "Bound variable in pattern: Line" here and line 454, so maybe want to use different names?



##########
src/config/src/config.erl:
##########
@@ -414,89 +415,73 @@ is_sensitive(Section, Key) ->
     end.
 
 parse_ini_file(IniFile) ->
+    IniBin = read_ini_file(IniFile),
+    ParsedIniValues = parse_ini(IniBin),
+    {ok, lists:filter(fun delete_keys/1, ParsedIniValues)}.
+
+parse_ini(IniBin) when is_binary(IniBin) ->
+    Lines0 = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]),
+    Lines1 = lists:map(fun remove_comments/1, Lines0),
+    Lines2 = lists:map(fun trim/1, Lines1),
+    Lines3 = lists:filter(fun(Line) -> Line =/= "" end, Lines2),
+    {_, IniValues} = lists:foldl(fun parse_fold/2, {"", []}, Lines3),
+    lists:reverse(IniValues).
+
+parse_fold("[" ++ Rest, {Section, KVs}) ->
+    % Check for end ] brackend, if not found or empty section skip the rest
+    case string:split(Rest, "]") of
+        ["", _] -> {Section, KVs};
+        [NewSection, ""] -> {NewSection, KVs};
+        _Else -> {Section, KVs}
+    end;
+parse_fold(_Line, {"" = Section, KVs}) ->
+    % Empty section don't parse any lines until we're in a section
+    {Section, KVs};
+parse_fold(Line, {Section, KVs}) ->
+    case string:split(Line, " = ") of

Review Comment:
   Should we add documentation that describes this algorithm?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org