Groovy web console

subscribe to the feed Subscribe
to this
site

Override Map

Published 2 months ago by David Bellizzi
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def g = [
  "cloud": [
    "projects": [
      "k1": "v1",
      "k2": "v2"
     ],
    "providers": [
      "k3": "v3",
      "k4": "v4"
     ]
  ],
  "onprem": [
    "backends": [
      "k5": "v5",
      "k6": "v6"
     ],
    "services": [
      "k7": "v7",
      "k8": "v8"
     ]
  ],
  "credentials": [
    "k9": "v9",
    "k10": "v10"
  ]
]  

def overrides = [:]
def s = "k3=foo,k9=bar"
for (o in s.split(",")) {
  def (k, v) = o.split('=')
  overrides.put(k,v)
}

println(g)
for (k in ['cloud', 'onprem', 'credentials']) {
  for (k1 in g[k].keySet()) {
    if (k == 'credentials') {
      if (overrides.containsKey(k1)) {
        g[k][k1] = overrides[k1]
      }
    } else {
      for (k2 in g[k][k1].keySet()){
        if (overrides.containsKey(k2)) {
          g[k][k1][k2] = overrides[k2]
        }
      }  
    }
  }
}  
println(g)