From 916ee7e6a6fc3266b51f8c0ae3d65a71a3bd8790 Mon Sep 17 00:00:00 2001 From: ehennestad Date: Sat, 24 Feb 2024 16:10:57 +0100 Subject: [PATCH] Update schema classes to allow empty datetime values (#1) (i.e support null values for date, datetime and time types) --- .gitignore | 1 + build.py | 2 -- pipeline/translator.py | 23 ++++++++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d7fc635f..528c4115 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ downloads/ target/ sources/ +sources_instances/ # Autosave files *.asv diff --git a/build.py b/build.py index 83572f1c..a9f73705 100644 --- a/build.py +++ b/build.py @@ -32,5 +32,3 @@ print(f"Error while building schema {schema_file_path}: {e}") save_resource_files(schema_version, schemas_file_paths) - - diff --git a/pipeline/translator.py b/pipeline/translator.py index 8948ab95..50854fce 100644 --- a/pipeline/translator.py +++ b/pipeline/translator.py @@ -33,7 +33,7 @@ "date": "datetime", "date-time": "datetime", "time": "datetime", - "email": "string", # could maybe use Pydantic or something to be stricter about this + "email": "string", "ECMA262": "string" } @@ -109,7 +109,7 @@ def _extract_template_variables(self): # Todo: Create method for getting name with right casing... schema_short_name = os.path.basename(schema[SCHEMA_PROPERTY_TYPE]) - + props = [] # List of template property attributes (shortened name to avoid very long lines) for full_name, property_info in sorted(schema["properties"].items(), key=_property_name_sort_key): @@ -152,6 +152,11 @@ def _extract_template_variables(self): if property_info.get("type") == 'integer': size_attribute = "(1,:)" + # ...ditto for date-time formats + if _is_datetime_format(property_info): + size_attribute = "(1,:)" + + # ...and for linked/embedded types if has_linked_type or has_embedded_type: size_attribute = "(1,:)" possible_types_docstr = [_create_matlab_help_link(type_str) for type_str in possible_types] @@ -343,6 +348,10 @@ def _property_name_sort_key(arg): def _strip_trailing_whitespace(s): return "\n".join([line.rstrip() for line in s.splitlines()]) +def _is_datetime_format(property_info): + return property_info.get("type") == 'string' \ + and property_info.get("_formats") \ + and any(item in ["date", "date-time", "time"] for item in property_info.get("_formats")) # # # LOCAL MATLAB SPECIFIC UTILITY FUNCTIONS # # # @@ -423,10 +432,18 @@ def _create_property_validator_functions(name, property_info): has_embedded_type = SCHEMA_PROPERTY_EMBEDDED_TYPES in property_info validation_functions = [] - property + if property_info.get("type") == 'integer': validation_functions += [f'mustBeSpecifiedLength({property_name}, 0, 1)'] + if _is_datetime_format(property_info): + validation_functions += [f'mustBeSpecifiedLength({property_name}, 0, 1)'] + + if "date" in property_info.get("_formats"): + validation_functions += [f"mustBeValidDate({property_name})"] + elif "time" in property_info.get("_formats") == "time": + validation_functions += [f"mustBeValidTime({property_name})"] + if has_linked_type or has_embedded_type: if not allow_multiple: validation_functions += [f'mustBeSpecifiedLength({property_name}, 0, 1)']