1212
1313import uuid_utils .compat as uuid
1414from django .db .models import QuerySet
15+ from django .utils .translation import gettext as _
1516
1617from common .utils .logger import maxkb_logger
1718from common .utils .rsa_util import rsa_long_decrypt
@@ -42,41 +43,28 @@ def get_field_value(value, kwargs):
4243 return get_reference (value .get ('value' ), kwargs )
4344
4445
45- def _coerce_by_type ( field_type , raw ):
46- if raw is None :
46+ def _convert_value ( _type , value ):
47+ if value is None :
4748 return None
48- t = (field_type or "" ).lower ()
49-
50- if t in ("string" , "str" , "text" ):
51- return str (raw )
52- if t in ("int" , "integer" ):
53- return int (raw )
54- if t in ("float" , "number" , "double" ):
55- return float (raw )
56- if t in ("bool" , "boolean" ):
57- if isinstance (raw , bool ):
58- return raw
59- if isinstance (raw , (int , float )):
60- return bool (raw )
61- if isinstance (raw , str ):
62- v = raw .strip ().lower ()
63- if v in ("true" , "1" , "yes" , "y" , "on" ):
64- return True
65- if v in ("false" , "0" , "no" , "n" , "off" ):
66- return False
67- raise ValueError (f"Cannot coerce { raw !r} to bool" )
68- if t in ("list" , "array" ):
69- if isinstance (raw , list ):
70- return raw
71- if isinstance (raw , tuple ):
72- return list (raw )
73- raise ValueError (f"Cannot coerce { raw !r} to list" )
74- if t in ("dict" , "object" , "json" ):
75- if isinstance (raw , dict ):
76- return raw
77- raise ValueError (f"Cannot coerce { raw !r} to dict" )
78-
79- return raw
49+
50+ if _type == 'int' :
51+ return int (value )
52+ if _type == 'boolean' :
53+ value = 0 if ['0' , '[]' ].__contains__ (value ) else value
54+ return bool (value )
55+ if _type == 'float' :
56+ return float (value )
57+ if _type == 'dict' :
58+ v = json .loads (value )
59+ if isinstance (v , dict ):
60+ return v
61+ raise Exception (_ ('type error' ))
62+ if _type == 'array' :
63+ v = json .loads (value )
64+ if isinstance (v , list ):
65+ return v
66+ raise Exception (_ ('type error' ))
67+ return value
8068
8169
8270def get_tool_execute_parameters (input_field_list , parameter_setting , kwargs ):
@@ -85,7 +73,7 @@ def get_tool_execute_parameters(input_field_list, parameter_setting, kwargs):
8573 parameters = {}
8674 for key , value in parameter_setting .items ():
8775 raw = get_field_value (value , kwargs )
88- parameters [key ] = _coerce_by_type (type_map .get (key ), raw )
76+ parameters [key ] = _convert_value (type_map .get (key ), raw )
8977 return parameters
9078
9179
0 commit comments