groovy
  1. groovy
  2. GROOVY-4639

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

    Details

    • Type: Improvement Improvement
    • Status: Closed 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:
    • Number of attachments :
      0

      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

        Hide
        Marc Palmer added a comment -

        I second this, it is very irritating.

        However, another approach might be adding grails-like coercion methods with optional default value arg:

        def conf = configInstance.my.values
        def txnl = conf.boolean('transactional', true) // coerce strings to booleans at the same time
        def maxthreads = conf.int('max', 10) // coerce strings to ints as same time etc
        
        Show
        Marc Palmer added a comment - I second this, it is very irritating. However, another approach might be adding grails-like coercion methods with optional default value arg: def conf = configInstance.my.values def txnl = conf. boolean ('transactional', true ) // coerce strings to booleans at the same time def maxthreads = conf. int ('max', 10) // coerce strings to ints as same time etc
        Hide
        Pascal Schumacher added a comment - - edited

        I submitted a pull request for this. Please review.

        Show
        Pascal Schumacher added a comment - - edited I submitted a pull request for this. Please review.
        Hide
        blackdrag blackdrag added a comment -

        I applied the pull request

        Show
        blackdrag blackdrag added a comment - I applied the pull request

          People

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

            Dates

            • Created:
              Updated:
              Resolved: