Feature/adds const #30
@@ -3,6 +3,7 @@ from .allof_type_parser import AllOfTypeParser
|
||||
from .anyof_type_parser import AnyOfTypeParser
|
||||
from .array_type_parser import ArrayTypeParser
|
||||
from .boolean_type_parser import BooleanTypeParser
|
||||
from .const_type_parser import ConstTypeParser
|
||||
from .enum_type_parser import EnumTypeParser
|
||||
from .float_type_parser import FloatTypeParser
|
||||
from .int_type_parser import IntTypeParser
|
||||
@@ -14,6 +15,7 @@ from .string_type_parser import StringTypeParser
|
||||
__all__ = [
|
||||
"GenericTypeParser",
|
||||
"EnumTypeParser",
|
||||
"ConstTypeParser",
|
||||
"AllOfTypeParser",
|
||||
"AnyOfTypeParser",
|
||||
"ArrayTypeParser",
|
||||
|
||||
46
jambo/parser/const_type_parser.py
Normal file
46
jambo/parser/const_type_parser.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from jambo.parser._type_parser import GenericTypeParser
|
||||
from jambo.types.json_schema_type import JSONSchemaNativeTypes
|
||||
from jambo.types.type_parser_options import TypeParserOptions
|
||||
|
||||
from pydantic import AfterValidator
|
||||
from typing_extensions import Annotated, Any, Unpack
|
||||
|
||||
|
||||
class ConstTypeParser(GenericTypeParser):
|
||||
json_schema_type = "const"
|
||||
|
||||
def from_properties_impl(
|
||||
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
||||
):
|
||||
if "const" not in properties:
|
||||
raise ValueError(f"Const type {name} must have 'const' property defined.")
|
||||
|
||||
const_value = properties["const"]
|
||||
|
||||
if not isinstance(const_value, JSONSchemaNativeTypes):
|
||||
raise ValueError(
|
||||
f"Const type {name} must have 'const' value of allowed types: {JSONSchemaNativeTypes}."
|
||||
)
|
||||
|
||||
const_type = self._build_const_type(const_value)
|
||||
parsed_properties = {
|
||||
"default": const_value,
|
||||
"description": properties.get("description"),
|
||||
}
|
||||
|
||||
return const_type, parsed_properties
|
||||
|
||||
def _build_const_type(self, const_value):
|
||||
def _validate_const_value(value: Any) -> Any:
|
||||
if value != const_value:
|
||||
raise ValueError(
|
||||
f"Value must be equal to the constant value: {const_value}"
|
||||
)
|
||||
return value
|
||||
|
||||
return Annotated[
|
||||
type(const_value),
|
||||
AfterValidator(
|
||||
_validate_const_value
|
||||
)
|
||||
]
|
||||
@@ -1,4 +1,5 @@
|
||||
from jambo.parser._type_parser import GenericTypeParser
|
||||
from jambo.types.json_schema_type import JSONSchemaNativeTypes
|
||||
from jambo.types.type_parser_options import TypeParserOptions
|
||||
|
||||
from typing_extensions import Unpack
|
||||
@@ -9,16 +10,6 @@ from enum import Enum
|
||||
class EnumTypeParser(GenericTypeParser):
|
||||
json_schema_type = "enum"
|
||||
|
||||
allowed_types: tuple[type] = (
|
||||
str,
|
||||
int,
|
||||
float,
|
||||
bool,
|
||||
list,
|
||||
set,
|
||||
type(None),
|
||||
)
|
||||
|
||||
def from_properties_impl(
|
||||
self, name, properties, **kwargs: Unpack[TypeParserOptions]
|
||||
):
|
||||
@@ -31,10 +22,10 @@ class EnumTypeParser(GenericTypeParser):
|
||||
raise ValueError(f"Enum type {name} must have 'enum' as a list of values.")
|
||||
|
||||
if any(
|
||||
not isinstance(value, self.allowed_types) for value in enum_values
|
||||
not isinstance(value, JSONSchemaNativeTypes) for value in enum_values
|
||||
):
|
||||
raise ValueError(
|
||||
f"Enum type {name} must have 'enum' values of allowed types: {self.allowed_types}."
|
||||
f"Enum type {name} must have 'enum' values of allowed types: {JSONSchemaNativeTypes}."
|
||||
)
|
||||
|
||||
# Create a new Enum type dynamically
|
||||
|
||||
@@ -1,11 +1,24 @@
|
||||
from typing_extensions import Dict, List, Literal, TypedDict, Union
|
||||
|
||||
from types import NoneType
|
||||
|
||||
|
||||
JSONSchemaType = Literal[
|
||||
"string", "number", "integer", "boolean", "object", "array", "null"
|
||||
]
|
||||
|
||||
|
||||
JSONSchemaNativeTypes: tuple[type, ...] = (
|
||||
str,
|
||||
int,
|
||||
float,
|
||||
bool,
|
||||
list,
|
||||
set,
|
||||
NoneType,
|
||||
)
|
||||
|
||||
|
||||
JSONType = Union[str, int, float, bool, None, Dict[str, "JSONType"], List["JSONType"]]
|
||||
|
||||
|
||||
|
||||
23
tests/parser/test_const_type_parser.py
Normal file
23
tests/parser/test_const_type_parser.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from typing_extensions import Annotated, get_args, get_origin
|
||||
from webbrowser import get
|
||||
from jambo.parser import ConstTypeParser
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class TestConstTypeParser(TestCase):
|
||||
def test_parse_const_type(self):
|
||||
parser = ConstTypeParser()
|
||||
|
||||
expected_const_value = "United States of America"
|
||||
properties = {
|
||||
"const": expected_const_value
|
||||
}
|
||||
|
||||
parsed_type, parsed_properties = parser.from_properties(
|
||||
"country", properties
|
||||
)
|
||||
|
||||
self.assertEqual(get_origin(parsed_type), Annotated)
|
||||
|
||||
self.assertIn(str, get_args(parsed_type))
|
||||
Reference in New Issue
Block a user