TransWikia.com

Why does Groovy change type of my collection?

Stack Overflow Asked on November 30, 2020

I use groovy 2.5.8. I’m stuck on a problem:

  1. I create def param = [:], and I check in my logs that it was created only like a map (line 219)
  2. I send this def to a method, where the map must become filled. I also check, what type of def will be sent as a response of the method (line 176)
  3. I check type of the def param again, and it become ArrayList…

enter image description here

Have somebody stuck on the problem? How to fix it?

Here is parts of my code:

def checkActivePurchase(RenewIviResponse response) {
    response.result.purchases.each {
        def finishTime = ZonedDateTime.parse(it.finish_time)
        def timeWithinMonth = ZonedDateTime.now().plusDays(30)
            if (finishTime.isBefore(timeWithinMonth)) {
                def params = optionPurchase(response)
                log.info("Line 176, params were requested, sent params   class is " + params.getClass())
                return params
            }
    }
}
    
def option(def response) {
    def params = [:]
    if (response.result) {
        if (response.result.purchases) {
            log.info("Line 219, requesting params, current params class is " + params.getClass())
            params = checkActivePurchase(response)
            log.info("Line 221, got params class is " + params.getClass())
        }
    }
}
    
def optionPurchase(RenewIviResponse response) {
    def params = [:]
    def result = response.result
    def purchase_options = result.purchase_options
    purchase_options.each {
    if (it.product_identifier == iviOptions.product) {
        def paymentOptions = it.payment_options
        paymentOptions.each {
            if (it.purchase_params?._product_identifier == iviOptions.product) {
                def purchase_params = it.purchase_params
                purchase_params.each {
                    if ((it.key as String).startsWith("_") || it.key == 'sign') {
                        params[it.key] = it.value
                            }
                        }
                    }
                }
            }
        }
        params

One Answer

You have several issues with the code.

params = checkActivePurchase(response)

here you re-assigning params to the return value of the method call, which

def checkActivePurchase(RenewIviResponse response) {
    response.result.purchases.each {
      ...
                return params
            }
    }
}

is not returning params as you expect, but actually it returns the result of response.result.purchases.each() call which is obviously of class ArrayList.

To fix the problem you should change you code to something like:

def checkActivePurchase(RenewIviResponse response) {
    response.result.purchases.findResult{
        def finishTime = ZonedDateTime.parse(it.finish_time)
        def timeWithinMonth = ZonedDateTime.now().plusDays(30)
        if (finishTime.isBefore(timeWithinMonth)) {
           def params = optionPurchase(response)
           log.info("Line 176, params were requested, sent params   class is " + params.getClass())
           params
        }else
           null
    }
}

Correct answer by injecteer on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP