Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.6, 1.8-beta-3
Description
A common requirement in Grails plugins and applications is to determine whether a config option is set and if not, to use a default. For example:
def baseServer = grailsApplication.config.base.server ?: "localhost"
where grailsApplication.config is an instance of ConfigObject. This works great - except when it comes to boolean values (or any type with special Groovy Truth behaviour). What if I want to check whether a boolean rabbitmq.myService.transactional setting has a value or not? Using the above code, the left-hand side of the Elvis operator would be assigned both when the option is not set and when it has a value of false.
To distinguish between an explicit value of false and an unset config option, we have to resort to code like:
def rabbitConfig = grailsApplication.config.rabbitmq def transactional = rabbitConfig.myService.transactional if (transactional instanceof ConfigObject) transactional = true
That's neither concise nor elegant, and checking the type of the return value is too dependent on the internal implementation of ConfigObject.
I suggest that we add an extra method (perhaps called isSet()) that allows code to readily determine whether a config options has a value or not:
def rabbitConfig = grailsApplication.config.rabbitmq def transactional = rabbitConfig.myService.isSet("transactional") ? rabbitConfig.myService.transactional : false
Perhaps we should also support rabbitConfig.isSet("myService.transactional")? In some ways I think that adds complexity with little benefit, but it may be useful to people.