Created
April 12, 2022 07:10
-
-
Save dracorp/c365dc1ff6320250ab2d40b1e738e7b9 to your computer and use it in GitHub Desktop.
Compare Map arrays in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.util.Eval | |
def compareArrays(def first, def path = '', def second) { | |
if (first instanceof java.util.LinkedHashMap) { | |
first.each { index, value -> | |
if ( index.indexOf('-') > 0 ) { | |
index = index.inspect() | |
} | |
if ( value instanceof java.util.LinkedHashMap ) { | |
def newPath = path ? path + "." + index : index | |
compareArrays(value, newPath, second) | |
} else { | |
def newPath = path ? path + "." + index : index | |
def secondValue = Eval.me("array", second, "array.$newPath") | |
if (secondValue == null ) { | |
println "ERROR: Missing path $newPath in second array!" | |
} else if ( value != secondValue ) { | |
println "ERROR: Different value for path: $newPath, '$value != '$secondValue" | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compareArrays(def first, def second) { | |
first = first as ConfigObject | |
second = second as ConfigObject | |
def firstProps = first.toProperties() | |
def secondProps = second.toProperties() | |
firstProps.each { index, value -> | |
def secondValue = secondProps[index] | |
if (secondValue == null ) { | |
println "ERROR: Missing path $index in second array!" | |
} else if ( value != secondValue ) { | |
println "ERROR: Different value for path: $index, '$value != '$secondValue" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sample of array | |
def First = [ | |
"PROD": [ | |
"service1: [ | |
"key1": "value1", | |
"key2": "value2" | |
], | |
"service2: [ | |
"key1": "value1", | |
"key2": "value2" | |
] | |
], | |
"DEV": [ | |
"service1: [ | |
"key1": "value1", | |
"key2": "value2" | |
], | |
"service2: [ | |
"key1": "value1", | |
"key2": "value2" | |
] | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment