Groovy web console

subscribe to the feed Subscribe
to this
site

Merge 2 maps, sort versions, find latest version

Published 5 months ago by Brijesh Lochan with tags map sort latestversion
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def sortVersions(versions) {
    def sortedVersions = versions.collectEntries {
        [it, it.version.split(/\./).collect { (it =~ /([0-9]+).*/)[0][1] }*.toInteger()]
    }.sort { a, b ->
        [a.value, b.value].transpose().findResult { x, y -> x <=> y ?: null } ?:
            a.value.size() <=> b.value.size() ?:
            a.key <=> b.key
    }.keySet()

    return sortedVersions
}

def mostRecentVersion(versions){
    sortVersions(versions)[-1]
}

Map merge(Map onto, Map... overrides) {
    println "Onto map: " + onto
    println "Overrides map: " + overrides[0]
    def mergedConfig = onto
    def newConfig = overrides[0]
    
    if (!newConfig)
        return mergedConfig
    else {
        newConfig.each { k1, v1 ->
            def newConfigMap = newConfig[k1]
        
            mergedConfig.each { k2, v2 ->
                if (k1 == k2) {
                    def existingConfigMap = mergedConfig[k2]
                    def existingConfigVersions = existingConfigMap.versions
                    def newConfigVersions = newConfigMap.versions

                    newConfigVersions.eachWithIndex { val, i ->
                
                        def version = newConfigVersions[i].version
                        def commit = newConfigVersions[i].commit
                       
                        def versionExists = false
                        existingConfigVersions.each {
                            if(it.version == version) {
                                println "Version ${it.version} already exists. Skipping writing into merged config..."
                                versionExists = true
                                return false
                            }
                        }                   

                        if (!versionExists) {
                            def newObject = [
                                "version": version,
                                "commit": commit
                            ]

                            println "Version ${version} does not exist. Writing into merged config..."
                            existingConfigVersions = existingConfigVersions << newObject
                            
                        } 
                    }
                }
            }
            // New config which is not present in the existing config, must be added to the existing config as well
            if(!mergedConfig[k1]) {
                println "Key " + k1 + " does not exist in mergedConfig map"
                println "Merged config: " + mergedConfig
                mergedConfig[k1] = newConfigMap
            }

            //Sort versions in merged config
            mergedConfig[k1].versions = sortVersions(mergedConfig[k1].versions)

            // Sets latest version
            mergedConfig[k1].latest = mostRecentVersion(mergedConfig[k1].versions)['version']
        }    
    }
    return mergedConfig
}

// def slurper = new groovy.json.JsonSlurper()

def bla = [
    "CPSecurity/cas-ams-server": [
        "versions": [
            [
                "version": "0.15.1",
                "commit": "1234"
            ],
            [
                "version": "0.15.4",
                "commit": "5678"
            ]
        ],
        "latest": "0.15.2"
    ],
    "CPSecurity/cas-bundle-gateway": [
        "versions": [
            [
                "version": "0.5.1",
                "commit": "3456"
            ]
        ],
        "latest": "0.5.1"
    ],
    "CPSecurity/cas-ams-db": [
        "versions": [
            [
                "version": "22",
                "commit": "6899"
            ]
        ],
        "latest": "22"
    ]
]


def bla1 = [
    "CPSecurity/cas-ams-server": [
        "versions": [
            [
                "version": "0.15.2",
                "commit": "5678"
            ],
            [
                "version": "0.15.3",
                "commit": "7890"
            ]
        ]
    ],
    "CPSecurity/cas-bundle-gateway": [
        "versions": [
            [
                "version": "0.5.2",
                "commit": "0123"
            ]
        ]
    ],
    "CPSecurity/cas-ams-db": [
        "versions": [
            [
                "version": "23",
                "commit": "6789"
            ]
        ]
    ]
]


// def m1 = slurper.parseText(bla)
// def m2 = slurper.parseText(bla1)

def m1 = merge(bla, bla1)
println m1