Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4639

Add isSet()/hasValue()/similar method to ConfigObject

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.6, 1.8-beta-3
    • Fix Version/s: 2.3.0-beta-1
    • Component/s: groovy-runtime
    • Labels:

      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.

        Attachments

          Activity

            People

            • Assignee:
              pschumacher Pascal Schumacher
              Reporter:
              pledbrook Peter Ledbrook
            • Votes:
              5 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: