Coverage for src/kwai/core/domain/value_objects/identifier.py: 83%
30 statements
« prev ^ index » next coverage.py v7.7.1, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2024-01-01 00:00 +0000
1"""Module that defines identifiers."""
3from abc import ABC, abstractmethod
5from kwai.core.domain.value_objects.unique_id import UniqueId
8class Identifier[T](ABC):
9 """Abstract and generic class for an identifier."""
11 def __init__(self, id_: T):
12 self._id = id_
14 @property
15 def value(self) -> T:
16 """Return the id."""
17 return self._id
19 def __eq__(self, other: object):
20 """Check the equality of identifiers."""
21 if isinstance(other, Identifier):
22 return self._id == other._id
23 return False
25 def __hash__(self):
26 """Create a hash for an identifier."""
27 return hash(self._id)
29 @abstractmethod
30 def is_empty(self) -> bool:
31 """Return true when the identifier is not set."""
32 raise NotImplementedError
34 def __str__(self) -> str:
35 """Return the string representation of the id."""
36 return str(self._id)
39class IntIdentifier(Identifier[int]):
40 """Class that implements an identifier with an integer."""
42 def __init__(self, id_: int = 0):
43 super().__init__(id_)
45 def is_empty(self) -> bool:
46 """Return True when the id equals 0."""
47 return self.value == 0
49 def __repr__(self):
50 """Return a string representation of the identifier."""
51 return f"IntIdentifier={self.value}"
54class UniqueIdIdentifier(Identifier[UniqueId]):
55 """Class that implements an identifier with a UniqueId."""
57 def __init__(self, id_: UniqueId | None = None):
58 super().__init__(id_ or UniqueId.generate())
60 def is_empty(self) -> bool:
61 """This identifier type is never empty."""
62 return False
64 def __repr__(self):
65 """Return a string representation of the identifier."""
66 return f"UniqueIdIdentifier={self.value}"