Skip to content

Commit

Permalink
Fix the invocation of function with gstring (#5020)
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso authored May 22, 2024
1 parent 42496db commit 04317f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package nextflow.script

import java.lang.reflect.Method

import groovy.transform.CompileStatic
/**
Expand Down Expand Up @@ -58,10 +57,22 @@ class FunctionDef extends ComponentDef implements ChainableDef {
final meta = target.metaClass.getMetaMethod(name, arr)
if( meta == null )
throw new MissingMethodException(name, target.getClass(), arr)
Method callMethod = target.getClass().getMethod(name, meta.getNativeParameterTypes())
final types = meta.getNativeParameterTypes()
final callMethod = target.getClass().getMethod(name, types)
if( callMethod == null )
throw new MissingMethodException(name, target.getClass(), arr)
return callMethod.invoke(target, arr)
return callMethod.invoke(target, coerce0(arr,types))
}

static protected Object[] coerce0(Object[] arr, Class[] types) {
assert arr.length==types.length
// when the argument is a GString and type declared is a String
// force the evaluation of the string to invoke the target function
for( int i=0; i<arr.length; i++ ){
if( arr[i] instanceof GString && types[i]==String.class )
arr[i] = ((GString)arr[i]).toString()
}
return arr
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,31 @@ class ScriptIncludesTest extends Dsl2Spec {
def e = thrown(IllegalStateException)
e.message == "Include statement is not allowed within a workflow definition"

}

def 'should should allow invoking function passing gstring' () {
given:
def folder = Files.createTempDirectory('test')
def MODULE = folder.resolve('module.nf')

MODULE.text = '''
def alpha(String str) {
return str.reverse()
}
'''

when:
def SCRIPT = """
include { alpha } from "$MODULE"
def x = "world"
def y = "Hello \$x"
return alpha(y)
"""
def runner = new MockScriptRunner()
def result = runner.setScript(SCRIPT).execute()
then:
result == 'dlrow olleH'
}
}

0 comments on commit 04317f0

Please sign in to comment.