forked from microsoft/durabletask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_instance_id.py
More file actions
87 lines (73 loc) · 2.61 KB
/
Copy pathentity_instance_id.py
File metadata and controls
87 lines (73 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
class EntityInstanceId:
def __init__(self, entity: str, key: str):
EntityInstanceId.validate_entity_name(entity)
EntityInstanceId.validate_key(key)
self.entity = entity.lower()
self.key = key
def __str__(self) -> str:
return f"@{self.entity}@{self.key}"
def __eq__(self, other: object) -> bool:
if not isinstance(other, EntityInstanceId):
return False
return self.entity == other.entity and self.key == other.key
def __lt__(self, other: object) -> bool:
if not isinstance(other, EntityInstanceId):
return NotImplemented
return str(self) < str(other)
@staticmethod
def parse(entity_id: str) -> "EntityInstanceId":
"""Parse a string representation of an entity ID into an EntityInstanceId object.
Parameters
----------
entity_id : str
The string representation of the entity ID, in the format '@entity@key'.
Returns
-------
EntityInstanceId
The parsed EntityInstanceId object.
Raises
------
ValueError
If the input string is not in the correct format.
"""
if not entity_id.startswith("@"):
raise ValueError("Entity ID must start with '@'.")
try:
_, entity, key = entity_id.split("@", 2)
except ValueError as ex:
raise ValueError(f"Invalid entity ID format: {entity_id}") from ex
return EntityInstanceId(entity=entity, key=key)
@staticmethod
def validate_entity_name(name: str) -> None:
"""Validate that the entity name does not contain invalid characters.
Parameters
----------
name : str
The entity name to validate.
Raises
------
ValueError
If the name is not a valid entity name.
"""
if not name:
raise ValueError("Entity name cannot be empty.")
if "@" in name:
raise ValueError("Entity name cannot contain '@' symbol.")
@staticmethod
def validate_key(key: str) -> None:
"""Validate that the entity key does not contain invalid characters.
Parameters
----------
key : str
The entity key to validate.
Raises
------
ValueError
If the key is not a valid entity key.
"""
if not key:
raise ValueError("Entity key cannot be empty.")
if "@" in key:
raise ValueError("Entity key cannot contain '@' symbol.")