summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriilyak <iilyak@users.noreply.github.com>2021-04-27 05:38:22 -0700
committerGitHub <noreply@github.com>2021-04-27 05:38:22 -0700
commit4f36e466ed14df5d89f6ce17c07e287d3cb1d365 (patch)
tree906429d88a11aa486522c4baccd6648bbcfeb02a
parentbdb38184e252dbd390bccb75d18db536d9240acd (diff)
parent94bcb53b16d1729f89031a99cda3295f42801814 (diff)
downloadcouchdb-4f36e466ed14df5d89f6ce17c07e287d3cb1d365.tar.gz
Merge pull request #3529 from cloudant/create_couch_lib
Create couch lib
-rw-r--r--rebar.config.script1
-rw-r--r--rel/reltool.config2
-rw-r--r--src/couch_lib/.gitignore21
-rw-r--r--src/couch_lib/README.md28
-rw-r--r--src/couch_lib/src/couch_lib.app.src22
-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
9 files changed, 148 insertions, 32 deletions
diff --git a/rebar.config.script b/rebar.config.script
index 6dafa8f89..e61b5aaa5 100644
--- a/rebar.config.script
+++ b/rebar.config.script
@@ -119,6 +119,7 @@ SubDirs = [
"src/couch",
"src/couch_eval",
"src/couch_js",
+ "src/couch_lib",
"src/couch_replicator",
"src/couch_stats",
"src/couch_tests",
diff --git a/rel/reltool.config b/rel/reltool.config
index 7d3599331..a1cc938c1 100644
--- a/rel/reltool.config
+++ b/rel/reltool.config
@@ -35,6 +35,7 @@
couch,
couch_epi,
couch_jobs,
+ couch_lib,
couch_log,
couch_replicator,
couch_stats,
@@ -93,6 +94,7 @@
{app, couch_eval, [{incl_cond, include}]},
{app, couch_js, [{incl_cond, include}]},
{app, couch_jobs, [{incl_cond, include}]},
+ {app, couch_lib, [{incl_cond, include}]},
{app, couch_log, [{incl_cond, include}]},
{app, couch_replicator, [{incl_cond, include}]},
{app, couch_stats, [{incl_cond, include}]},
diff --git a/src/couch_lib/.gitignore b/src/couch_lib/.gitignore
new file mode 100644
index 000000000..fed08d1ca
--- /dev/null
+++ b/src/couch_lib/.gitignore
@@ -0,0 +1,21 @@
++*.o
++*.so
++*.lib
++*.dll
++*.d/
++
++ebin/
++.eunit
++.rebar
++
++*.plt
++*.swp
++*.swo
++.erlang.cookie
++erl_crash.dump
++.idea
++.vscode
++*.iml
++rebar.lock
++_*
++*~ \ No newline at end of file
diff --git a/src/couch_lib/README.md b/src/couch_lib/README.md
new file mode 100644
index 000000000..510c12f8e
--- /dev/null
+++ b/src/couch_lib/README.md
@@ -0,0 +1,28 @@
+# Description
+
+`couch_lib` application is a collection of "pure" miscellaneous functions.
+They are "pure" in a sense that these functions should not call any other CouchDB
+applications. Think of this application as an extension for Erlang/OTP standard library.
+
+The two main reasons for this application to exist are:
+
+- to share non CouchDB specific helper functions between applications
+- avoid or break cyclic dependencies between applications
+
+Please DO NOT put CouchDB specific functionality in here. This means you shouldn't:
+
+- call couch_log:
+- call config:
+- rely on process dictionary values set by processes within CouchDB using `erlang:put/2`
+- send messages to specific `gen_server` processes using `gen_server:call`
+
+# 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.app.src b/src/couch_lib/src/couch_lib.app.src
new file mode 100644
index 000000000..1f78cf9fd
--- /dev/null
+++ b/src/couch_lib/src/couch_lib.app.src
@@ -0,0 +1,22 @@
+% 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.
+
+{application, couch_lib, [
+ {description, "CouchDB library of various helpers"},
+ {vsn, git},
+ {registered, [
+ ]},
+ {applications, [
+ kernel,
+ stdlib
+ ]}
+]}.
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.