I am playing with grails and groovy. I wondered if its possible to do something like this.
def inbuiltReqAttributes = ['actionName','actionUri','controllerName','controllerUri']
inbuiltReqAttributes.each() { print " ${it} = ? " };
what would i put in the ? to get groovy to evaluate the current iterator value as a variable e.g. to do it the long way
print " actionName = $actionName "
Thanks
I believe off the top of my head, this should work:
print " ${it} = ${this[ it ]}"
Or:
print " ${it} = ${getProperty( it )}"
But i'm not at a computer to 100% verify this atm...
Try this:
inbuiltReqAttributes.each() {
evaluate("value = ${it}")
print "$it = $value"
}
Related
I am newbie to jenkins pipeline scripting and i am just trying to concatenate date to string getting below No Such Property exception. Dont know where am doing wrong. Could some one please help me to resolve this
def generateRandomText(){
def temp = ""
try{
Date date = new Date()
String datePart = date.format("ddHHmmssSSS")
temp = "abcde" + datepart
echo "printing ... $temp"
return temp
}
catch(theError){
echo "Error getting while generating random text: {$theError}"
}
return temp
}
MissingPropertyException means the variable wasn't declared.
There were some errors in your code:
You used echo, which doesn't exist in Groovy. Use one of the print functions instead. On the code below I used println
The datePart variable was mispelled
Here's your code fixed:
def generateRandomText(){
def temp = ""
try{
Date date = new Date()
String datePart = date.format("ddHHmmssSSS")
temp = "abcde" + datePart
println "printing ... $temp"
return temp
}
catch(theError){
println "Error getting while generating random text: {$theError}"
}
return temp
}
generateRandomText()
Output on groovyConsole:
printing ... abcde21195603124
Result: abcde21195603124
See Groovy's documentation.
I'm new in velocity and need help.
I have com.atlassian.jira.user.RemoteUser and remoteUser.getDisplayName() returns string which I want to split around matches of the given regular expression. And then get first element of String array, but unfortunately I have:
#set ($userName = $remoteUser.getDisplayName().split(" "))
$userName, Hello!
And have [Ljava.lang.String;#74a70acb as I can understand it is String array.
But if I try try get
#set ($userName = $remoteUser.getDisplayName().split(" ")[0])
$userName, Hello!
I have
org.apache.velocity.exception.ParseErrorException: Encountered "[" at getEncodedBodyFromContent[line 1, column 58] Was expecting one of:
<RPAREN> ...
<WHITESPACE> ...
"-" ...
"+" ...
"*" ...
"/" ...
"%" ...
<LOGICAL_AND> ...
<LOGICAL_OR> ...
<LOGICAL_LT> ...
<LOGICAL_LE> ...
<LOGICAL_GT> ...
<LOGICAL_GE> ...
<LOGICAL_EQUALS> ...
<LOGICAL_NOT_EQUALS> ...
<DOT> ...
If I try #set ($userName = $remoteUser.getDisplayName().split(" ")).get(0)) then I get null pointer exception
Velocity 1.6.4 is more than ten years old and doesn't support arrays.
Velocity 1.7 was released in November 2010 and does supports arrays.
Someone, somewhere, should upgrade something (FYI current version is Velocity 2.2 ...).
I don't know why but $string.split(" ") command doesn't work. After that I thought I had String array, but after $userName = $remoteUser.getDisplayName().split(" ").get($index) I had null pointer exception.
Solution which helped was using indexOf() and substring() commands like this:
#set ($userName = $remoteUser.getDisplayName())
#set ($index = $userName.indexOf(' '))
#set ($surname = $userName.substring(0, $index))
#set ($index = $index + 1)
#set ($name = $userName.substring($index))
I want to select all the choices in my Groovy script so it defaults to all. I am using the Active Choices Reactive Parameter because I am reading in the previous option. How do I make my "output" variable so it is all selected without having the user select them all?
def output = []
def line
def release = RELEASE_NUMBER
releaseNumber = release.replaceAll("\\n", "");
String[] number = releaseNumber.split("\\.");
def list = "cmd /c e:\\tools\\wget --no-check-certificate --http-user=username--http-password=password-qO- \"https://1.1.1.1:443/svn/Instructions/trunk/${number[0]}.${number[1]}.${number[2]}/ICAN/EI_${releaseNumber}.txt\"".execute().text
list.eachLine {
if (it.contains("- LH")) {
String newName = it.replaceAll("\\s", "");
String newName2 = newName.replaceAll("-", "");
output.add(newName2)
}
}
return output
I don't know anything about Jenkins, but reading the documentation for the plugin you mention you should be able to simply use output.add("${newName2}:selected").
I'm adding some HTML content to an UIWebView.
This line:
generatedHtml += "<br><p style=\"font-family:'Chevin-Medium';font-size:12px;color:#505050;padding-top:0px;\">" + newsItem.entry.likes + " like this " + newsItem.entry.comments?.count + " comments</p>"
I get:
expressions was too complex to be solved in reasonable time
I'm just doing a count on an array, i don't know how to make that less complex?
The object looks like this:
public class NewsItem: NSObject {
var entry: EntryObject = EntryObject()
}
public class EntryObject: NSObject {
var comments: [Comment]? = []
}
newsItem.entry.comments?.count is an integer, and you can't add an integer to a string using +, you should use string interpolation with \():
" like this \(newsItem.entry.comments?.count) comments</p>"
Or use the String initializer if you need to keep using +:
" like this " + String(newsItem.entry.comments?.count) + " comments</p>"
If the error "too complex" persists, you'll have to break down the statements and use variables instead of inserting the expressions directly.
Try to do by this way
var countComments : Int = 0
//Validate comment counting
if let cComments = newsItem.entry.comments?.count
{
countComments = cComments
}
//... Some code here ...
//Devide to Conquest.
//If is easy to find... Is not hard to fix
generatedHtml += "<br>"
generatedHtml += "<p style=\"font-family:'Chevin-Medium';font-size:12px;color:#505050;padding-top:0px;\">"
generatedHtml += "\(newsItem.entry.likes) "
generatedHtml += "like this \(countComments) comments" //Here you have a valid value
genetatedHtml += "</p>"
But, why?
Maybe you have a problem with the optional value newsItem.entry.comments?.count that can gets you a nil value. Then, first of all, validate the value and be sure about what was returned. Better "0", a valid value than nil
When you split the string creation, the debug working will be more easy to execute. You will can have a better idea where is happening an error.
Maybe it´s not a definitive solution to your problem, but a good way to help you fix it.
I want to replace a word into my body content from other string .
To implement this i am using ngx.re.sgub but i am getting a weird issue. ngx.re.gsub is not handling magic characters.
Example :
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, body)
ngx.print(content2)
output is
Hi.00lHelo .00 how are you .00all he.00llo .00 how are you .00all
while output should like :
Hi$100.00Hello .00 how are you .00all.
Please let me know how can i achieve this .
In ngx regex, $1, $2, etc. are variable to be captured. Try escape the $ character:
body = "$$100.00"
Wrap the body with a function also avoids it:
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, function()
return body
end, "o")
ngx.print(content2)