Groovy method naming convention or magic? - groovy

I try to create a small DSL, but i'm struggling with even simple stuff.
The following script gives me an error.
def DEMON(String input) {
['a': input]
}
DEMON 'Hello thingy' a
For some reasons, the parentheses around the parameters are not optional and i get an error.
This script runs fine:
def dEMON(String input) {
['a': input]
}
dEMON 'Hello thingy' a
Note: the only difference is the lowercase first char.
So what is going on here? Why are the scripts interpreted (compiled?) different? Is there some kind of method/class naming schemes i have to follow?
Update: The error message. I guess a Syntax error:
unexpected token: Hello thingy # line 4, column 7.

The groovy syntax is sometime complex, and the compiler use some rules to choose what it must do. One of this rule is simple : If a word starts with an uppercase, it's probably a class.
for example, f String is a syntax valid in groovy, and the compiler converts it to f(String.class).
you can use parenthesis to help groovy understand your DEMON is not a class but a method, DEMON('Hello thingy', a)

Related

Call groovy script dynamically in Apache Camel using doTry-doCatch

I'm building a route which calls a groovy script whose path is dynamically computed and, if the script can't be found, defaults to a generic, static script:
.doTry()
.toD("language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
The problem is that the exchange property consumerType is not resolved since the uri string parameter of toD is evaluated using groovy and not simple.
MultipleCompilationErrorsException -> startup failed:
Script_09b4150584d9e2c979353feee06897b5.groovy: 1: Unexpected input: 'scripts/${exchangeProperty.consumerType}' # line 1, column 20.
resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy
^
1 error
How can I obtain the desired behavior?
According to the error shown there, it seems Camel is not able to resolve the string you provided in the toD().
By default, the expression you pass to a dynamic to is evaluated as Simple language but, as described in To Dynamic Camel documentation, you can specify other languages for the dynamic evaluation.
In your case, you are trying to evaluate the endpoint with groovy language but then you're using Simple language to substitute a piece of the name of the script.
One solution I've found (yet not the best) would be to specify the language for the interpretation of the string as simple and then use language:groovy to specify the endpoint that will need to be called.
You could write something like this:
.doTry()
.toD("language:simple:language://groovy:resource:classpath:scripts/${exchangeProperty.consumerType}ResponseHandler.groovy")
.doCatch(FileNotFoundException.class)
.script().groovy("resource:classpath:scripts/defaultResponseHandler.groovy")
.end()
It seems to work, but I hope someone else comes up with a better solution.

What $() syntax means for Groovy language?

I found this in Groovy Syntax documentation at 4.6.1. Special cases:
As slashy strings were mostly designed to make regexp easier so a few
things that are errors in GStrings like $() or $5 will work with
slashy strings.
What $() syntax means? give some usage examples please
I also found it at Define the Contract Locally in the Repository of the Fraud Detection Service:
body([ // (4)
"client.id": $(regex('[0-9]{10}')),
loanAmount : 99999
])
but I don't understand what $() means when used with regex('[0-9]{10}').
It means nothing (or what you make of it). There are two places, you
are addressing, but they have nothing to do with each other.
The docs just mention this as "you can use slashy strings to write
things, that would give you an error with a GString" - the same is true
for just using '-Strings.
E.g.
"hello $()"
Gives this error:
unknown recognition error type: groovyjarjarantlr4.v4.runtime.LexerNoViableAltException
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/x.groovy: 1: token recognition error at: '(' # line 1, column 9.
"hello $()"
The parser either wants a { or any char, that is a valid first char
for a variable (neither ( nor 5 is).
The other place you encountered $() (in Spring cloud contract), this
is just a function with the name $.
Form the docs 8. Contract DSL:
You can set the properties inside the body either with the value method or, if you use the Groovy map notation, with $()
So this is just a function, with a very short name.
E.g. you can try this yourself:
void $(x) { println x }
$("Hello")

How to test if a string variable in Robot Framework is empty?

How to test if a string variable in Robot Framework is empty?
My first naïve attempt looked like this:
Run Keyword If ${myVar}!=${EMPTY}
but it failed:
Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)
I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:
${length}= Get Length ${Portfolio_ste}
Run Keyword If ${length} Go To Edit Portfolio
but is this really the best practice?
(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)
The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:
Run Keyword if 42!=
When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:
Run Keyword If "${myVar}"!="${EMPTY}"
Try Get Variable Value. It solved my problem.

Why can't groovy deal with curly braces?

In an Android Studio (and presumably ANY) gradle file, the following code works:
task build {
}
And one minor change causes a complete meltdown:
task build
{
}
This has come up in other threads before, but in the context of fixing the build files. My question is why can't gradle/groovy be made to deal with either bracing style? Many other languages cope just fine with it, so what's the big deal here?
It's actually all right there in the error message:
build file '.../build.gradle': 80: Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement;
solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...}, and also either remove the previous newline, or add an explicit semicolon ';' # line 80, column 1.
Because of a Groovy syntax sugar to make methods with a lambda as the last parameter look line language constructs, the following code blocks:
task build {}
task build2(type: Copy) {}
are equal to their more regular form:
task build({})
task build(type: Copy, {})
Now, you do not really want those curly braces there to delimit a regular code block, but a Groovy lambda, which should be passed as a parameter to the build method.
Yet from the looks of it, Groovy can't really decide if it really is a lambda being passed as a parameter to the method in the previous line or an unrelated code block when you put a newline in between. And there you go, an ambiguousness as described in the error message, right there.
Following the advice in the error message, you can also use the following syntax instead of the one where you are escaping the new-line character:
task build
{ ->
}
Finally, the task keyword used to invoke the dynamic method (named build in your example) is not Groovy specific, but a Gradle DSL feature.
In case anyone reading this is wondering, the work-around is simple enough.
task build \
{
}
I was just wondering as to the "why"...

Error on printIN function for groovy

I am fairly new to java programming and I am trying to learn groovy right now.
I am getting this weird error when I am entering a simple line of code of hello world.
I am buffered. I believe I have set the environment variables correctly
You have the method name wrong
It's println not printIn
If you look at the exception, it tells you what's wrong, and a list of possible solutions
I think that you are using the wrong letter:
println "hello"
with "L" lowercase

Resources