From 02844629ac1bdc1080a9f632df7234e29de5a4d2 Mon Sep 17 00:00:00 2001 From: atopilski Date: Wed, 20 Jan 2021 10:36:22 -0500 Subject: [PATCH] Maker --- pyfastogt/maker.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pyfastogt/maker.py diff --git a/pyfastogt/maker.py b/pyfastogt/maker.py new file mode 100644 index 0000000..fec5567 --- /dev/null +++ b/pyfastogt/maker.py @@ -0,0 +1,52 @@ +class Maker: + @classmethod + def make_entry(cls, json: dict): + cl = cls() + cl.update_entry(json) + return cl + + def update_entry(self, json: dict): + if json is None: + raise ValueError('Invalid input') + + @staticmethod + def check_required_type(field: str, expected, json: dict): + if json is None: + raise ValueError('Invalid input') + + value_field = json.get(field, None) + if value_field is None: + raise ValueError('Invalid input({0} required)'.format(field)) + + actual = type(value_field) + if not Maker._check_is_same_types(actual, expected): + raise ValueError('Invalid input field({0}) actual type: {1}, expected: {2}'.format(field, actual, expected)) + + return True, value_field + + @staticmethod + def check_optional_type(field: str, expected, json: dict): + if json is None: + raise ValueError('Invalid input') + + value_field = json.get(field, None) + if value_field is not None: # optional field + actual = type(value_field) + if not Maker._check_is_same_types(actual, expected): + raise ValueError( + 'Invalid input field({0}) actual type: {1}, expected: {2}'.format(field, actual, expected)) + + return True, value_field + + return False, None + + @staticmethod + def _check_is_same_types(actual, expected) -> bool: + if actual is expected: + return True + + if (int == actual or int == expected) and (float == actual or float == expected): + return True + + return False +