narrow: Use dict to map operator to by_* method in NarrowBuilder.

Updates the logic for identifying the method to use to extend the
query for the given term from a narrow to use a dictionary that
maps the operator string to the by_* method in the NarrowBuilder
class.

Previously, the by_* method was determined by building a string
based on the operator string and replacing dashes with underscores.
This commit is contained in:
Lauryn Menard 2023-03-20 19:58:10 +01:00 committed by Tim Abbott
parent ff6ff1e014
commit a9b3a9c673
1 changed files with 17 additions and 3 deletions

View File

@ -256,6 +256,20 @@ class NarrowBuilder:
self.msg_id_column = msg_id_column self.msg_id_column = msg_id_column
self.realm = realm self.realm = realm
self.is_web_public_query = is_web_public_query self.is_web_public_query = is_web_public_query
self.by_method_map = {
"has": self.by_has,
"in": self.by_in,
"is": self.by_is,
"stream": self.by_stream,
"streams": self.by_streams,
"topic": self.by_topic,
"sender": self.by_sender,
"near": self.by_near,
"id": self.by_id,
"search": self.by_search,
"pm-with": self.by_pm_with,
"group-pm-with": self.by_group_pm_with,
}
def add_term(self, query: Select, term: Dict[str, Any]) -> Select: def add_term(self, query: Select, term: Dict[str, Any]) -> Select:
""" """
@ -278,9 +292,9 @@ class NarrowBuilder:
negated = term.get("negated", False) negated = term.get("negated", False)
method_name = "by_" + operator.replace("-", "_") if operator in self.by_method_map:
method = getattr(self, method_name, None) method = self.by_method_map[operator]
if method is None: else:
raise BadNarrowOperatorError("unknown operator " + operator) raise BadNarrowOperatorError("unknown operator " + operator)
if negated: if negated: