Merge pull request #73 from JCHacking/string-duration
feat: duration string parser
This commit was merged in pull request #73.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# Ruff version.
|
# Ruff version.
|
||||||
rev: v0.11.4
|
rev: v0.14.7
|
||||||
hooks:
|
hooks:
|
||||||
# Run the linter.
|
# Run the linter.
|
||||||
- id: ruff
|
- id: ruff
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ from jambo.exceptions import InvalidSchemaException
|
|||||||
from jambo.parser._type_parser import GenericTypeParser
|
from jambo.parser._type_parser import GenericTypeParser
|
||||||
from jambo.types.type_parser_options import TypeParserOptions
|
from jambo.types.type_parser_options import TypeParserOptions
|
||||||
|
|
||||||
from pydantic import AnyUrl, EmailStr
|
from pydantic import AnyUrl, EmailStr, TypeAdapter, ValidationError
|
||||||
from typing_extensions import Any, Unpack
|
from typing_extensions import Unpack
|
||||||
|
|
||||||
from datetime import date, datetime, time, timedelta
|
from datetime import date, datetime, time, timedelta
|
||||||
from ipaddress import IPv4Address, IPv6Address
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
@@ -62,37 +62,19 @@ class StringTypeParser(GenericTypeParser):
|
|||||||
if format_type in self.format_pattern_mapping:
|
if format_type in self.format_pattern_mapping:
|
||||||
mapped_properties["pattern"] = self.format_pattern_mapping[format_type]
|
mapped_properties["pattern"] = self.format_pattern_mapping[format_type]
|
||||||
|
|
||||||
if "examples" in mapped_properties:
|
try:
|
||||||
mapped_properties["examples"] = [
|
if "examples" in mapped_properties:
|
||||||
self._parse_example(example, format_type, mapped_type)
|
mapped_properties["examples"] = [
|
||||||
for example in mapped_properties["examples"]
|
TypeAdapter(mapped_type).validate_python(example)
|
||||||
]
|
for example in mapped_properties["examples"]
|
||||||
|
]
|
||||||
|
except ValidationError as err:
|
||||||
|
raise InvalidSchemaException(
|
||||||
|
f"Invalid example type for field {name}."
|
||||||
|
) from err
|
||||||
|
|
||||||
if "json_schema_extra" not in mapped_properties:
|
if "json_schema_extra" not in mapped_properties:
|
||||||
mapped_properties["json_schema_extra"] = {}
|
mapped_properties["json_schema_extra"] = {}
|
||||||
mapped_properties["json_schema_extra"]["format"] = format_type
|
mapped_properties["json_schema_extra"]["format"] = format_type
|
||||||
|
|
||||||
return mapped_type, mapped_properties
|
return mapped_type, mapped_properties
|
||||||
|
|
||||||
def _parse_example(
|
|
||||||
self, example: Any, format_type: str, mapped_type: type[Any]
|
|
||||||
) -> Any:
|
|
||||||
"""
|
|
||||||
Parse example from JSON Schema format to python format
|
|
||||||
:param example: Example Value
|
|
||||||
:param format_type: Format Type
|
|
||||||
:param mapped_type: Type to parse
|
|
||||||
:return: Example parsed
|
|
||||||
"""
|
|
||||||
match format_type:
|
|
||||||
case "date" | "time" | "date-time":
|
|
||||||
return mapped_type.fromisoformat(example)
|
|
||||||
case "duration":
|
|
||||||
# TODO: Implement duration parser
|
|
||||||
raise NotImplementedError
|
|
||||||
case "ipv4" | "ipv6":
|
|
||||||
return mapped_type(example)
|
|
||||||
case "uuid":
|
|
||||||
return mapped_type(example)
|
|
||||||
case _:
|
|
||||||
return example
|
|
||||||
|
|||||||
@@ -129,9 +129,7 @@ class SchemaConverter:
|
|||||||
:param ref_name: The name of the reference to get.
|
:param ref_name: The name of the reference to get.
|
||||||
:return: The cached reference, or None if not found.
|
:return: The cached reference, or None if not found.
|
||||||
"""
|
"""
|
||||||
cached_type = self._namespace_registry.get(
|
cached_type = self._namespace_registry.get(namespace, {}).get(ref_name)
|
||||||
namespace, {}
|
|
||||||
).get(ref_name)
|
|
||||||
|
|
||||||
if isinstance(cached_type, type):
|
if isinstance(cached_type, type):
|
||||||
return cached_type
|
return cached_type
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from jambo.parser import StringTypeParser
|
|||||||
|
|
||||||
from pydantic import AnyUrl, EmailStr
|
from pydantic import AnyUrl, EmailStr
|
||||||
|
|
||||||
import unittest
|
|
||||||
from datetime import date, datetime, time, timedelta, timezone
|
from datetime import date, datetime, time, timedelta, timezone
|
||||||
from ipaddress import IPv4Address, IPv6Address, ip_address
|
from ipaddress import IPv4Address, IPv6Address, ip_address
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
@@ -121,7 +120,7 @@ class TestStringTypeParser(TestCase):
|
|||||||
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
type_parsing, type_validator = parser.from_properties("placeholder", properties)
|
||||||
|
|
||||||
self.assertEqual(type_parsing, AnyUrl)
|
self.assertEqual(type_parsing, AnyUrl)
|
||||||
self.assertEqual(type_validator["examples"], ["test://domain/resource"])
|
self.assertEqual(type_validator["examples"], [AnyUrl("test://domain/resource")])
|
||||||
|
|
||||||
def test_string_parser_with_ip_formats(self):
|
def test_string_parser_with_ip_formats(self):
|
||||||
parser = StringTypeParser()
|
parser = StringTypeParser()
|
||||||
@@ -299,7 +298,6 @@ class TestStringTypeParser(TestCase):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@unittest.skip("Duration parsing not yet implemented")
|
|
||||||
def test_string_parser_with_timedelta_format(self):
|
def test_string_parser_with_timedelta_format(self):
|
||||||
parser = StringTypeParser()
|
parser = StringTypeParser()
|
||||||
|
|
||||||
@@ -315,9 +313,9 @@ class TestStringTypeParser(TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
type_validator["examples"],
|
type_validator["examples"],
|
||||||
[
|
[
|
||||||
timedelta(days=7),
|
timedelta(days=428, hours=4, minutes=5, seconds=6),
|
||||||
timedelta(minutes=30),
|
timedelta(minutes=30),
|
||||||
timedelta(hours=4, minutes=5, seconds=6),
|
timedelta(days=7),
|
||||||
timedelta(seconds=0.5),
|
timedelta(seconds=0.5),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1166,5 +1166,7 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
self.converter.clear_ref_cache(namespace=namespace)
|
self.converter.clear_ref_cache(namespace=namespace)
|
||||||
|
|
||||||
cleared_cached_model = self.converter.get_cached_ref("Person", namespace=namespace)
|
cleared_cached_model = self.converter.get_cached_ref(
|
||||||
|
"Person", namespace=namespace
|
||||||
|
)
|
||||||
self.assertIsNone(cleared_cached_model)
|
self.assertIsNone(cleared_cached_model)
|
||||||
|
|||||||
Reference in New Issue
Block a user