import re
from typing import Any, Dict, Pattern, Type
-from knot_resolver_manager.utils.modeling import BaseCustomType
+from knot_resolver_manager.utils.modeling import BaseValueType
-class IntBase(BaseCustomType):
+class IntBase(BaseValueType):
"""
Base class to work with integer value.
"""
return {"type": "integer"}
-class StrBase(BaseCustomType):
+class StrBase(BaseValueType):
"""
Base class to work with string value.
"""
from typing import Any, Dict, Optional, Type, Union
from knot_resolver_manager.datamodel.types.base_types import IntRangeBase, PatternBase, StrBase, UnitBase
-from knot_resolver_manager.utils.modeling import BaseCustomType
+from knot_resolver_manager.utils.modeling import BaseValueType
class IntNonNegative(IntRangeBase):
)
-class IPv4Address(BaseCustomType):
+class IPv4Address(BaseValueType):
_value: ipaddress.IPv4Address
def __init__(self, source_value: Any, object_path: str = "/") -> None:
}
-class IPv6Address(BaseCustomType):
+class IPv6Address(BaseValueType):
_value: ipaddress.IPv6Address
def __init__(self, source_value: Any, object_path: str = "/") -> None:
IPAddress = Union[IPv4Address, IPv6Address]
-class IPNetwork(BaseCustomType):
+class IPNetwork(BaseValueType):
_value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
def __init__(self, source_value: Any, object_path: str = "/") -> None:
}
-class IPv6Network96(BaseCustomType):
+class IPv6Network96(BaseValueType):
_value: ipaddress.IPv6Network
def __init__(self, source_value: Any, object_path: str = "/") -> None:
return {"type": "string"}
-class UncheckedPath(BaseCustomType):
+class UncheckedPath(BaseValueType):
"""
Wrapper around pathlib.Path object. Can represent pretty much any Path. No checks are
performed on the value. The value is taken as is.
## Creating custom type
-Custom types can be made by extending `BaseCustomType` class which is integrated to parsing and validating process.
+Custom types can be made by extending `BaseValueType` class which is integrated to parsing and validating process.
Use `DataValidationError` to rase exception during validation. `object_path` is used to track node in more complex/nested schemas and create useful logging message.
```python
-from .modeling import BaseCustomType
+from .modeling import BaseValueType
from .modeling.exceptions import DataValidationError
-class IntNonNegative(BaseCustomType):
+class IntNonNegative(BaseValueType):
def __init__(self, source_value: Any, object_path: str = "/") -> None:
super().__init__(source_value)
if isinstance(source_value, int) and not isinstance(source_value, bool):
-from .base_custom_type import BaseCustomType
from .base_schema import BaseSchema
+from .base_value_type import BaseValueType
from .parsing import ParsedTree, parse, parse_json, parse_yaml
__all__ = [
- "BaseCustomType",
+ "BaseValueType",
"BaseSchema",
"ParsedTree",
"parse",
from knot_resolver_manager.utils.functional import all_matches
-from .base_custom_type import BaseCustomType
+from .base_value_type import BaseValueType
from .exceptions import AggregateDataValidationError, DataDescriptionError, DataValidationError
from .parsing import ParsedTree
from .types import (
or is_dict(typ)
or is_list(typ)
or (inspect.isclass(typ) and issubclass(typ, Serializable))
- or (inspect.isclass(typ) and issubclass(typ, BaseCustomType))
+ or (inspect.isclass(typ) and issubclass(typ, BaseValueType))
or (inspect.isclass(typ) and issubclass(typ, BaseSchema))
or (is_optional(typ) and Serializable.is_serializable(get_optional_inner_type(typ)))
or (is_union(typ) and all_matches(Serializable.is_serializable, get_generic_type_arguments(typ)))
if isinstance(obj, Serializable):
return obj.to_dict()
- elif isinstance(obj, BaseCustomType):
+ elif isinstance(obj, BaseValueType):
return obj.serialize()
elif isinstance(obj, list):
if inspect.isclass(typ) and issubclass(typ, BaseSchema):
return typ.json_schema(include_schema_definition=False)
- elif inspect.isclass(typ) and issubclass(typ, BaseCustomType):
+ elif inspect.isclass(typ) and issubclass(typ, BaseValueType):
return typ.json_schema()
elif is_none_type(typ):
elif is_dict(typ):
key, val = get_generic_type_arguments(typ)
- if inspect.isclass(key) and issubclass(key, BaseCustomType):
+ if inspect.isclass(key) and issubclass(key, BaseValueType):
assert (
- key.__str__ is not BaseCustomType.__str__
- ), "To support derived 'BaseCustomType', __str__ must be implemented."
+ key.__str__ is not BaseValueType.__str__
+ ), "To support derived 'BaseValueType', __str__ must be implemented."
else:
assert key == str, "We currently do not support any other keys then strings"
# int
elif cls == int:
# we don't want to make an int out of anything else than other int
- # except for BaseCustomType class instances
- if is_obj_type(obj, int) or isinstance(obj, BaseCustomType):
+ # except for BaseValueType class instances
+ if is_obj_type(obj, int) or isinstance(obj, BaseValueType):
return int(obj)
raise DataValidationError(f"expected int, found {type(obj)}", object_path)
# str
elif cls == str:
# we are willing to cast any primitive value to string, but no compound values are allowed
- if is_obj_type(obj, (str, float, int)) or isinstance(obj, BaseCustomType):
+ if is_obj_type(obj, (str, float, int)) or isinstance(obj, BaseValueType):
return str(obj)
elif is_obj_type(obj, bool):
raise DataValidationError(
elif is_obj_type(obj, cls):
return obj
- # BaseCustomType subclasses
- elif inspect.isclass(cls) and issubclass(cls, BaseCustomType):
+ # BaseValueType subclasses
+ elif inspect.isclass(cls) and issubclass(cls, BaseValueType):
if isinstance(obj, cls):
# if we already have a custom value type, just pass it through
return obj
from typing import Any, Dict, Type
-class BaseCustomType:
+class BaseValueType:
"""
Subclasses of this class can be used as type annotations in 'DataParser'. When a value
is being parsed from a serialized format (e.g. JSON/YAML), an object will be created by
raise NotImplementedError(f"{type(self).__name__}'s' 'serialize()' not implemented.")
@classmethod
- def json_schema(cls: Type["BaseCustomType"]) -> Dict[Any, Any]:
+ def json_schema(cls: Type["BaseValueType"]) -> Dict[Any, Any]:
raise NotImplementedError()