Skip to content

Commit

Permalink
Support methods with closures to be compatible with Nextflow
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Oct 6, 2023
1 parent a451dee commit 55510d0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/main/java/com/askimed/nf/test/lang/ParamsMap.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.askimed.nf.test.lang;

import java.beans.Transient;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;

import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerHelper;

import groovy.json.JsonSlurper;
import groovy.lang.Closure;
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import groovy.text.SimpleTemplateEngine;
import groovy.yaml.YamlSlurper;

public class ParamsMap extends HashMap<String, Object> {
public class ParamsMap extends HashMap<String, Object> implements GroovyObject {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -97,7 +102,7 @@ protected void evaluateNestedClosures(Map<String, Object> map) {
if (!(value instanceof Closure))
continue;

Map<String, Object> nestedMap = new HashMap<String, Object>();
ParamsMap nestedMap = new ParamsMap(context);
Closure closure = (Closure) value;

// use context as new owner and this object
Expand All @@ -114,4 +119,31 @@ protected void evaluateNestedClosures(Map<String, Object> map) {

}

// never persist the MetaClass
private transient MetaClass metaClass = getDefaultMetaClass();

@Override
@Transient
public MetaClass getMetaClass() {
return this.metaClass;
}

@Override
public void setMetaClass(/* @Nullable */ final MetaClass metaClass) {
this.metaClass = Optional.ofNullable(metaClass).orElseGet(this::getDefaultMetaClass);
}

private MetaClass getDefaultMetaClass() {
return InvokerHelper.getMetaClass(this.getClass());
}

public Object invokeMethod(String var1, Object var2) {
try {
return this.getMetaClass().invokeMethod(this, var1, var2);
} catch (groovy.lang.MissingMethodException e) {
put(var1, ((Object[]) var2)[0]);
return null;
}
}

}
59 changes: 59 additions & 0 deletions test-data/process/nested-params/process.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,65 @@ nextflow_process {
script "test-data/process/nested-params/process.nf"
process "TEST_PROCESS"

test("Test nested params with closure 0") {

when {

params {
var1 = "var1"
nested {
var2 = "var2"
nested {
var3 = "$baseDir"
}
}
}

process {
"""
input[0] = "var1"
input[1] = "var2"
input[2] = "$baseDir"
"""
}
}

then {
assert process.success
}

}

test("Test nested params with closure 0_1") {

when {

params {
var1 = "var1"
nested {
var2 = "var2"
nested {
var3 = baseDir
}
}
}

process {
"""
input[0] = "var1"
input[1] = "var2"
input[2] = "$baseDir"
"""
}
}

then {
assert process.success
}

}


test("Test nested params with closure 1") {

when {
Expand Down

0 comments on commit 55510d0

Please sign in to comment.