Groovy
  1. Groovy
  2. GROOVY-4639

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

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major 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.

        Activity

        Peter Ledbrook created issue -
        Jochen Theodorou made changes -
        Field Original Value New Value
        Fix Version/s 1.8-rc-1 [ 17132 ]
        Fix Version/s 1.8-beta-4 [ 17015 ]
        Guillaume Delcroix made changes -
        Fix Version/s 1.8-rc-1 [ 17132 ]
        Fix Version/s 1.9-beta-x [ 17013 ]
        Fix Version/s 1.7.x [ 15538 ]
        Fix Version/s 1.8.x [ 15750 ]
        Jochen Theodorou made changes -
        Fix Version/s 1.7.x [ 15538 ]
        Cédric Champeau made changes -
        Labels contrib
        Jochen Theodorou made changes -
        Fix Version/s 1.8.x [ 15750 ]
        Pascal Schumacher made changes -
        Assignee Pascal Schumacher [ pschumacher ]
        Jochen Theodorou made changes -
        Component/s groovy-runtime [ 16250 ]
        Jochen Theodorou made changes -
        Fix Version/s 2.3.0 [ 19608 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Fix Version/s 2.x [ 17013 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733571 ] Default workflow, editable Closed status [ 12745367 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12972457 ] Default workflow, editable Closed status [ 12980288 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development