I wonder how I can pass the implicit variable 'it' to the closure.
See the following code:
def myfunc(String name, Closure cl)
{
println("name: ${name}")
cl.call()
}
list = ['a', 'b']
list.each
{
println("it1: ${it}")
}
list.each
{
myfunc("f1")
{
println("it2: ${it}")
}
}
list.each
{
p ->
myfunc("f2")
{
println("p: ${p}")
}
}
The resulting output is:
it1: a
it1: b
name: f1
it2: null
name: f1
it2: null
name: f2
p: a
name: f2
p: b
How can I achieve that in the second version (f1/it2) the implicit variable it is passed to the closure? Currently it is "null".
I want to achieve to have 'a', 'b' within 'it' in the second version.
The background of the question is, that within a Jenkins Pipeline, the following code
mylist = ['a1', 'a2']
mylist.each {
dir ("xxxx") {
echo "it in xxxx is ${it}"
}
}
prints out the values 'a1' and 'a2' for it.
This does not fit to the understanding that it is bound to the outermost closure.
In case of Jenkins the behaviour seems to be different.
I want to achieve the very same behaviour and I am wondering how to do this.
Thanks for all feedback!
Best regards
Mathias
dir from jenkins is not a function - it's a jenkins Step and your groovy function example is not applicable.
you have to check if it's achievable from groovy-defined custom steps https://rubix.nl/jenkins-creating-a-custom-pipeline-step-in-your-library/
or think about custom plugin creation.
btw, here is a source of dir jenkins step: https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/steps/PushdStep.java
Related
I would like to write a wrapper method for a webservice, the service accepts 2 mandatory and 3 optional parameters.
To have a shorter example, I would like to get the following code working
def myMethod(pParm1='1', pParm2='2') {
println "${pParm1}${pParm2}"
}
myMethod();
myMethod('a')
myMethod(pParm2:'a') // doesn't work as expected
myMethod('b','c')
The output is:
12
a2
[pParm2:a]2
a2
bc
What I would like to achieve is to give one parameter and get 1a as the result.
Is this possible (in the laziest way)?
Can't be done as it stands... The code
def myMethod(pParm1='1', pParm2='2'){
println "${pParm1}${pParm2}"
}
Basically makes groovy create the following methods:
Object myMethod( pParm1, pParm2 ) {
println "$pParm1$pParm2"
}
Object myMethod( pParm1 ) {
this.myMethod( pParm1, '2' )
}
Object myMethod() {
this.myMethod( '1', '2' )
}
One alternative would be to have an optional Map as the first param:
def myMethod( Map map = [:], String mandatory1, String mandatory2 ){
println "${mandatory1} ${mandatory2} ${map.parm1 ?: '1'} ${map.parm2 ?: '2'}"
}
myMethod( 'a', 'b' ) // prints 'a b 1 2'
myMethod( 'a', 'b', parm1:'value' ) // prints 'a b value 2'
myMethod( 'a', 'b', parm2:'2nd') // prints 'a b 1 2nd'
Obviously, documenting this so other people know what goes in the magical map and what the defaults are is left to the reader ;-)
You can use arguments with default values.
def someMethod(def mandatory,def optional=null){}
if argument "optional" not exist, it turns to "null".
Just a simplification of the Tim's answer. The groovy way to do it is using a map, as already suggested, but then let's put the mandatory parameters also in the map. This will look like this:
def someMethod(def args) {
println "MANDATORY1=${args.mandatory1}"
println "MANDATORY2=${args.mandatory2}"
println "OPTIONAL1=${args?.optional1}"
println "OPTIONAL2=${args?.optional2}"
}
someMethod mandatory1:1, mandatory2:2, optional1:3
with the output:
MANDATORY1=1
MANDATORY2=2
OPTIONAL1=3
OPTIONAL2=null
This looks nicer and the advantage of this is that you can change the order of the parameters as you like.
We can Deal with Optional parameters in 2 ways
Creating the method parameter with null values:
def generateReview(def id, def createDate=null) {
return new Review(id, createDate ?: new Date()) // ?: short hand of ternary operator
}
generateReview(id) // createDate is not passed
generateReview(id, createDate) // createDate is passed
Using Java Optional.of()
def generateReview(def id, Optional<Date> createDate) {
return new Review(id, createDate.isPresent() ? createDate.get() : new Date())
}
generateReview(id, Optional.empty()) // createDate is not passed
generateReview(id, Optional.of(createDate)) // createDate is passed
I have the following Groovy script:
def Deploy() {
if (App == "TEST"){
def book = load "book.groovy"
book.buildList.each {
a lot of actions
}
else {
book.each {
the same a lot of actions
}
}
So the difference only in execution methods (properties): book.buildList.each or book.each. How to avoid to repeat those a lot of actions and keep code cleaner. Probably there is a way to put book.buildList.each or book.each into the variable?
File Book.groovy contain few map:
buildList = [
'key1':'value1',
'key2':'value2',
'key3':'value3',
]
anotherList = [
'key11':'value11',
'key22':'value22',
'key33':'value33',
]
return this
But if App not "TEST" I have book map:
[
'key1':'value1',
'key2':'value2',
'key3':'value3',
]
If both each invocations do the exact same thing, you can extract the body of the closure you pass to each method to a variable and share it between both invocations. Consider the following (simplified) example:
def closure = { k, v ->
println "k = $k, v = $v"
}
def a = [
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
]
def b = [
'key11': 'value11',
'key22': 'value22',
'key33': 'value33',
]
println "Calling common closure on the map a:"
a.each closure
println "Calling common closure on the map b:"
b.each closure
In this example, I common closure to the variable, and then I was able to re-use it in both iterations. Also, keep in mind that in your example you are using maps, not lists. Calling each on map is absolutely correct, and in this case, you can use a closure with k and v parameters to map key and value accurately.
I'm trying to implement the code inspired from here with << operator in the following way:
builder = { name, param1, param2 ->
[job: name, parameters: [string(name: 'Param1', value: param1), string(name: 'Param2', value: param2)], quietPeriod: 2, wait: false]
}
node {
stage('Tests') {
def testBuilds = [:]
testBuilds << build *builder('Test', 'Foo', 'Bar')
testBuilds << build *builder('Test', 'Foo2', 'Bar2')
parallel testBuilds
}
}
where I expect to append two jobs into testBuilds map in order to run them in parallel.
However when running the job, I've got the following exception error:
groovy.lang.MissingPropertyException: No such property: build for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:24)
...
What would be the correct syntax using the above approach?
You've got few problems in your code:
Branches description testBuild is a map (key: value) object, not a list. So you cannot left shift (<<) values into it the way you did, such operation is not supported for map
Jenkins pipeline parallel operation expects map with closure as its values.
build is part of pipeline DSL, not a common Groovy method. It appears it cannot accept arguments this way. While the expression build *builder(foo, bar) passes syntax check at first, omitting round brackets is just groovy syntactic sugar. Rewriting this line as build(*builder(foo, bar)) generates syntax error exception.
All in all, you can rewrite your code in a way like:
def builder(name, param1, param2) {
return build(job: name, parameters: [string(name: 'Param1', value: param1)], [string(name: 'Param2', value: param2)], quietPeriod: 2, wait: false)
}
node {
stage('Tests') {
def testBuilds = [:]
testBuilds['test1'] = { builder('Test', 'Foo', 'Bar') }
testBuilds['test2'] = { builder('Test', 'Foo2', 'Bar2') }
parallel testBuilds
}
}
I am developing a DSL with Groovy and I have run into the following problem. I have a method which performs some action on an object with the given parameters.
def run(x) {
[with:{ y -> foo(x,y) }]
}
run "thing" with "param" // evaluates to foo("thing","param")
Now, assume I want to add a default functionality to my DSL:
def runDefault(x) {
foo(x)
}
runDefault "thing" // evaluates to foo("thing")
Is there a way to combine the two into a single function, such that the with "param" part becomes an optional clause? I want to be able to use the DSL as shown below:
run "thing" with "param" // should do foo("thing","param")
run "thing" // should do foo("thing")
if you are able to distinguish both calls in the run-method, you could do something like this:
def run(x) {
switch (x) {
case 'foo':
println "foo($x)"; break;
case 'bar':
[with:{ y -> println "bar($x,$y)" }]; break;
}
}
run "bar" with "param"
run "foo"
I would like to return from a closure, like one would if using a break statement in a loop.
For example:
largeListOfElements.each{ element->
if(element == specificElement){
// do some work
return // but this will only leave this iteration and start the next
}
}
In the above if statement I would like to stop iterating through the list and leave the closure to avoid unnecessary iterations.
I've seen a solution where an exception is thrown within the closure and caught outside, but I'm not too fond of that solution.
Are there any solutions to this, other than changing the code to avoid this kind of algorithm?
I think you want to use find instead of each (at least for the specified example). Closures don't directly support break.
Under the covers, groovy doesn't actually use a closure either for find, it uses a for loop.
Alternatively, you could write your own enhanced version of find/each iterator that takes a conditional test closure, and another closure to call if a match is found, having it break if a match is met.
Here's an example:
Object.metaClass.eachBreak = { ifClosure, workClosure ->
for (Iterator iter = delegate.iterator(); iter.hasNext();) {
def value = iter.next()
if (ifClosure.call(value)) {
workClosure.call(value)
break
}
}
}
def a = ["foo", "bar", "baz", "qux"]
a.eachBreak( { it.startsWith("b") } ) {
println "working on $it"
}
// prints "working on bar"
I think you're working on the wrong level of abstraction. The .each block does exactly what it says: it executes the closure once for each element. What you probably want instead is to use List.indexOf to find the right specificElement, and then do the work you need to do on it.
If you want to process all elements until a specific one was found you could also do something like this:
largeListOfElements.find { element ->
// do some work
element == specificElement
}
Although you can use this with any kind of "break condition".
I just used this to process the first n elements of a collection by returning
counter++ >= n
at the end of the closure.
As I understand groovy, the way to shortcut these kinds of loops would be to throw a user-defined exception. I don't know what the syntax would be (not a grrovy programmer), but groovy runs on the JVM so it would be something something like:
class ThisOne extends Exception {Object foo; ThisOne(Object foo) {this.foo=foo;}}
try { x.each{ if(it.isOk()) throw new ThisOne(it); false} }
catch(ThisOne x) { print x.foo + " is ok"; }
After paulmurray's answer I wasn't sure myself what would happen with an Exception thrown from within a closure, so I whipped up a JUnit Test Case that is easy to think about:
class TestCaseForThrowingExceptionFromInsideClosure {
#Test
void testEearlyReturnViaException() {
try {
[ 'a', 'b', 'c', 'd' ].each {
System.out.println(it)
if (it == 'c') {
throw new Exception("Found c")
}
}
}
catch (Exception exe) {
System.out.println(exe.message)
}
}
}
The output of the above is:
a
b
c
Found c
But remember that "one should NOT use Exceptions for flow control", see in particular this Stack Overflow question: Why not use exceptions as regular flow of control?
So the above solution is less than ideal in any case. Just use:
class TestCaseForThrowingExceptionFromInsideClosure {
#Test
void testEarlyReturnViaFind() {
def curSolution
[ 'a', 'b', 'c', 'd' ].find {
System.out.println(it)
curSolution = it
return (it == 'c') // if true is returned, find() stops
}
System.out.println("Found ${curSolution}")
}
}
The output of the above is also:
a
b
c
Found c
Today I faced a similar problem while working with each closure. I wanted to break the flow of execution based on my condition but couldn't do it.
The easiest way to do in groovy is to use any() on a list instead of each if you wish to return a boolean based on some condition.
Good ole for loop still works in Groovy for your use case
for (element in largeListOfElements) {
if(element == specificElement){
// do some work
return
}
}