feat(api): Update variable handling for `VariableAssigner` nodes
- Put updated variables data in `process_data` - Use structured object to describe updated variables.pull/20699/head
parent
a3202356e6
commit
c604659287
@ -1,24 +1,36 @@
|
|||||||
from collections.abc import Sequence
|
from collections.abc import Mapping, MutableMapping, Sequence
|
||||||
from typing import Any, TypedDict
|
from typing import Any, TypeVar
|
||||||
|
|
||||||
from core.variables import Segment, SegmentType
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from core.variables import Segment
|
||||||
from core.variables.consts import MIN_SELECTORS_LENGTH
|
from core.variables.consts import MIN_SELECTORS_LENGTH
|
||||||
|
|
||||||
|
# Use double underscore (`__`) prefix for internal variables
|
||||||
|
# to minimize risk of collision with user-defined variable names.
|
||||||
|
_UPDATED_VARIABLES_KEY = "__updated_variables"
|
||||||
|
|
||||||
|
|
||||||
class VariableOutput(TypedDict):
|
class UpdatedVariable(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
selector: Sequence[str]
|
selector: Sequence[str]
|
||||||
new_value: Any
|
new_value: Segment
|
||||||
type: SegmentType
|
|
||||||
|
|
||||||
|
_T = TypeVar("_T", bound=MutableMapping[str, Any])
|
||||||
|
|
||||||
|
|
||||||
def variable_to_output_mapping(selector: Sequence[str], seg: Segment) -> VariableOutput:
|
def variable_to_processed_data(selector: Sequence[str], seg: Segment) -> UpdatedVariable:
|
||||||
if len(selector) < MIN_SELECTORS_LENGTH:
|
if len(selector) < MIN_SELECTORS_LENGTH:
|
||||||
raise Exception("selector too short")
|
raise Exception("selector too short")
|
||||||
node_id, var_name = selector[:2]
|
node_id, var_name = selector[:2]
|
||||||
return {
|
return UpdatedVariable(name=var_name, selector=list(selector[:2]), new_value=seg)
|
||||||
"name": var_name,
|
|
||||||
"selector": selector[:2],
|
|
||||||
"new_value": seg.value,
|
def set_updated_variables(m: _T, updates: Sequence[UpdatedVariable]) -> _T:
|
||||||
"type": seg.value_type,
|
# m[_UPDATED_VARIABLES_KEY] = updates
|
||||||
}
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
def get_updated_variables(m: Mapping[str, Any]) -> Sequence[UpdatedVariable] | None:
|
||||||
|
return m.get(_UPDATED_VARIABLES_KEY, None)
|
||||||
|
|||||||
Loading…
Reference in New Issue