Edit
See @tim's solution below for the "correct" Groovy-esque approach to map recursion. Since Map findRecursive does not yet exist in Groovy, if you find yourself needing this functionality in various parts of your app, just add it to Map metaClass:
Map.metaClass.findRecursive = {String key->
if(delegate.containsKey(key)) return delegate."$key"
else
for(m in delegate) {
if(m.value in Map) return m.value.findRecursive(key)
}
}
// then anywhere in your app
someMap.findRecursive('foo')
Original
Was hoping something like findResult{it.key=='foo'} would recurse through map elements beyond 1-d deep, but appears not to be the case.
Rolled my own recursive map finder, but am wondering if there's a better way to do this. Maybe there's a built-in function I'm missing, or an even Groovier (concise) way to pull off the below:
Map map = [school:[id:'schoolID', table:'_school',
children:[team:[id:'teamID',table:'_team',
children:[player:[id:'playerID',table:'_roster']]
]]
]]
class Foo {
static finder = {Map map, String key->
if(map.containsKey(key)) return map[key]
else
for(m in map) {
if(m.value in Map) return this.finder(m.value,key)
}
}
}
println Foo.finder(map,'team')
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…