feat(variable-handling): enhance variable and segment conversion (#10483)

This commit is contained in:
-LAN-
2024-11-12 21:51:09 +08:00
committed by GitHub
parent 9c7edb9242
commit 70c2ec8ed5
5 changed files with 91 additions and 18 deletions

View File

@@ -17,6 +17,7 @@ from .segments import (
from .types import SegmentType
from .variables import (
ArrayAnyVariable,
ArrayFileVariable,
ArrayNumberVariable,
ArrayObjectVariable,
ArrayStringVariable,
@@ -58,4 +59,5 @@ __all__ = [
"ArrayStringSegment",
"FileSegment",
"FileVariable",
"ArrayFileVariable",
]

View File

@@ -1,9 +1,13 @@
from collections.abc import Sequence
from uuid import uuid4
from pydantic import Field
from core.helper import encrypter
from .segments import (
ArrayAnySegment,
ArrayFileSegment,
ArrayNumberSegment,
ArrayObjectSegment,
ArrayStringSegment,
@@ -24,11 +28,12 @@ class Variable(Segment):
"""
id: str = Field(
default="",
description="Unique identity for variable. It's only used by environment variables now.",
default=lambda _: str(uuid4()),
description="Unique identity for variable.",
)
name: str
description: str = Field(default="", description="Description of the variable.")
selector: Sequence[str] = Field(default_factory=list)
class StringVariable(StringSegment, Variable):
@@ -78,3 +83,7 @@ class NoneVariable(NoneSegment, Variable):
class FileVariable(FileSegment, Variable):
pass
class ArrayFileVariable(ArrayFileSegment, Variable):
pass

View File

@@ -95,13 +95,16 @@ class VariablePool(BaseModel):
if len(selector) < 2:
raise ValueError("Invalid selector")
if isinstance(value, Variable):
variable = value
if isinstance(value, Segment):
v = value
variable = variable_factory.segment_to_variable(segment=value, selector=selector)
else:
v = variable_factory.build_segment(value)
segment = variable_factory.build_segment(value)
variable = variable_factory.segment_to_variable(segment=segment, selector=selector)
hash_key = hash(tuple(selector[1:]))
self.variable_dictionary[selector[0]][hash_key] = v
self.variable_dictionary[selector[0]][hash_key] = variable
def get(self, selector: Sequence[str], /) -> Segment | None:
"""