Skip to content

Commit

Permalink
Null handling and optional values for integers, floats, and text para…
Browse files Browse the repository at this point in the history
…meters.
  • Loading branch information
jmchilton committed Nov 12, 2020
1 parent 690e5b5 commit 7214ecf
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/galaxy/tools/parameters/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -346,6 +350,7 @@ def to_dict(self, trans, other_values={}):
d = super().to_dict(trans)
d['area'] = self.area
d['datalist'] = self.datalist
d['optional'] = self.optional
return d


Expand Down
5 changes: 4 additions & 1 deletion lib/galaxy/tools/parameters/wrapped_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def _data_input_to_path(v):
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":
Expand Down
22 changes: 22 additions & 0 deletions test/functional/tools/expression_null_handling_float.xml
Original file line number Diff line number Diff line change
@@ -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>
22 changes: 22 additions & 0 deletions test/functional/tools/expression_null_handling_integer.xml
Original file line number Diff line number Diff line change
@@ -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>
26 changes: 26 additions & 0 deletions test/functional/tools/expression_null_handling_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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;&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>
3 changes: 3 additions & 0 deletions test/functional/tools/samples_tool_conf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,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" />
Expand Down

0 comments on commit 7214ecf

Please sign in to comment.