Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the invocation of function with gstring #5020

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
}