Skip to content

Commit

Permalink
add utf8 check
Browse files Browse the repository at this point in the history
  • Loading branch information
willholley committed Nov 2, 2023
1 parent 5296a02 commit caf1e70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/mango/src/mango_idx_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ range(Selector, Index) ->
range(Selector, Index, '$gt', mango_json:min(), '$lt', mango_json:max()).

% Adjust Low and High based on values found for the
% givend Index in Selector.
% given Index in Selector.
range({[{<<"$and">>, Args}]}, Index, LCmp, Low, HCmp, High) ->
lists:foldl(
fun
Expand Down Expand Up @@ -624,6 +624,10 @@ indexable_fields_gte_test() ->
Selector = #{<<"field">> => #{<<"$gte">> => undefined}},
?assertEqual([<<"field">>], indexable_fields_of(Selector)).

indexable_fields_beginswith_test() ->
Selector = #{<<"field">> => #{<<"$beginsWith">> => undefined}},
?assertEqual([<<"field">>], indexable_fields_of(Selector)).

indexable_fields_gt_test() ->
Selector = #{<<"field">> => #{<<"$gt">> => undefined}},
?assertEqual([<<"field">>], indexable_fields_of(Selector)).
Expand Down
12 changes: 10 additions & 2 deletions src/mango/src/mango_selector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ norm_ops({[{<<"$text">>, Arg}]}) when
norm_ops({[{<<"$text">>, Arg}]}) ->
?MANGO_ERROR({bad_arg, '$text', Arg});
norm_ops({[{<<"$beginsWith">>, Arg}]} = Cond) when is_binary(Arg) ->
Cond;
case couch_util:validate_utf8(Arg) of
true -> Cond;
false -> ?MANGO_ERROR({bad_arg, '$beginsWith', Arg})
end;
% Not technically an operator but we pass it through here
% so that this function accepts its own output. This exists
% so that $text can have a field name value which simplifies
Expand Down Expand Up @@ -1070,12 +1073,17 @@ check_beginswith(Field, Prefix) ->
match_beginswith_test() ->
% matching
?assertEqual(true, check_beginswith(<<"_id">>, <<"f">>)),
% no match (user_id is not a binary string)
% no match (user_id field in the test doc contains an integer)
?assertEqual(false, check_beginswith(<<"user_id">>, <<"f">>)),
% invalid (prefix is not a binary string)
?assertThrow(
{mango_error, mango_selector, {invalid_operator, <<"$beginsWith">>}},
check_beginswith(<<"user_id">>, 1)
),
% invalid (prefix is not a utf8 string)
?assertThrow(
{mango_error, mango_selector, {invalid_operator, <<"$beginsWith">>}},
check_beginswith(<<123>>, 1)
).

-endif.

0 comments on commit caf1e70

Please sign in to comment.