Merge pull request #10703 from jmchilton/null_handling_2

Null handling and optional values for integers, floats, and text params.
This commit is contained in:
Marius van den Beek
2020-11-13 11:24:32 +01:00
committed by GitHub
6 changed files with 90 additions and 3 deletions
+7 -2
View File
@@ -302,12 +302,16 @@ class SimpleTextToolParameter(ToolParameter):
def __init__(self, tool, input_source):
input_source = ensure_input_source(input_source)
super().__init__(tool, input_source)
self.value = ''
self.optional = input_source.get_bool('optional', False)
if self.optional:
self.value = None
else:
self.value = ''
def to_json(self, value, app, use_security):
"""Convert a value to a string representation suitable for persisting"""
if value is None:
rval = ''
rval = '' if not self.optional else None
else:
rval = unicodify(value)
return rval
@@ -346,6 +350,7 @@ class TextToolParameter(SimpleTextToolParameter):
d = super().to_dict(trans)
d['area'] = self.area
d['datalist'] = self.datalist
d['optional'] = self.optional
return d
+4 -1
View File
@@ -86,7 +86,10 @@ def _json_wrap_input(input, value_wrapper, profile, handle_files="skip"):
return SKIP_INPUT
raise NotImplementedError()
elif input_type in ["text", "color", "hidden"]:
json_value = _cast_if_not_none(value_wrapper, str)
if getattr(input, "optional", False) and value_wrapper is not None and value_wrapper.value is None:
json_value = None
else:
json_value = _cast_if_not_none(value_wrapper, str)
elif input_type == "float":
json_value = _cast_if_not_none(value_wrapper, float, empty_to_none=True)
elif input_type == "integer":
@@ -0,0 +1,22 @@
<tool name="expression_null_handling_float" id="expression_null_handling_float"
version="0.1.0" tool_type="expression">
<expression type="ecma5.1">
{return {'float_out': $job.float_input};}
</expression>
<inputs>
<param type="float" label="Float input." name="float_input" optional="true" />
</inputs>
<outputs>
<output type="float" name="float_out" from="float_out" />
</outputs>
<tests>
<test>
<param name="float_input" value_json="7.2" />
<output name="float_out" value_json="7.2" />
</test>
<test>
<param name="float_input" value_json="null" />
<output name="float_out" value_json="null" />
</test>
</tests>
</tool>
@@ -0,0 +1,22 @@
<tool name="expression_null_handling_integer" id="expression_null_handling_integer"
version="0.1.0" tool_type="expression">
<expression type="ecma5.1">
{return {'int_out': $job.int_input};}
</expression>
<inputs>
<param type="integer" label="Integer input." name="int_input" optional="true" />
</inputs>
<outputs>
<output type="integer" name="int_out" from="int_out" />
</outputs>
<tests>
<test>
<param name="int_input" value_json="7" />
<output name="int_out" value_json="7" />
</test>
<test>
<param name="int_input" value_json="null" />
<output name="int_out" value_json="null" />
</test>
</tests>
</tool>
@@ -0,0 +1,32 @@
<tool name="expression_null_handling_text" id="expression_null_handling_text"
version="0.1.0" tool_type="expression">
<expression type="ecma5.1">
{return {'text_out': $job.text_input};}
</expression>
<inputs>
<param type="text" label="Text input." name="text_input" optional="true" />
</inputs>
<outputs>
<output type="text" name="text_out" from="text_out" />
</outputs>
<tests>
<test>
<param name="text_input" value_json="&quot;foo bar&quot;" />
<output name="text_out" value_json="&quot;foo bar&quot;" />
</test>
<test>
<param name="text_input" value_json="&quot;null&quot;" />
<output name="text_out" value_json="&quot;null&quot;" />
</test>
<!-- Bug with the following test on postgres.
<test>
<param name="text_input" value_json="&quot;&quot;" />
<output name="text_out" value_json="&quot;&quot;" />
</test>
-->
<test>
<param name="text_input" value_json="null" />
<output name="text_out" value_json="null" />
</test>
</tests>
</tool>
@@ -175,6 +175,9 @@
<tool file="expression_parse_int.xml" />
<tool file="expression_log_line_count.xml" />
<tool file="expression_null_handling_boolean.xml" />
<tool file="expression_null_handling_integer.xml" />
<tool file="expression_null_handling_float.xml" />
<tool file="expression_null_handling_text.xml" />
<tool file="cheetah_casting.xml" />
<tool file="cheetah_problem_unbound_var.xml" />
<tool file="cheetah_problem_unbound_var_input.xml" />