Skip to content

Commit 6fc973d

Browse files
committed
fix number to float conversion
1 parent ffa4134 commit 6fc973d

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

plugins/nf-validation/src/main/nextflow/validation/SamplesheetConverter.groovy

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ class SamplesheetConverter {
164164
if(metaNames) {
165165
for(name : metaNames) {
166166
meta[name] = (input != '' && input) ?
167-
transform(input, field) :
167+
castToType(input, field) :
168168
field['value']['default'] ?
169-
transform(field['value']['default'] as String, field) :
169+
castToType(field['value']['default'] as String, field) :
170170
null
171171
}
172172
}
173173
else {
174174
def inputFile = (input != '' && input) ?
175-
transform(input, field) :
175+
castToType(input, field) :
176176
field['value']['default'] ?
177-
transform(field['value']['default'] as String, field) :
177+
castToType(field['value']['default'] as String, field) :
178178
[]
179179
output.add(inputFile)
180180
}
@@ -240,14 +240,14 @@ class SamplesheetConverter {
240240
}
241241

242242
// Function to transform an input field from the samplesheet to its desired type
243-
private static transform(
243+
private static castToType(
244244
String input,
245245
Map.Entry<String, Map> field
246246
) {
247247
def String type = field['value']['type']
248248
def String key = field.key
249249

250-
// Check and convert string values
250+
// Convert string values
251251
if(type == "string" || !type) {
252252
def String result = input as String
253253

@@ -262,21 +262,38 @@ class SamplesheetConverter {
262262
return result
263263
}
264264

265-
// Check and convert integer values
266-
else if(type == "integer" || type == "number") {
265+
// Convert number values
266+
else if(type == "number") {
267+
try {
268+
def int result = input as int
269+
return result
270+
}
271+
catch (NumberFormatException e) {
272+
log.debug("Could not convert ${input} to an integer. Trying to convert to a float.")
273+
}
274+
275+
try {
276+
def float result = input as float
277+
return result
278+
}
279+
catch (NumberFormatException e) {
280+
log.debug("Could not convert ${input} to a float. Trying to convert to a double.")
281+
}
282+
283+
def double result = input as double
284+
return result
285+
}
267286

268-
// Stop conversion if there are errors (prevents unwanted exceptions)
269-
if(this.getErrors()){ return }
287+
// Convert integer values
288+
else if(type == "integer") {
270289

271-
// Convert the string value to an integer value and return it
272-
def Integer result = input as Integer
290+
def int result = input as int
273291
return result
274292
}
275293

276-
// Check and convert boolean values
294+
// Convert boolean values
277295
else if(type == "boolean") {
278296

279-
// Convert and return the boolean value
280297
if(input.toLowerCase() == "true") {
281298
return true
282299
}

0 commit comments

Comments
 (0)