One more groovy -> kts question. before, with g...
# gradle
r
One more groovy -> kts question. before, with groovy, for the Copy task's
expand
function, we passed in a map where a key contained a function value:
Copy code
[ pick: { Map<String, Object> stackMap, Object fallback -> stackMap.getOrDefault(stack, fallback) } ]
The template file we copied had stuff like:
Copy code
replicas: ${pick(1, 'staging': 2, 'prod': 9)}
Now that I've converted the groovy code (in the build file, not the template) to Kotlin script and it now looks like
Copy code
val pick = { stackMap: Map<String, Any>, fallback: Any -> stackMap.getOrDefault(stack, fallback) }
and I pass in to `expand`:
Copy code
expand(mapOf("pick" to pick))
expansion fails saying this:
Copy code
groovy.lang.MissingMethodException: No signature of method: Build_gradle$pick$1.call() is applicable for argument types: (LinkedHashMap, Integer) values: [[staging:2, prod:9], 1]
If possible, what's the right way to pass a function in
expand
properties?
teammate figured this one out:
Copy code
"pick" to object : groovy.lang.Closure<Any>(null) {
	override fun call(vararg args: Any?): Any? {
		val stackMap: Map<String, Any?> = args[0] as? Map<String, Any?> ?: emptyMap()
		val fallback = args[1]
		return stackMap.getOrDefault(stack, fallback)
	}
}