https://pulumi.com logo
#java
Title
# java
c

careful-nail-88325

03/22/2024, 7:13 PM
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

enough-garden-22763

03/22/2024, 8:06 PM
Sure, you can do
o.apply(x => x)
to flatten
c

careful-nail-88325

03/22/2024, 8:15 PM
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

enough-garden-22763

03/22/2024, 8:23 PM
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

careful-nail-88325

03/23/2024, 2:45 AM
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

enough-garden-22763

03/25/2024, 4:24 PM
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