davisp commented on a change in pull request #1376: Add fields to sort based on
selector
URL: https://github.com/apache/couchdb/pull/1376#discussion_r193521699
##########
File path: src/mango/src/mango_selector.erl
##########
@@ -638,11 +639,110 @@ has_required_fields_int([{[{Field, Cond}]} | Rest],
RequiredFields) ->
end.
+% Returns true if a field in the selector is a constant value e.g. {a: {$eq:
1}}
+is_constant_field(Selector, Field) ->
+ is_constant_field_int(Selector, Field).
+
+
+is_constant_field_int({[]}, _Field) ->
+ false;
+
+is_constant_field_int(Selector, Field) when not is_list(Selector) ->
+ is_constant_field_int([Selector], Field);
+
+is_constant_field_int([], _Field) ->
+ false;
+
+is_constant_field_int([{[{<<"$and">>, Args}]}], Field) when is_list(Args) ->
+ lists:any(fun(Arg) -> is_constant_field_int(Arg, Field) end, Args);
+
+is_constant_field_int([{[{<<"$and">>, Args}]}], Field) ->
+ is_constant_field_int(Args, Field);
+
+is_constant_field_int([{[{SelectorField, {[{Cond, _Val}]}}]} | _Rest], Field)
when SelectorField =:= Field ->
+ Cond =:= <<"$eq">>;
+
+is_constant_field_int([{[{SelectorField, _}]} | Rest], Field) when
SelectorField =/= Field ->
Review comment:
No need to use the guard here as you capture all equality cases in the
previous clause.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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