diff --git a/fieldtest.py b/fieldtest.py index 3d21f34..e73cae0 100644 --- a/fieldtest.py +++ b/fieldtest.py @@ -10,7 +10,7 @@ from uuid import uuid4, UUID from decimal import Decimal -from testimplementation import parse_function_args +from testimplementation import parse_function_args, parse_function_args_with_auto_type_cast from funcargpreprocessor import DateArg, DateTimeArg, ErrorCode, DecimalArg from funcargpreprocessor import FieldTypeError, FieldError, MissingFieldError, FieldValueError @@ -49,7 +49,7 @@ def inner_fu(): , 'reg_time': {"data_type": DateTimeArg('%Y-%m-%d %H:%M:%S'), "default": get_current_time} , 'request_id': {'validator': validate_uuid4, 'required': True} , "name": {"data_type": dict, "nested": { - "first_name": {"data_type": str} + "first_name": {"data_type": str}, "last_name": {"data_type": str} }} , "location": {"data_type": list , "nested": { @@ -72,14 +72,23 @@ def inner_fu(): } -class Test: +class TestNonAutoCast: @parse_function_args(function_arg_definition) def test(self, fun_arg, **kwargs): return kwargs -class_instance = Test() +not_auto_cast_class_instance = TestNonAutoCast() + + +class TestAutoCast: + @parse_function_args_with_auto_type_cast(function_arg_definition) + def test(self, fun_arg, **kwargs): + return kwargs + + +auto_cast_class_instance = TestAutoCast() class FunctionArgTestCases(unittest.TestCase): @@ -103,7 +112,7 @@ def test_positive_1(self): name = { "first_name": "Sabari" } - response = class_instance.test( + response = not_auto_cast_class_instance.test( {"pageNo": 10, "start_date": start_date.strftime('%Y-%m-%d') , "reg_time": reg_time.strftime('%Y-%m-%d %H:%M:%S'), 'request_id': request_uuid, @@ -146,7 +155,7 @@ def test_positive_2(self): name = { "first_name": "Sabari" } - response = class_instance.test( + response = not_auto_cast_class_instance.test( { "start_date": start_date.strftime('%Y-%m-%d'), 'request_id': request_uuid, @@ -171,14 +180,14 @@ def test_positive_2(self): def test_mandatory_error(self): with self.assertRaises(MissingFieldError) as e: - class_instance.test({}) + not_auto_cast_class_instance.test({}) self.assertEqual(e.exception.error_code, ErrorCode.MISSING_MANDATORY_FIELD) self.assertEqual(e.exception.field_name, 'request_id') self.assertEqual(e.exception.message, 'request_id is a mandatory field') def test_type_error_int(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 'a', "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { @@ -190,9 +199,9 @@ def test_type_error_int(self): self.assertEqual(f'pageNo should be of type {int}', e.exception.message) self.assertEqual({"expectedType": f'{int}'}, e.exception.error_data) - def test_type_error_int(self): + def test_type_error_nested_int(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, 'id_list': [1, 'a'], @@ -207,7 +216,7 @@ def test_type_error_int(self): def test_type_error_datetime(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, "start_date": "afsa", @@ -225,7 +234,7 @@ def test_type_error_datetime(self): def test_type_error_bool(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, 'id_list': [1, 3], @@ -241,7 +250,7 @@ def test_type_error_bool(self): def test_value_error_value_list(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, 'id_list': [1, 4], @@ -256,7 +265,7 @@ def test_value_error_value_list(self): def test_value_error_min_value(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': -1, "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { @@ -270,7 +279,7 @@ def test_value_error_min_value(self): def test_value_error_max_value(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 11, "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { @@ -284,7 +293,7 @@ def test_value_error_max_value(self): def test_value_error_min_value_1(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, "location": [{"address_line_1": "fad", "pincode": 6544554, "latitude": "-91", "contact_person": { @@ -298,7 +307,7 @@ def test_value_error_min_value_1(self): def test_value_error_max_value_1(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, "location": [{"address_line_1": "fad", "pincode": 6544554, "latitude": "91", "contact_person": { @@ -313,7 +322,7 @@ def test_value_error_max_value_1(self): def test_value_error_max_value_2(self): start_date = (date.today() + timedelta(days=11)).strftime('%Y-%m-%d') with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( {'request_id': str(uuid4()), "start_date": start_date, "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { @@ -328,10 +337,341 @@ def test_value_error_max_value_2(self): def test_length_error_1(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + not_auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "saba" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MIN_LENGTH_VIOLATED, e.exception.error_code) + self.assertEqual('location[0].contact_person.first_name', e.exception.field_name) + self.assertEqual('location[0].contact_person.first_name has a minimum length of 5', e.exception.message) + self.assertEqual({'minLength': 5}, e.exception.error_data) + + def test_length_error_2(self): + with self.assertRaises(FieldValueError) as e: + not_auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari fadsfasfasf" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MAX_LENGTH_VIOLATED, e.exception.error_code) + self.assertEqual('location[0].contact_person.first_name', e.exception.field_name) + self.assertEqual('location[0].contact_person.first_name has a maximum length of 10', e.exception.message) + self.assertEqual({'maxLength': 10}, e.exception.error_data) + + def test_regex_error(self): + with self.assertRaises(FieldValueError) as e: + not_auto_cast_class_instance.test( + {'request_id': str(uuid4()), + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "888423A3317" + }}]}) + self.assertEqual(ErrorCode.FIELD_REGEX_VALIDATION_FAILED, e.exception.error_code) + self.assertEqual('location[0].contact_person.phone_number', e.exception.field_name) + self.assertEqual('location[0].contact_person.phone_number should be of format - [0-9]{10,12}', + e.exception.message) + + def test_validator_error(self): + with self.assertRaises(FieldError) as e: + not_auto_cast_class_instance.test( + {'request_id': 'fasdfsdaf', + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual('request_id', e.exception.field_name) + self.assertEqual('request_id should be UUID4', e.exception.message) + + def test_field_validation_list(self): + with self.assertRaises(FieldTypeError) as e: + not_auto_cast_class_instance.test({ + "request_id": str(uuid4()) + , "location": {"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }} + }) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual("location", e.exception.field_name) + self.assertEqual(e.exception.message, "location should be of type ") + + def test_field_validation_dict(self): + with self.assertRaises(FieldTypeError) as e: + not_auto_cast_class_instance.test({ + "request_id": str(uuid4()) + , "location": {"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }} + , "name": "fasdf" + }) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual("name", e.exception.field_name) + self.assertEqual("name should be of type ", e.exception.message) + + +class FunctionArgAutoTypeCastTestCases(unittest.TestCase): + def test_positive_1(self): + start_date = date.today() + reg_time = datetime.now() + reg_time = reg_time.replace(microsecond=0) + request_uuid = str(uuid4()) + location_1 = {"address_line_1": "fad", "pincode": 123124, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }} + latitude = "-43.12412" + location_2 = { + "address_line_1": "fad" + , "pincode": 6544554 + , "latitude": latitude + , "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + } + } + name = { + "first_name": "Sabari" + } + response = auto_cast_class_instance.test( + {"pageNo": 10, "start_date": start_date.strftime('%Y-%m-%d') + , "reg_time": reg_time.strftime('%Y-%m-%d %H:%M:%S'), + 'request_id': request_uuid, + "id_list": [1, 1, 2, 0], + "location": [{**deepcopy(location_1), 'fad': 'fad'}, deepcopy(location_2)] + , "name": deepcopy(name) + , "location_check": "true" + } + + ) + location_2["latitude"] = Decimal(latitude) + self.assertEqual(response.get('page_no'), 10) + self.assertEqual(response.get('start_date'), start_date) + self.assertEqual(response.get('request_id'), request_uuid) + self.assertEqual(response.get('reg_time'), reg_time) + self.assertEqual(response.get('id_list'), [1, 1, 2, 0]) + self.assertEqual(response.get('location')[0], location_1) + self.assertEqual(response.get('location')[1], location_2) + self.assertEqual(response.get('location')[1], location_2) + self.assertEqual(response.get('name'), name) + self.assertEqual(response.get('location_check'), True) + + def test_positive_2(self): + start_date = date.today() + reg_time = datetime.now() + reg_time = reg_time.replace(microsecond=0) + request_uuid = str(uuid4()) + location_1 = {"address_line_1": "fad", "pincode": 123124, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }} + latitude = "-43.12412" + location_2 = { + "address_line_1": "fad" + , "pincode": 6544554 + , "latitude": latitude + , "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + } + } + name = { + "first_name": "Sabari" + } + page_no = "9" + response = auto_cast_class_instance.test( + { + "pageNo": page_no, + "start_date": start_date.strftime('%Y-%m-%d'), + 'request_id': request_uuid, + "id_list": [1, 1, 2, 0], + "location": [{**deepcopy(location_1), 'fad': 'fad'}, deepcopy(location_2)] + , "name": deepcopy(name) + , "location_check": "True" + } + + ) + location_2["latitude"] = Decimal(latitude) + self.assertEqual(response.get('page_no'), int(page_no)) + self.assertEqual(response.get('start_date'), start_date) + self.assertEqual(response.get('request_id'), request_uuid) + self.assertEqual(response.get('reg_time'), reg_time) + self.assertEqual(response.get('id_list'), [1, 1, 2, 0]) + self.assertEqual(response.get('location')[0], location_1) + self.assertEqual(response.get('location')[1], location_2) + self.assertEqual(response.get('location')[1], location_2) + self.assertEqual(response.get('name'), name) + self.assertEqual(response.get('location_check'), True) + + def test_mandatory_error(self): + with self.assertRaises(MissingFieldError) as e: + auto_cast_class_instance.test({}) + self.assertEqual(e.exception.error_code, ErrorCode.MISSING_MANDATORY_FIELD) + self.assertEqual(e.exception.field_name, 'request_id') + self.assertEqual(e.exception.message, 'request_id is a mandatory field') + + def test_type_error_int(self): + with self.assertRaises(FieldTypeError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 'a', + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual('pageNo', e.exception.field_name) + self.assertEqual(f'pageNo should be of type {int}', e.exception.message) + self.assertEqual({"expectedType": f'{int}'}, e.exception.error_data) + + def test_type_error_nested_int(self): + with self.assertRaises(FieldTypeError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': '1', + 'id_list': [1, 'a'], + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual('id_list[1]', e.exception.field_name) + self.assertEqual(f'id_list[1] should be of type {int}', e.exception.message) + self.assertEqual({"expectedType": f'{int}'}, e.exception.error_data) + + def test_type_error_datetime(self): + with self.assertRaises(FieldTypeError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + "start_date": "afsa", + 'id_list': [1, 3], + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual('start_date', e.exception.field_name) + self.assertEqual('start_date should be of type {"type": "", "format": "%Y-%m-%d"}', + e.exception.message) + self.assertEqual({'expectedType': '{"type": "", "format": "%Y-%m-%d"}'}, + e.exception.error_data) + + def test_type_error_bool(self): + with self.assertRaises(FieldTypeError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + 'id_list': [1, 3], + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}] + , "location_check": 1}) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual('location_check', e.exception.field_name) + self.assertEqual("location_check should be of type ", e.exception.message) + self.assertEqual({'expectedType': ""}, e.exception.error_data) + + def test_value_error_value_list(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + 'id_list': [1, 4], + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_VALUE_NOT_IN_ALLOWED_LIST, e.exception.error_code) + self.assertEqual('id_list[1]', e.exception.field_name) + self.assertEqual(f'id_list[1] should be one of these - [0, 1, 2, 3]', e.exception.message) + self.assertEqual({"allowedValue": [0, 1, 2, 3]}, e.exception.error_data) + + def test_value_error_min_value(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': -1, + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MIN_RANGE_VIOLATED, e.exception.error_code) + self.assertEqual('pageNo', e.exception.field_name) + self.assertEqual('pageNo should be greater than or equal to 0', e.exception.message) + self.assertEqual({'minValue': 0}, e.exception.error_data) + + def test_value_error_max_value(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 11, + "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MAX_RANGE_VIOLATED, e.exception.error_code) + self.assertEqual('pageNo', e.exception.field_name) + self.assertEqual('pageNo should be lesser than or equal to 10', e.exception.message) + self.assertEqual({'maxValue': 10}, e.exception.error_data) + + def test_value_error_min_value_1(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, + "location": [{"address_line_1": "fad", "pincode": 6544554, "latitude": "-91", "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MIN_RANGE_VIOLATED, e.exception.error_code) + self.assertEqual('location[0].latitude', e.exception.field_name) + self.assertEqual('location[0].latitude should be greater than or equal to -90', e.exception.message) + self.assertEqual({'minValue': Decimal("-90")}, e.exception.error_data) + + def test_value_error_max_value_1(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + "location": [{"address_line_1": "fad", "pincode": 6544554, "latitude": "91", "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MAX_RANGE_VIOLATED, e.exception.error_code) + self.assertEqual('location[0].latitude', e.exception.field_name) + self.assertEqual('location[0].latitude should be lesser than or equal to 90', e.exception.message) + self.assertEqual({'maxValue': Decimal("90")}, e.exception.error_data) + + def test_value_error_max_value_2(self): + start_date = (date.today() + timedelta(days=11)).strftime('%Y-%m-%d') + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + "start_date": start_date, "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}]}) + self.assertEqual(ErrorCode.FIELD_MAX_RANGE_VIOLATED, e.exception.error_code) + self.assertEqual('start_date', e.exception.field_name) + self.assertEqual(f'start_date should be lesser than or equal to {date.today() + timedelta(days=10)}', + e.exception.message) + self.assertEqual({'maxValue': date.today() + timedelta(days=10)}, e.exception.error_data) + + def test_length_error_1(self): + with self.assertRaises(FieldValueError) as e: + auto_cast_class_instance.test( + {'request_id': str(uuid4()), + 'pageNo': 1, + "location": [{"address_line_1": "fad", "pincode": "6544554", "contact_person": { "first_name": "saba" , "phone_number": "8884233317" }}]}) @@ -342,7 +682,7 @@ def test_length_error_1(self): def test_length_error_2(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + auto_cast_class_instance.test( {'request_id': str(uuid4()), 'pageNo': 1, "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { @@ -356,7 +696,7 @@ def test_length_error_2(self): def test_regex_error(self): with self.assertRaises(FieldValueError) as e: - class_instance.test( + auto_cast_class_instance.test( {'request_id': str(uuid4()), "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { "first_name": "sabari" @@ -369,7 +709,7 @@ def test_regex_error(self): def test_validator_error(self): with self.assertRaises(FieldError) as e: - class_instance.test( + auto_cast_class_instance.test( {'request_id': 'fasdfsdaf', "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { "first_name": "sabari" @@ -381,7 +721,7 @@ def test_validator_error(self): def test_field_validation_list(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test({ + auto_cast_class_instance.test({ "request_id": str(uuid4()) , "location": {"address_line_1": "fad", "pincode": 6544554, "contact_person": { "first_name": "sabari" @@ -394,7 +734,7 @@ def test_field_validation_list(self): def test_field_validation_dict(self): with self.assertRaises(FieldTypeError) as e: - class_instance.test({ + auto_cast_class_instance.test({ "request_id": str(uuid4()) , "location": {"address_line_1": "fad", "pincode": 6544554, "contact_person": { "first_name": "sabari" @@ -406,6 +746,20 @@ def test_field_validation_dict(self): self.assertEqual("name", e.exception.field_name) self.assertEqual("name should be of type ", e.exception.message) + def test_cast_error(self): + with self.assertRaises(FieldTypeError) as e: + auto_cast_class_instance.test({ + "request_id": str(uuid4()) + , "location": [{"address_line_1": "fad", "pincode": 6544554, "contact_person": { + "first_name": "sabari" + , "phone_number": "8884233317" + }}] + , "name": "fasdf" + }) + self.assertEqual(ErrorCode.ERRONEOUS_FIELD_TYPE, e.exception.error_code) + self.assertEqual("name", e.exception.field_name) + self.assertEqual("name should be of type ", e.exception.message) + if __name__ == '__main__': unittest.main() diff --git a/funcargpreprocessor/funcargpreprocesser.py b/funcargpreprocessor/funcargpreprocesser.py index 54c6ff5..38f3a5d 100644 --- a/funcargpreprocessor/funcargpreprocesser.py +++ b/funcargpreprocessor/funcargpreprocesser.py @@ -16,9 +16,10 @@ class FunctionArgPreProcessor: - def __init__(self, definition, is_strict=True): + def __init__(self, definition, is_strict=True, auto_type_cast=False): self.is_strict = is_strict self.definition = self.validate_type_definition(definition) + self.auto_type_cast = auto_type_cast def __call__(self, func_obj): @wraps(func_obj) @@ -90,6 +91,29 @@ def parse_value(self, key, value, data_type, nested, **value_constraints): self.check_constraint(value, key, **value_constraints) return value + def type_cast(self, value, data_type): + """ + To check if the value is of expected data type if not type cast it to the required datatype. Supports type cast + for BaseParams as well + :param value: Value from the request + :param data_type: Expected data type of the param + :return: type casted value + """ + if isinstance(data_type, BaseArg): + value = data_type(value) + elif isinstance(value, data_type) is False: + if self.auto_type_cast and isinstance(value, str) and data_type in (int, bool, float): + if data_type is bool: + value = value.lower() + if value not in {"true", "false"}: + raise Exception() + value = True if value == "true" else False + else: + value = data_type(value) + else: + raise Exception() + return value + @classmethod def check_constraint(cls, value, key, min_val=None, max_val=None, value_list=None, regex=None, regex_error_message=None, min_len=None, max_len=None): @@ -149,21 +173,6 @@ def validate_type_definition(type_definition): validator = type_definition.get('validator') return type_definition - @staticmethod - def type_cast(value, data_type): - """ - To check if the value is of expected data type if not type cast it to the required datatype. Supports type cast - for BaseParams as well - :param value: Value from the request - :param data_type: Expected data type of the param - :return: type casted value - """ - if isinstance(data_type, BaseArg): - value = data_type(value) - elif isinstance(value, data_type) is False: - raise Exception() - return value - @staticmethod def is_non_empty_value(value): """ diff --git a/setup.py b/setup.py index 67200c8..43125b1 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ long_description = fh.read() setup(name='funcargpreprocessor', - version='0.9.3', + version='0.10.0', python_requires='>=3.6', description='Parser for function arguments', url='https://github.com/sabariramc/funcargpreprocessor', @@ -29,6 +29,7 @@ 'Operating System :: OS Independent', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', 'Topic :: Software Development :: Libraries :: Python Modules' , "License :: OSI Approved :: MIT License" ]) diff --git a/testimplementation.py b/testimplementation.py index 40d0c37..af3e937 100644 --- a/testimplementation.py +++ b/testimplementation.py @@ -19,3 +19,10 @@ def inner_get_fu(fu): return FuncArgParser(query_param_definition, is_strict=is_strict)(fu) return inner_get_fu + + +def parse_function_args_with_auto_type_cast(query_param_definition, is_strict=False): + def inner_get_fu(fu): + return FuncArgParser(query_param_definition, is_strict=is_strict, auto_type_cast=True)(fu) + + return inner_get_fu