https://pulumi.com logo
#golang
Title
# golang
h

hundreds-article-77945

12/17/2021, 8:26 PM
How do you serialize a
pulumi.StringPtrOutput
with
pulumi.Sprintf
? I’m getting this:
"serviceAccount:<...>.svc.id.goog[%!s(*string=0x140001425d0)/%!s(*string=0x140006169b0)]"
from:
Copy code
pulumi.Sprintf("serviceAccount:%s.svc.id.goog[%v/%v]", config.Require(ctx, "gcp:project"), namespace.Metadata.Name(), ksa.Metadata.Name()),
The first arg is a string, but the latter two are string pointers.
I suppose that’s normal behavior for `fmt.Printf`: https://goplay.space/#ncEmglC2dUI
I suppose I could write an
ApplyT
func that converts it to a string from a string pointer
Just following up, but that’s what I went with:
Copy code
// UnwrapString converts a StringPtrOutput to a StringOutput.
//
// If the string is `nil`, an empty string will be returned.
func UnwrapString(sp pulumi.StringPtrOutput) pulumi.StringOutput {
	return sp.ApplyT(func(v *string) string {
		if v == nil {
			return ""
		}
		return *v
	}).(pulumi.StringOutput)
}
Copy code
pulumi.Sprintf("serviceAccount:%s.svc.id.goog[%v/%v]", config.Require(ctx, "gcp:project"), UnwrapString(namespace.Metadata.Name()), UnwrapString(ksa.Metadata.Name()))
TIL you can just call
.Elem()
!
4 Views