Skip to content

Commit

Permalink
Update schema classes to allow empty datetime values (#1)
Browse files Browse the repository at this point in the history
(i.e support null values for date, datetime and time types)
  • Loading branch information
ehennestad committed Feb 24, 2024
1 parent ebc48d2 commit 916ee7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
downloads/
target/
sources/
sources_instances/

# Autosave files
*.asv
Expand Down
2 changes: 0 additions & 2 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@
print(f"Error while building schema {schema_file_path}: {e}")

save_resource_files(schema_version, schemas_file_paths)


23 changes: 20 additions & 3 deletions pipeline/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 # # #

Expand Down Expand Up @@ -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)']
Expand Down

0 comments on commit 916ee7e

Please sign in to comment.