瀏覽代碼

fix(segments): Support NoneType. (#6581)

-LAN- 9 月之前
父節點
當前提交
2bc0632d0d

+ 5 - 2
api/core/app/segments/__init__.py

@@ -1,11 +1,12 @@
 from .segment_group import SegmentGroup
-from .segments import Segment
+from .segments import NoneSegment, Segment
 from .types import SegmentType
 from .variables import (
     ArrayVariable,
     FileVariable,
     FloatVariable,
     IntegerVariable,
+    NoneVariable,
     ObjectVariable,
     SecretVariable,
     StringVariable,
@@ -23,5 +24,7 @@ __all__ = [
     'Variable',
     'SegmentType',
     'SegmentGroup',
-    'Segment'
+    'Segment',
+    'NoneSegment',
+    'NoneVariable',
 ]

+ 3 - 0
api/core/app/segments/factory.py

@@ -10,6 +10,7 @@ from .variables import (
     FileVariable,
     FloatVariable,
     IntegerVariable,
+    NoneVariable,
     ObjectVariable,
     SecretVariable,
     StringVariable,
@@ -39,6 +40,8 @@ def build_variable_from_mapping(m: Mapping[str, Any], /) -> Variable:
 
 
 def build_anonymous_variable(value: Any, /) -> Variable:
+    if value is None:
+        return NoneVariable(name='anonymous')
     if isinstance(value, str):
         return StringVariable(name='anonymous', value=value)
     if isinstance(value, int):

+ 17 - 0
api/core/app/segments/segments.py

@@ -43,6 +43,23 @@ class Segment(BaseModel):
         return self.value
 
 
+class NoneSegment(Segment):
+    value_type: SegmentType = SegmentType.NONE
+    value: None = None
+
+    @property
+    def text(self) -> str:
+        return 'null'
+
+    @property
+    def log(self) -> str:
+        return 'null'
+
+    @property
+    def markdown(self) -> str:
+        return 'null'
+
+
 class StringSegment(Segment):
     value_type: SegmentType = SegmentType.STRING
     value: str

+ 2 - 1
api/core/app/segments/types.py

@@ -14,4 +14,5 @@ class SegmentType(str, Enum):
     ARRAY_STRING = 'array[string]'
     ARRAY_NUMBER = 'array[number]'
     ARRAY_OBJECT = 'array[object]'
-    ARRAY_FILE = 'array[file]'
+    ARRAY_FILE = 'array[file]'
+    NONE = 'none'

+ 5 - 0
api/core/app/segments/variables.py

@@ -81,3 +81,8 @@ class SecretVariable(StringVariable):
     @property
     def log(self) -> str:
         return encrypter.obfuscated_token(self.value)
+
+
+class NoneVariable(Variable):
+    value_type: SegmentType = SegmentType.NONE
+    value: None = None

+ 13 - 1
api/tests/unit_tests/app/test_variables.py

@@ -2,14 +2,16 @@ import pytest
 from pydantic import ValidationError
 
 from core.app.segments import (
+    ArrayVariable,
     FloatVariable,
     IntegerVariable,
+    NoneVariable,
+    ObjectVariable,
     SecretVariable,
     SegmentType,
     StringVariable,
     factory,
 )
-from core.app.segments.variables import ArrayVariable, ObjectVariable
 
 
 def test_string_variable():
@@ -134,3 +136,13 @@ def test_variable_to_object():
     assert var.to_object() == 3.14
     var = SecretVariable(name='secret', value='secret_value')
     assert var.to_object() == 'secret_value'
+
+
+def test_build_a_object_variable_with_none_value():
+    var = factory.build_anonymous_variable(
+        {
+            'key1': None,
+        }
+    )
+    assert isinstance(var, ObjectVariable)
+    assert isinstance(var.value['key1'], NoneVariable)