Using lambda expressions with Rythm Engine

59 Views Asked by At

I'm trying to use lambda expressions with Rythm Engine and it's working in most cases. However, it isn't working when I invoke a tag inside a lambda expression.

Simplest example:

@test(x -> blabla(x) )

@def test(java.util.function.Function f) {
    @f.apply("Test")
}

@def blabla(String text) {
    ...
    Test: @text
    ...
}

If I use string concatenation in the lambda expression, it works perfectly, but in my real code I need to invoke a complex tag.

Does anybody know how I can do it?

Thank you.

1

There are 1 best solutions below

0
Jose Luis Rm On

Finally, I found a solution.

Revising the generated java code, it can be seen that the function called "blabla" (sorry, stupid name) has the signature:

public org.rythmengine.utils.RawData blabla(String text)

So, the way to make it work is calling "RawData.toString()".

The solution is:

@test(x -> blabla( x.toString() )

@def test(java.util.function.Function<String, String> f) {
    @f.apply("Test")
}

@def blabla(String text) {
    ...
    Test: @text
    ...
}

To prevent unnecessary castings, I add the arguments "String, String" to the generic class "java.util.function.Function".

To test the code, it's useful to define the environment variable "home.tmp.dir" and use a raw CodeType:

Map<String, Object> conf = new HashMap<>();
conf.put("home.tmp.dir", "./rythm/"); // Generated code

RythmEngine engine = new RythmEngine(conf);
engine.prepare(ICodeType.DefImpl.RAW);