|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import re |
| 4 | + |
| 5 | +from typing import List, Union |
| 6 | + |
| 7 | + |
| 8 | +class FilterBuilder: |
| 9 | + def __init__(self): |
| 10 | + self.filters = [] |
| 11 | + |
| 12 | + def _add_value_filter(self, allowed_types, operator, field, value, allow_list): |
| 13 | + self._verify_field_is_string(field) |
| 14 | + |
| 15 | + type_check_value = value |
| 16 | + if isinstance(value, list) and allow_list: |
| 17 | + self._verify_list_of_single_type(value, allowed_types) |
| 18 | + type_check_value = value[0] |
| 19 | + |
| 20 | + if not isinstance(type_check_value, allowed_types): |
| 21 | + raise TypeError(f'value must be a {allowed_types}') |
| 22 | + |
| 23 | + if isinstance(value, list): |
| 24 | + value = [v.isoformat() if isinstance(v, datetime.datetime) else v for v in value] |
| 25 | + elif isinstance(value, datetime.datetime): |
| 26 | + value = value.isoformat() |
| 27 | + else: |
| 28 | + value = self._toJson(value) |
| 29 | + |
| 30 | + # python surrounds datetime values in a list with single quotes |
| 31 | + if isinstance(type_check_value, datetime.datetime): |
| 32 | + value = re.sub("'", '', f'{value}') |
| 33 | + |
| 34 | + constructed_filter = re.sub(r'\s', '', f'{operator}({field},{value})') |
| 35 | + self.filters.append(constructed_filter) |
| 36 | + |
| 37 | + def equals(self, field: str, value: Union[list, bool, int, float, str, datetime.datetime]): |
| 38 | + types = (bool, int, float, str, datetime.datetime) |
| 39 | + self._add_value_filter(types, 'equals', field, value, allow_list=True) |
| 40 | + |
| 41 | + def less_than(self, field: str, value: Union[int, float, datetime.datetime]): |
| 42 | + types = (int, float, datetime.datetime) |
| 43 | + self._add_value_filter(types, 'less-than', field, value, allow_list=False) |
| 44 | + |
| 45 | + def less_or_equal(self, field: str, value: Union[int, float, datetime.datetime]): |
| 46 | + types = (int, float, datetime.datetime) |
| 47 | + self._add_value_filter(types, 'less-or-equal', field, value, allow_list=False) |
| 48 | + |
| 49 | + def greater_than(self, field: str, value: Union[int, float, datetime.datetime]): |
| 50 | + types = (int, float, datetime.datetime) |
| 51 | + self._add_value_filter(types, 'greater-than', field, value, allow_list=False) |
| 52 | + |
| 53 | + def greater_or_equal(self, field: str, value: Union[int, float, datetime.datetime]): |
| 54 | + types = (int, float, datetime.datetime) |
| 55 | + self._add_value_filter(types, 'greater-or-equal', field, value, allow_list=False) |
| 56 | + |
| 57 | + def contains(self, field: str, value: Union[str, list]): |
| 58 | + types = (str,) |
| 59 | + self._add_value_filter(types, 'contains', field, value, allow_list=True) |
| 60 | + |
| 61 | + def contains_any(self, field: str, value: Union[str, list]): |
| 62 | + types = (str,) |
| 63 | + self._add_value_filter(types, 'contains-any', field, value, allow_list=True) |
| 64 | + |
| 65 | + def contains_all(self, field: str, value: Union[str, list]): |
| 66 | + types = (str,) |
| 67 | + self._add_value_filter(types, 'contains-all', field, value, allow_list=True) |
| 68 | + |
| 69 | + def ends_with(self, field: str, value: str): |
| 70 | + types = (str,) |
| 71 | + self._add_value_filter(types, 'ends-with', field, value, allow_list=False) |
| 72 | + |
| 73 | + def starts_with(self, field: str, value: str): |
| 74 | + types = (str,) |
| 75 | + self._add_value_filter(types, 'starts-with', field, value, allow_list=False) |
| 76 | + |
| 77 | + def any(self, field: str, value: List[Union[str, bool, int, float, datetime.datetime]]): |
| 78 | + if type(value) is not list: |
| 79 | + raise TypeError('value must be a list') |
| 80 | + types =(bool, int, float, str, datetime.datetime) |
| 81 | + self._add_value_filter(types, 'any', field, value, allow_list=True) |
| 82 | + |
| 83 | + def has(self, field: str): |
| 84 | + self._verify_field_is_string(field) |
| 85 | + self.filters.append(f'has({field})') |
| 86 | + |
| 87 | + def _verify_field_is_string(self, field): |
| 88 | + if not isinstance(field, str): |
| 89 | + raise TypeError('field must be a string') |
| 90 | + |
| 91 | + def _verify_list_of_single_type(self, value, allowed_types): |
| 92 | + if not value: # if list is empty, we cannot determine the type |
| 93 | + raise ValueError('value cannot be an empty list') |
| 94 | + |
| 95 | + first_type = type(value[0]) |
| 96 | + |
| 97 | + if first_type not in allowed_types: |
| 98 | + raise TypeError(f'Items in list must be one of the following types: {allowed_types}') |
| 99 | + |
| 100 | + if not all(isinstance(v, first_type) for v in value): |
| 101 | + raise TypeError('All items in value must be of the same type') |
| 102 | + |
| 103 | + def _toJson(self, value): |
| 104 | + return json.dumps(value, separators=(',', ':')) |
| 105 | + |
| 106 | + def build(self): |
| 107 | + return ','.join(self.filters) |
0 commit comments