nickva commented on code in PR #4414:
URL: https://github.com/apache/couchdb/pull/4414#discussion_r1140685068
##########
src/couch_mrview/src/couch_mrview_util.erl:
##########
@@ -581,13 +581,12 @@ validate_args(Args) ->
ok;
{[], _, _} ->
ok;
+ {[Key], StartKey, EndKey} when Key =:= StartKey andalso Key =:= EndKey
->
Review Comment:
If we translate the `key` to `start_key` & `end_key` would we hit this
clause? Or would this be for cases (if any) where we may pass `#mrargs` in
which have not been validated in parse_param?
##########
src/couch_mrview/src/couch_mrview_http.erl:
##########
@@ -486,8 +491,43 @@ parse_params(Props, Keys, #mrargs{} = Args0, Options) ->
Args0;
_ ->
% group_level set to undefined to detect if explicitly set by
user
- Args0#mrargs{keys = Keys, group = undefined, group_level =
undefined}
+ case Keys of
+ [_] ->
+ Args0#mrargs{group = undefined, group_level =
undefined};
+ _ ->
+ Args0#mrargs{keys = Keys, group = undefined,
group_level = undefined}
+ end
end,
+
+ PropsKeys = proplists:get_keys(Props),
+ case config:get_boolean("chttpd", "enable_key_exclusivity",
?DEFAULT_ENABLE_KEY_EXCLUSIVITY) of
+ true ->
+ HasKey = lists:member("key", PropsKeys),
+ HasKeys = lists:member("keys", PropsKeys),
+ HasStartEndKey =
+ lists:foldl(
+ fun(X, Result) ->
+ lists:member(X, ["start_key", "startkey", "end_key",
"endkey"]) orelse
+ Result
+ end,
+ false,
+ PropsKeys
+ ),
+ KeyIncompatible =
+ (HasKey andalso HasKeys) orelse
+ (HasKey andalso HasStartEndKey) orelse
+ (HasKeys andalso HasStartEndKey),
+
+ case KeyIncompatible of
+ true ->
+ throw(?ERROR_KEY_INCOMPATIBLE);
+ _ ->
+ ok
+ end;
+ false ->
+ ok
Review Comment:
This might look better a separate function.
Can use sets to check for membership. It might be simpler than
`lists:foldl/3`?
```erlang
PropSet = sets:from_list(Props),
HasKey = sets:is_element("key", PropSet),
HasKeys = sets:is_element("keys", PropSet),
StartEndSet = sets:from_list(["start_key", "startkey", "end_key", "endkey"]),
HasStartEndKey = not sets:is_empty(sets:intersection(StartEndSet, PropSet)).
```
What do you think?
##########
src/couch_mrview/src/couch_mrview_util.erl:
##########
@@ -564,7 +564,7 @@ validate_args(Args) ->
{red, exact, _} ->
ok;
{red, _, KeyList} when is_list(KeyList) ->
- Msg = <<"Multi-key fetchs for reduce views must use
`group=true`">>,
+ Msg = <<"Multi-key fetches for reduce views must use
`group=true`">>,
Review Comment:
Good catch!
##########
src/chttpd/test/eunit/chttpd_view_test.erl:
##########
@@ -69,86 +71,251 @@ all_view_test_() ->
fun setup/0,
fun teardown/1,
[
- fun should_succeed_on_view_with_queries_keys/1,
- fun should_succeed_on_view_with_queries_limit_skip/1,
- fun should_succeed_on_view_with_multiple_queries/1
+ ?TDEF_FE(t_view_with_queries_keys, ?TIMEOUT),
+ ?TDEF_FE(t_view_with_queries_limit_skip, ?TIMEOUT),
+ ?TDEF_FE(t_view_with_multiple_queries, ?TIMEOUT),
+ ?TDEF_FE(t_view_with_key_and_end_key),
+ ?TDEF_FE(t_key_exclusivity_view_with_key_and_end_key),
+ ?TDEF_FE(t_view_with_single_keys_and_start_key),
+
?TDEF_FE(t_key_exclusivity_view_with_single_keys_and_start_key),
+ ?TDEF_FE(t_view_with_keys_and_start_key),
+ ?TDEF_FE(t_key_exclusivity_view_with_keys_and_start_key),
+ ?TDEF_FE(t_view_map_reduce_with_key),
+ ?TDEF_FE(t_view_map_reduce_with_single_keys),
+ ?TDEF_FE(t_view_map_reduce_with_single_keys_and_group),
+ ?TDEF_FE(t_view_map_reduce_with_keys),
+ ?TDEF_FE(t_view_map_reduce_with_keys_and_group)
Review Comment:
Very nice test cleanups.
##########
src/chttpd/test/eunit/chttpd_view_test.erl:
##########
@@ -19,45 +19,47 @@
-define(PASS, "pass").
-define(AUTH, {basic_auth, {?USER, ?PASS}}).
-define(CONTENT_JSON, {"Content-Type", "application/json"}).
--define(DDOC,
- "{\"_id\": \"_design/bar\", \"views\": {\"baz\":\n"
- " {\"map\": \"function(doc) {emit(doc._id, doc._id);}\"}}}"
-).
--define(FIXTURE_TXT, ?ABS_PATH(?FILE)).
--define(i2l(I), integer_to_list(I)).
+-define(DOCS, #{
+ <<"docs">> => [
+ #{<<"_id">> => <<"a">>, <<"key">> => <<"a">>, <<"value">> => 1},
+ #{<<"_id">> => <<"b">>, <<"key">> => <<"b">>, <<"value">> => 2},
+ #{<<"_id">> => <<"c">>, <<"key">> => <<"c">>, <<"value">> => 3}
+ ]
+}).
+-define(DDOC, #{
+ <<"_id">> => <<"_design/ddoc">>,
+ <<"views">> => #{
+ <<"map">> => #{<<"map">> => <<"function(doc) { emit(doc.key,
doc.value) }">>},
+ <<"map_reduce">> => #{
+ <<"map">> => <<"function(doc) { emit(doc.key, doc.value) }">>,
+ <<"reduce">> => <<"_sum">>
+ }
+ }
+}).
+-define(ERROR_KEY_INCOMPATIBLE, #{
+ <<"error">> := <<"query_parse_error">>,
+ <<"reason">> := <<"`key(s)` is incompatible with `start_key` and
`end_key`">>
+}).
Review Comment:
Good idea to use a define here. Makes tests more compact and organized.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]