summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2021-04-26 10:30:35 -0700
committerILYA Khlopotov <iilyak@apache.org>2021-04-27 03:57:40 -0700
commit94bcb53b16d1729f89031a99cda3295f42801814 (patch)
tree397b79e79019392d758f168abb8d13c99eb6ad80
parent845f917e35005a84ee790cd9cb9918d0b5be25ef (diff)
downloadcouchdb-94bcb53b16d1729f89031a99cda3295f42801814.tar.gz
Add couch_lib_parse:parse_{boolean|integer|non_neg_integer}
-rw-r--r--src/couch_lib/README.md7
-rw-r--r--src/couch_lib/src/couch_lib_parse.erl61
-rw-r--r--src/couch_views/src/couch_views.app.src1
-rw-r--r--src/couch_views/src/couch_views_http_util.erl41
-rw-r--r--src/couch_views/test/couch_views_server_test.erl3
5 files changed, 81 insertions, 32 deletions
diff --git a/src/couch_lib/README.md b/src/couch_lib/README.md
index c5f0b366f..510c12f8e 100644
--- a/src/couch_lib/README.md
+++ b/src/couch_lib/README.md
@@ -18,4 +18,11 @@ Please DO NOT put CouchDB specific functionality in here. This means you shouldn
# Provided functionality
+## String parsing
+
+All string parsing functions expect either string or binary as well as the type itself. I.e. if `parse_boolean/1` is called as `parse_boolean(true)` it would return `true`. They return either the parsed type or `{error, Reason :: term()}`.
+
+- `couch_lib_parse:parse_boolean/1` - parses given string or binary into Erlang's `boolean()` type
+- `couch_lib_parse:parse_integer/1` - parses given string as Erlang's `integer()` type
+- `couch_lib_parse:parse_non_neg_integer/1` - parses given string as Erlang's `non_neg_integer()` type (`[0..infinity)`)
diff --git a/src/couch_lib/src/couch_lib_parse.erl b/src/couch_lib/src/couch_lib_parse.erl
new file mode 100644
index 000000000..e0b6c85d8
--- /dev/null
+++ b/src/couch_lib/src/couch_lib_parse.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_lib_parse).
+
+-export([
+ parse_boolean/1,
+ parse_integer/1,
+ parse_non_neg_integer/1
+]).
+
+parse_boolean(true) ->
+ true;
+parse_boolean(false) ->
+ false;
+
+parse_boolean(Val) when is_binary(Val) ->
+ parse_boolean(binary_to_list(Val));
+
+parse_boolean(Val) ->
+ case string:to_lower(Val) of
+ "true" -> true;
+ "false" -> false;
+ _ ->
+ Msg = io_lib:format("Invalid boolean: ~p", [Val]),
+ {error, list_to_binary(Msg)}
+ end.
+
+
+parse_integer(Val) when is_integer(Val) ->
+ Val;
+parse_integer(Val) when is_list(Val) ->
+ case (catch list_to_integer(Val)) of
+ IntVal when is_integer(IntVal) ->
+ IntVal;
+ _ ->
+ Msg = io_lib:format("Invalid value for integer: ~p", [Val]),
+ {error, list_to_binary(Msg)}
+ end;
+parse_integer(Val) when is_binary(Val) ->
+ binary_to_list(Val).
+
+
+parse_non_neg_integer(Val) ->
+ case parse_integer(Val) of
+ IntVal when IntVal >= 0 ->
+ IntVal;
+ _ ->
+ Fmt = "Invalid value for non negative integer: ~p",
+ Msg = io_lib:format(Fmt, [Val]),
+ {error, list_to_binary(Msg)}
+ end. \ No newline at end of file
diff --git a/src/couch_views/src/couch_views.app.src b/src/couch_views/src/couch_views.app.src
index 985c503cd..8ec3a3243 100644
--- a/src/couch_views/src/couch_views.app.src
+++ b/src/couch_views/src/couch_views.app.src
@@ -23,6 +23,7 @@
stdlib,
erlfdb,
couch_epi,
+ couch_lib,
couch_log,
config,
couch_stats,
diff --git a/src/couch_views/src/couch_views_http_util.erl b/src/couch_views/src/couch_views_http_util.erl
index 7af07266a..b3fd7efc5 100644
--- a/src/couch_views/src/couch_views_http_util.erl
+++ b/src/couch_views/src/couch_views_http_util.erl
@@ -319,40 +319,19 @@ parse_param(Key, Val, Args, IsDecoded) ->
end.
-parse_boolean(true) ->
- true;
-parse_boolean(false) ->
- false;
-
-parse_boolean(Val) when is_binary(Val) ->
- parse_boolean(?b2l(Val));
-
parse_boolean(Val) ->
- case string:to_lower(Val) of
- "true" -> true;
- "false" -> false;
- _ ->
- Msg = io_lib:format("Invalid boolean parameter: ~p", [Val]),
- throw({query_parse_error, ?l2b(Msg)})
+ case couch_lib_parse:parse_boolean(Val) of
+ {error, Reason} ->
+ throw({query_parse_error, Reason});
+ Boolean ->
+ Boolean
end.
-parse_int(Val) when is_integer(Val) ->
- Val;
-parse_int(Val) ->
- case (catch list_to_integer(Val)) of
- IntVal when is_integer(IntVal) ->
- IntVal;
- _ ->
- Msg = io_lib:format("Invalid value for integer: ~p", [Val]),
- throw({query_parse_error, ?l2b(Msg)})
- end.
parse_pos_int(Val) ->
- case parse_int(Val) of
- IntVal when IntVal >= 0 ->
- IntVal;
- _ ->
- Fmt = "Invalid value for positive integer: ~p",
- Msg = io_lib:format(Fmt, [Val]),
- throw({query_parse_error, ?l2b(Msg)})
+ case couch_lib_parse:parse_non_neg_integer(Val) of
+ {error, Reason} ->
+ throw({query_parse_error, Reason});
+ Int ->
+ Int
end.
diff --git a/src/couch_views/test/couch_views_server_test.erl b/src/couch_views/test/couch_views_server_test.erl
index 3c0c0a86a..41d7aaf42 100644
--- a/src/couch_views/test/couch_views_server_test.erl
+++ b/src/couch_views/test/couch_views_server_test.erl
@@ -46,7 +46,8 @@ setup() ->
fabric,
couch_jobs,
couch_js,
- couch_eval
+ couch_eval,
+ couch_lib
]),
Ctx.