I use Grails to set properties in a domain class if the property is not null. Currently, the code looks something like this:
def product = Product.getById(5);
if (!product.Name) {
product.Name = "Default Product"
}
if (!product.Price) {
product.Price = 5;
}
if (!product.Type) {
product.Type = "Shampoo"
}
What is the best way to implement this code in Groovy? I managed to simplify it:
product.Name = product.Name ?: "Default Product"
product.Price = product.Price ?: 5
product.Type = product.Type = "Shampoo"
But I would like to do something like this (invalid code):
product {
Name = product.Name ?: "Default Product",
Price = product.Price ?: 5,
Type = product.Type ?: "Shampoo"
}
What would you guys recommend to me?
source
share