AnswerBun.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!

Related Questions

form validation with data ranges

2  Asked on August 7, 2020 by eniol

   

How to format this output?

2  Asked on August 6, 2020 by shantanu-jain

     

uploading csv file django using a form

2  Asked on August 5, 2020 by sheraram_prajapat

       

Set focus to another Control

3  Asked on August 4, 2020 by binu

 

Animate gradient bar chart – matplotlib

1  Asked on August 4, 2020 by jonboy

       

c# error – Can not convert Array to byte array

1  Asked on August 2, 2020 by rakesh

         

Correcting Business name misspellings

1  Asked on August 2, 2020 by jonathan-rauscher

     

How to cut a string to the first “/” from right to left c# .net

1  Asked on August 1, 2020 by ignacio-gomez

   

Inserting random data from a list

2  Asked on August 1, 2020 by smiley

     

Invalid token SELECT

2  Asked on July 31, 2020 by virendra-varma

     

Django – No column found for custom field?

0  Asked on July 29, 2020 by jare42

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir