I have the following controller action in grails to determine if an item is in my shopping cart
def itemInCart(){
def cartHasItem = false
def product = Product.findOrCreateByPartNumberAndColor(params.partNumber, params.color)
def cart = shoppingCartItems()
cart.each() { item ->
if (item['partInfo']['partNumber'] == params.partNumber && item['partInfo']['color']==params.color){
cartHasItem = true
}
}
render(cartHasItem)
}
I added an item to the cart
cart at the time of cart.each() is
[{partInfo={partNumber=69874, color=BLACK}, qty=1, price=16.99}]
params.partNumber = 69874
params.color = BLACK
The issue is that my code is completely skipping over the each() block. debug hits cart.each() { item -> and then goes streight to render
Looks like you want to use any instead of each here:
def cartHasItem = cart.any{ item ->
item['partInfo']['partNumber'] == params.partNumber && item['partInfo']['color']==params.color
}
I wanted to expand on #Dónal's comment on the question, to find out why each() vs each is different.
I've never used object.each() {}, instead I always use this form:
cart.each { item ->
// do work
}
However, I couldn't actually get each() to fail in this simple test:
def myfn() {
def items = [1,2,3]
def ret = false
items.each() {item -> if (item == 2) { ret = true }}
return ret
}
myfn()
==> true
I tried a more complicated version using a map to represent the data you are using, and a simple version just using
[[a:1, b:2, c:3]].each() { println it }
which always renders the item.
What version of groovy are you using?
I'm assuming your data is actually a list of maps here, your syntax for the item was more clojure, than groovy.
Related
I have a table that looks like this:
{
block_0 = {
hash = "98d1a61c4e3d6394b2970a2a5c44ec2caf172ad5c6844b114867b31fa528220e",
index = 0
}
}
Shouldn't I be able to access the index and hash values of block_0 by saying chain["block_0"]["hash"]? It is not working. When I use this line, I get the error attempt to index a nil value (field 'block_0'). How can I properly access hash and index?
EDIT: Here is some more context:
function add_thing()
block_name = "block_0"
block = { }
block[block_name] = { }
block[block_name]["hash"] = ""
block[block_name]["index"] = ""
block[block_name]["hash"] = "this is a test hash"
block[block_name]["index"] = 10
return block
end
chain = { }
table.insert(chain, add_thing())
require 'pl.pretty'.dump(chain)
You are inserting the return value of add_thing into chain. Thus chain is now a table of tables. To index the correct field you have to index chain first, i.e. chain[1]["block_0"]["hash"]. I rather suspect that this is not the intended behaviour and you want to do the following
local function add_thing(chain)
local block_name = "block_0"
chain[block_name] = {
hash = "this is a test hash",
index = 10
}
end
local chain = {}
add_thing(chain)
print(chain["block_0"]["hash"]) -- this is a test hash
Live on Wandbox
This works as expected because tables are reference types.
I have a task where my Jenkins job needs two parameters for build. The first specifies the application name and can be either QA, Dev, Prod etc and the second is a server which is dependent on the first one.
Example: If I chose the app name as QA, the second parameter should display values like QAServer1, QAServer2, QAServer3.
I'm using Active Choices Plugin (https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin) to get this done but facing an problem in fetching the second parameter contents.
Snapshots:
For obtaining the second parameter, I've written a Groovy code which reads the respective files of the selected first parameter and gets the details.
code:
#!/usr/bin/env groovy
import hudson.model.*
def Appliname = System.getenv("APPNAME")
//println Appliname
def list1 = []
def directoryName = "C:/Users/Dev/Desktop/JSONSTest"
def fileSubStr = Appliname
def filePattern = ~/${fileSubStr}/
def directory = new File(directoryName)
def findFilenameClosure =
{
if (filePattern.matcher(it.name).find())
{
def jsoname = it.name
def jsoname1 = jsoname.reverse().take(9).reverse()
list1.add(jsoname1.substring(1,4))
String listAsString = "[\'${list1.join("', '")}\']"
println "return"+listAsString
}
}
directory.eachFileRecurse(findFilenameClosure)
The above code will print the output as return['QAServer1', 'QAServer2'] which i want to use it as input for the second parameter.
Snapshot of Second parameter:
Somehow the Groovy script is not being executed and second parameter value remains empty. How can i get this done dynamically. Am i following the right away to it. Kindly help me figure out. TIA
Would you like to try below change
From:
def findFilenameClosure =
{
if (filePattern.matcher(it.name).find())
{
def jsoname = it.name
def jsoname1 = jsoname.reverse().take(9).reverse()
list1.add(jsoname1.substring(1,4))
String listAsString = "[\'${list1.join("', '")}\']"
println "return"+listAsString
}
}
directory.eachFileRecurse(findFilenameClosure)
To:
directory.eachFileRecurse {
if (filePattern.matcher(it.name).find()) {
def jsoname = it.name
def jsoname1 = jsoname.reverse().take(9).reverse()
list1.add(jsoname1.substring(1,4))
}
}
return list1
I have several g:selects in my gsp that I want to use for filtering a query. How can I access their values in the controller? Here is a select that I have:
<td><g:select name="accts" from="${accountSelection}"/></td>
Here is how I populate the list
def filter() {
def allaccts = getAccountSelection()
[accountInstanceList:allaccts.accountInstanceList]
}
def getAccountSelection() {
params.max = 10
def accts = []
accts.add("all")
Account.list().each { item -> accts.add(item) }
[accountInstanceList: accts, accountInstanceTotal: Account.count()]
}
In your controller it's just another parameter. You can access it like this:
params?.accts?.each {
println it
}
I need to perform a null or empty check on a collection; I think that !members?.empty is incorrect. Is there a groovier way to write the following?
if (members && !members.empty) {
// Some Work
}
There is indeed a Groovier Way.
if (members) {
//Some work
}
does everything if members is a collection. Null check as well as empty check (Empty collections are coerced to false). Hail Groovy Truth. :)
FYI this kind of code works (you can find it ugly, it is your right :) ) :
def list = null
list.each { println it }
soSomething()
In other words, this code has null/empty checks both useless:
if (members && !members.empty) {
members.each { doAnotherThing it }
}
def doAnotherThing(def member) {
// Some work
}
!members.find()
I think now the best way to solve this issue is code above. It works since Groovy 1.8.1 http://docs.groovy-lang.org/docs/next/html/groovy-jdk/java/util/Collection.html#find(). Examples:
def lst1 = []
assert !lst1.find()
def lst2 = [null]
assert !lst2.find()
def lst3 = [null,2,null]
assert lst3.find()
def lst4 = [null,null,null]
assert !lst4.find()
def lst5 = [null, 0, 0.0, false, '', [], 42, 43]
assert lst5.find() == 42
def lst6 = null;
assert !lst6.find()
I'm working on a small event calendar and i want to sort the events by start time!
I'm using JodaTime Plugin in grails for the startTime attribute. ( http://www.grails.org/JodaTime+Plugin )
So, how can i sort with this datatype? This does not work:
def sortedEvents = events.asList().sort({ a, b -> a.startTime <=> b.startTime } as Comparator)
I hope you can help me!
Thanks,
whitenexx
/EDIT/
This is the code where i'm getting the events:
def getEventsNext(Location location) {
def events = location.events.findAll { it.endTime >= new DateTime() }
def sortedEvents = events.sort{it.startTime}
System.out.println(sortedEvents); //test
return sortedEvents
}
In /event/list action everything works fine with g:sortableColumn (sorting by startTime):
Try this:
def sortedEvents = events.asList().sort{it.startTime}
To reverse the sorting order, use:
def sortedEvents = events.asList().sort{-it.startTime}
FYI, Groovy adds this sort() method to Collection so you can remove asList() from the code above if events is already a Collection.
Try overriding the compareTo method in your domain classes.
For example,
int compareTo(obj) {
startTime.compareTo(obj.startTime)
}
Edit
Sort your events like so:
def sortedEvents = events.sort{e1,e2-> e1.startTime.compareTo(2.startTime)}
Or as suggested by #Don, the groovier equivalent
def sortedEvents = events.sort{e1,e2-> e1.startTime <=> e2.startTime}
Try
def events = location.events.findAll { it.endTime.isAfterNow() }
def sortedEvents = events.sort{it.startTime.toDate()}
JavaDoc for isAfterNow()