|
| 1 | +from typing import Optional, Any, Dict, Tuple, List, Callable |
| 2 | +from azure.functions._durable_functions import _deserialize_custom_object |
| 3 | +import json |
| 4 | + |
| 5 | + |
| 6 | +class DurableEntityContext: |
| 7 | + """Context of the durable entity context. |
| 8 | +
|
| 9 | + Describes the API used to specify durable entity user code. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self, |
| 13 | + name: str, |
| 14 | + key: str, |
| 15 | + exists: bool, |
| 16 | + state: Any): |
| 17 | + """Context of the durable entity context. |
| 18 | +
|
| 19 | + Describes the API used to specify durable entity user code. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + name: str |
| 24 | + The name of the Durable Entity |
| 25 | + key: str |
| 26 | + The key of the Durable Entity |
| 27 | + exists: bool |
| 28 | + Flag to determine if the entity exists |
| 29 | + state: Any |
| 30 | + The internal state of the Durable Entity |
| 31 | + """ |
| 32 | + self._entity_name: str = name |
| 33 | + self._entity_key: str = key |
| 34 | + |
| 35 | + self._exists: bool = exists |
| 36 | + self._is_newly_constructed: bool = False |
| 37 | + |
| 38 | + self._state: Any = state |
| 39 | + self._input: Any = None |
| 40 | + self._operation: Optional[str] = None |
| 41 | + self._result: Any = None |
| 42 | + |
| 43 | + @property |
| 44 | + def entity_name(self) -> str: |
| 45 | + """Get the name of the Entity. |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + str |
| 50 | + The name of the entity |
| 51 | + """ |
| 52 | + return self._entity_name |
| 53 | + |
| 54 | + @property |
| 55 | + def entity_key(self) -> str: |
| 56 | + """Get the Entity key. |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + str |
| 61 | + The entity key |
| 62 | + """ |
| 63 | + return self._entity_key |
| 64 | + |
| 65 | + @property |
| 66 | + def operation_name(self) -> Optional[str]: |
| 67 | + """Get the current operation name. |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + Optional[str] |
| 72 | + The current operation name |
| 73 | + """ |
| 74 | + if self._operation is None: |
| 75 | + raise Exception("Entity operation is unassigned") |
| 76 | + return self._operation |
| 77 | + |
| 78 | + @property |
| 79 | + def is_newly_constructed(self) -> bool: |
| 80 | + """Determine if the Entity was newly constructed. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + bool |
| 85 | + True if the Entity was newly constructed. False otherwise. |
| 86 | + """ |
| 87 | + # This is not updated at the moment, as its semantics are unclear |
| 88 | + return self._is_newly_constructed |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def from_json(cls, json_str: str) -> Tuple['DurableEntityContext', List[Dict[str, Any]]]: |
| 92 | + """Instantiate a DurableEntityContext from a JSON-formatted string. |
| 93 | +
|
| 94 | + Parameters |
| 95 | + ---------- |
| 96 | + json_string: str |
| 97 | + A JSON-formatted string, returned by the durable-extension, |
| 98 | + which represents the entity context |
| 99 | +
|
| 100 | + Returns |
| 101 | + ------- |
| 102 | + DurableEntityContext |
| 103 | + The DurableEntityContext originated from the input string |
| 104 | + """ |
| 105 | + json_dict = json.loads(json_str) |
| 106 | + json_dict["name"] = json_dict["self"]["name"] |
| 107 | + json_dict["key"] = json_dict["self"]["key"] |
| 108 | + json_dict.pop("self") |
| 109 | + |
| 110 | + serialized_state = json_dict["state"] |
| 111 | + if serialized_state is not None: |
| 112 | + json_dict["state"] = from_json_util(serialized_state) |
| 113 | + |
| 114 | + batch = json_dict.pop("batch") |
| 115 | + return cls(**json_dict), batch |
| 116 | + |
| 117 | + def set_state(self, state: Any) -> None: |
| 118 | + """Set the state of the entity. |
| 119 | +
|
| 120 | + Parameter |
| 121 | + --------- |
| 122 | + state: Any |
| 123 | + The new state of the entity |
| 124 | + """ |
| 125 | + self._exists = True |
| 126 | + |
| 127 | + # should only serialize the state at the end of the batch |
| 128 | + self._state = state |
| 129 | + |
| 130 | + def get_state(self, initializer: Optional[Callable[[], Any]] = None) -> Any: |
| 131 | + """Get the current state of this entity. |
| 132 | +
|
| 133 | + Parameters |
| 134 | + ---------- |
| 135 | + initializer: Optional[Callable[[], Any]] |
| 136 | + A 0-argument function to provide an initial state. Defaults to None. |
| 137 | +
|
| 138 | + Returns |
| 139 | + ------- |
| 140 | + Any |
| 141 | + The current state of the entity |
| 142 | + """ |
| 143 | + state = self._state |
| 144 | + if state is not None: |
| 145 | + return state |
| 146 | + elif initializer: |
| 147 | + if not callable(initializer): |
| 148 | + raise Exception("initializer argument needs to be a callable function") |
| 149 | + state = initializer() |
| 150 | + return state |
| 151 | + |
| 152 | + def get_input(self) -> Any: |
| 153 | + """Get the input for this operation. |
| 154 | +
|
| 155 | + Returns |
| 156 | + ------- |
| 157 | + Any |
| 158 | + The input for the current operation |
| 159 | + """ |
| 160 | + input_ = None |
| 161 | + req_input = self._input |
| 162 | + req_input = json.loads(req_input) |
| 163 | + input_ = None if req_input is None else from_json_util(req_input) |
| 164 | + return input_ |
| 165 | + |
| 166 | + def set_result(self, result: Any) -> None: |
| 167 | + """Set the result (return value) of the entity. |
| 168 | +
|
| 169 | + Paramaters |
| 170 | + ---------- |
| 171 | + result: Any |
| 172 | + The result / return value for the entity |
| 173 | + """ |
| 174 | + self._exists = True |
| 175 | + self._result = result |
| 176 | + |
| 177 | + def destruct_on_exit(self) -> None: |
| 178 | + """Delete this entity after the operation completes.""" |
| 179 | + self._exists = False |
| 180 | + self._state = None |
| 181 | + |
| 182 | +def from_json_util(self, json_str: str) -> Any: |
| 183 | + """Load an arbitrary datatype from its JSON representation. |
| 184 | +
|
| 185 | + The Out-of-proc SDK has a special JSON encoding strategy |
| 186 | + to enable arbitrary datatypes to be serialized. This utility |
| 187 | + loads a JSON with the assumption that it follows that encoding |
| 188 | + method. |
| 189 | +
|
| 190 | + Parameters |
| 191 | + ---------- |
| 192 | + json_str: str |
| 193 | + A JSON-formatted string, from durable-extension |
| 194 | +
|
| 195 | + Returns |
| 196 | + ------- |
| 197 | + Any: |
| 198 | + The original datatype that was serialized |
| 199 | + """ |
| 200 | + return json.loads(json_str, object_hook=_deserialize_custom_object) |
0 commit comments