iilyak commented on a change in pull request #2662: Use `couch_rate` application for `couch_view` URL: https://github.com/apache/couchdb/pull/2662#discussion_r393048997
########## File path: src/couch_rate/src/couch_rate_config.erl ########## @@ -0,0 +1,61 @@ +% 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. + +-module(couch_rate_config). + +% This parser supports only maps where key is atom and value +% is positive float or positive integer. + +-include_lib("syntax_tools/include/merl.hrl"). + +-export([ + from_str/1, + to_str/1 +]). + +from_str(String) -> + parse_map(merl:quote(String)). + +to_str(Map) when is_map(Map) -> + StringArgs = maps:fold(fun(Key, Val, Acc) -> + Acc ++ [atom_to_list(Key) ++ " => " ++ number_to_list(Val)] + end, [], Map), + "#{" ++ string:join(StringArgs, ", ") ++ "}". + +number_to_list(Int) when is_integer(Int) -> + integer_to_list(Int); +number_to_list(Float) when is_float(Float) -> + float_to_list(Float). + +parse_map(MapAST) -> Review comment: It could work. I am not sure it would save the amount of code though. In the `parse_map` function I combine parsing with validation of parameters. If we switch to JSON parser the validation code can easily reach the same amount of code. Frankly speaking our config file is a Frankenstein anyway. Because we cannot split the lines and have to write a map or JSON in a single line. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
