Skip to content

Allow custom regular expression for query filters #60

Description

@Prill

Currently, parse_query_string separates out filter queries (in the form of filter:value) from the rest of the query, using a regular expression. This is hard-coded in the file as filters_regexp. However, I would like to extend this to allow hyphens in the filter values without quotes (e.g. "foo:value-with-hyphens"). This is a trivial change to the regex, but because the value is hard-coded, changing this would require duplicating separate_filters_from_query just to change the expression.

Proposed solution

Change the signature of separate_filters_from_query and parse_query_string to have an optional filter_regex parameter which defaults to the existing expression:

DEFAULT_FILTER_REGEXP = re.compile(r'\b(\w+):(\w+|"[^"]+"|\'[^\']+\')')

def separate_filters_from_query(query_string, filters_regexp=DEFAULT_FILTER_REGEXP):
    ...  # The method body is unchanged.

def parse_query_string(query_string, operator=None, zero_terms=MATCH_NONE, filters_regexp=DEFAULT_FILTER_REGEXP):
    filters, query_string = separate_filters_from_query(query_string, filters_regexp=filters_regexp)
    # Remaining method body unchanged.

This would allow users to easily customize the regular expression for their own needs. Examples would include the one I mentioned above, or perhaps replacing the colon with an equals sign (filter=foo).

If this is something people think could be reasonable, I could try my hand at making a merge request. Wanted to clear with the maintainers first though.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions