Skip to content

Instantly share code, notes, and snippets.

@dracorp
Created April 12, 2022 07:10
Show Gist options
  • Save dracorp/c365dc1ff6320250ab2d40b1e738e7b9 to your computer and use it in GitHub Desktop.
Save dracorp/c365dc1ff6320250ab2d40b1e738e7b9 to your computer and use it in GitHub Desktop.
Compare Map arrays in Groovy
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"
}
}
}
}
}
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"
}
}
}
// 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