]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: schema utils: updated documentation about serialization of custom value...
authorVasek Sraier <git@vakabus.cz>
Thu, 29 Sep 2022 09:45:33 +0000 (11:45 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 10 Jan 2023 18:57:13 +0000 (19:57 +0100)
manager/knot_resolver_manager/datamodel/types/types.py
manager/knot_resolver_manager/utils/modeling/base_schema.py
manager/knot_resolver_manager/utils/modeling/base_value_type.py

index 32d951b02caa1ab265df79b8bb13c38b716bdbd9..43e47a0ba639ad48797fb5f439473a85bcd6ada6 100644 (file)
@@ -412,6 +412,7 @@ class UncheckedPath(BaseValueType):
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
         super().__init__(source_value, object_path=object_path)
         if isinstance(source_value, str):
+            self._raw_value: str = source_value
             self._value: Path = Path(source_value)
         else:
             raise ValueError(f"expected file path in a string, got '{source_value}' with type '{type(source_value)}'.")
@@ -432,7 +433,7 @@ class UncheckedPath(BaseValueType):
         return self._value
 
     def serialize(self) -> Any:
-        return str(self._value)
+        return self._raw_value
 
     @classmethod
     def json_schema(cls: Type["UncheckedPath"]) -> Dict[Any, Any]:
index 17f003ee94122a57f564567838f068b0bcaefdc9..f279afa40fa05a65429b272b2eed8fdc1b0a6201 100644 (file)
@@ -1,5 +1,6 @@
 import enum
 import inspect
+from abc import ABC, abstractmethod
 from typing import Any, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union, cast
 
 import yaml
@@ -35,11 +36,12 @@ def is_obj_type(obj: Any, types: Union[type, Tuple[Any, ...], Tuple[type, ...]])
     return type(obj) == types
 
 
-class Serializable:
+class Serializable(ABC):
     """
     An interface for making classes serializable to a dictionary (and in turn into a JSON).
     """
 
+    @abstractmethod
     def to_dict(self) -> Dict[Any, Any]:
         raise NotImplementedError(f"...for class {self.__class__.__name__}")
 
index b3249752a817a61423833fd99af155f823cf4962..e5da922daefee0dc7fdfab24c9a3fe6597d2afcf 100644 (file)
@@ -1,7 +1,8 @@
+from abc import ABC, abstractmethod
 from typing import Any, Dict, Type
 
 
-class BaseValueType:
+class BaseValueType(ABC):
     """
     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
@@ -13,18 +14,23 @@ class BaseValueType:
     raise a `ValueError` in case of errors.
     """
 
+    @abstractmethod
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
         pass
 
+    @abstractmethod
     def __int__(self) -> int:
         raise NotImplementedError(f" return 'int()' value for {type(self).__name__} is not implemented.")
 
+    @abstractmethod
     def __str__(self) -> str:
         raise NotImplementedError(f"return 'str()' value for {type(self).__name__} is not implemented.")
 
+    @abstractmethod
     def serialize(self) -> Any:
         """
-        Every custom type should implement this. It is used for dumping configuration.
+        Used for dumping configuration. Returns a JSON-serializable object from which the object
+        can be recreated again using the constructor.
 
         It's not necessary to return the same structure that was given as an input. It only has
         to be the same semantically.
@@ -32,5 +38,6 @@ class BaseValueType:
         raise NotImplementedError(f"{type(self).__name__}'s' 'serialize()' not implemented.")
 
     @classmethod
+    @abstractmethod
     def json_schema(cls: Type["BaseValueType"]) -> Dict[Any, Any]:
         raise NotImplementedError()