I've been struggling to figure out how do you flat...
# java
c
I've been struggling to figure out how do you flatten nested outputs. For example, I want to call apply on some String values and a different type. It appears that the Java SDK only allows a single type on the parameters passed to
Output.all
so I have to nest multiple calls to apply or
Output.all
and then my results ends up something like
Output<Output<Output<String>>>
. I there a way to flatten this to
Output<String>
? Also, is there any way to block and get the value of an Output? I'm using some cross stack references and it would simplify things if I could block to get the actual values of the outputs from the stack reference.
e
Sure, you can do
o.apply(x => x)
to flatten
c
I saw that recommendation somewhere but when I tried that I seemed to still get back a nested output. I'm using the experimental Kotlin support but wasn't sure if that'd make a difference.
e
Each apply removes one level of nesting
Perhaps if you have Output<Output<Output<String>>> you need x.apply(x => x).apply(x => x) to fully collapse.
If you like it's possible to contribute to the Java SDK in pulumi/pulumi-java , could add a helper method such as
.flatten()
that does the same
c
Thanks for your help! I was definitely doing something wrong as it's working as you suggested. I wonder how you could automatically flatten it multiple levels. Maybe multiple method with a different signature?
Copy code
fun <T> flatten(output: Output<Output<T>>): Output<T> {
    return output.apply { it }
}

fun <T> flatten(output: Output<Output<Output<T>>>): Output<T> {
    return output.apply { it }.apply { it }
}

fun <T> flatten(output: Output<Output<Output<Output<T>>>>): Output<T> {
    return output.apply { it }.apply { it }.apply { it }
}

// etc
e
Might be worth a try, I don't code Java daily but I recall it had some issues at the intersection of generics and overloads, so this might need to be flatten1/flatten2/flatten3