If statements in Groovy Closures

I am trying to write a mini recursive closure to solve parent child iteration. At some point, I need to check if other children exist before I recursively call this closure. I check with IF, but for some reason returns if always.

Code Update with Objects Included

Have a Json object that has a Parent Children structure (jsonArray is indicated in the code below)

   [
    {
        "id": "1",
        "name": "No Children" //And yet the if condition is true -> element.children
    },
    {
        "id": "2",
        "name": "Does not like Children either"
    },
    {
        "id": "123",
        "name": "Some Parent Element",
        "children": [
            {
                "id": "123123",
                "name": "NameWhatever"
            },
            {
                "id": "123123123",
                "name": "Element with Additional Children",
                "children": [
                    {
                        "id": "123123123",
                        "name": "WhateverChildName"
                    },
                    {
                        "id": "12112",
                        "name": "NameToMap"
                    }
                ]
            }
        ]
    }
]

There is also an ArrayList object that has no identifiers and needs to extract them from the iteration in jsonArray. This is called elementToGetID, which I simplify:

["count": 2741,"value": "NameToMap" ], ["count": 133,"value": "OtherName" ]

Thetwo for loops were written some time ago, and I'm trying to write a recursive closure to go deeper into children

for(int i=0; i<elementsToGetID.size(); i++){
    for(int j=0; j<jsonArray.size(); j++){

        { element ->
            if(element.name == elementsToGetID[i].value){
                elementsToGetID[i]["id"] = element.id
            }
            if (element.children) {
                element.children.each {inst ->
                    log.info "Element to continue function " +inst
                    call(inst) //This call fails
                }
            }
        }(jsonArray[j])

    }
}
+3
source share
2

. .:)


, .

def json = '''
[
    {
        "id": "1",
        "name": "No Children"
    },
    {
        "id": "2",
        "name": "Does not like Children either"
    },
    {
        "id": "123",
        "name": "Some Parent Element",
        "children": [
            {
                "id": "123123",
                "name": "NameWhatever"
            },
            {
                "id": "123123123",
                "name": "Element with Additional Children",
                "children": [
                    {
                        "id": "123123123",
                        "name": "WhateverChildName"
                    },
                    {
                        "id": "12112",
                        "name": "NameToMap"
                    }
                ]
            }
        ]
    }
]
'''

def jsonArray = new groovy.json.JsonSlurper().parseText(json)
def elementsToGetID = [["count": 2741,"value": "NameToMap" ], 
                       ["count": 133,"value": "OtherName" ]]

//can be defined here as well
//def closure

for(int i=0; i < elementsToGetID.size(); i++){
    for(int j=0; j < jsonArray.size(); j++){
        def closure //defined locally

        closure = { element ->
            if(element.name == elementsToGetID[i].value){
                elementsToGetID[i]["id"] = element.id
            }
            if (element.children) {
                element.children.each {inst ->
                    closure(inst) //This call does not fail anymore
                }
            }
        }

        closure(jsonArray[j])
    }
}

assert elementsToGetID == [[count:2741, value:'NameToMap', id:'12112'], 
                           [count:133, value:'OtherName']]
+2

for dmahapatro:

for(int i=0; i < elementsToGetID.size(); i++){
    for(int j=0; j < jsonArray.size(); j++){
        def closure //defined locally

        closure = { element ->
            if(element.name == elementsToGetID[i].value){
                elementsToGetID[i]["id"] = element.id
            }
            if (element.children) {
                element.children.each {inst ->
                    closure(inst) //This call does not fail anymore
                }
            }
        }

        closure(jsonArray[j])
    }
}

, :

jsonArray.each { a ->
    elementsToGetID.find { a.name == it.value }?.id = a.id
    if( a.children ) {
        { -> a.children.each( owner ) }()
    }
}
+2

All Articles