Ran Magen

01/26/2020, 5:26 AM
One more groovy -> kts question. before, with groovy, for the Copy task's
function, we passed in a map where a key contained a function value:
[ pick: { Map<String, Object> stackMap, Object fallback -> stackMap.getOrDefault(stack, fallback) } ]
The template file we copied had stuff like:
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
val pick = { stackMap: Map<String, Any>, fallback: Any -> stackMap.getOrDefault(stack, fallback) }
and I pass in to `expand`:
expand(mapOf("pick" to pick))
expansion fails saying this:
groovy.lang.MissingMethodException: No signature of method: Build_gradle$pick$ 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
teammate figured this one out:
"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)