prehistoric-kite-30979
12/21/2020, 12:21 PMpulumi up
func Template(tmpl string, data interface{}) pulumi.StringOutput {
any := pulumi.Any(data)
return pulumi.All(tmpl, any).ApplyString(func(v interface{}) (string, error) {
args := v.([]interface{})
toParse := args[0].(string)
fields := args[1].(interface{})
Debug(fields, "test")
var buf bytes.Buffer
parsed, err := template.New("tmpl").Parse(toParse)
if err != nil {
return "", err
}
if err := parsed.Execute(&buf, fields); err != nil {
return "", err
}
return buf.String(), nil
})
}
func TestTemplate(t *testing.T) {
tests := []struct {
name string
tmpl string
data interface{}
want string
}{
{
name: "Shallow object",
tmpl: "Hello, {{ .Field }}!",
data: struct{ Field pulumi.StringInput }{Field: pulumi.String("world")},
want: "Hello, world!",
},
}
for _, tt := range tests {
var wg sync.WaitGroup
t.Run(tt.name, func(t *testing.T) {
got := Template(tt.tmpl, tt.data)
wg.Add(1)
pulumi.All(got, tt.want).ApplyString(func(args []interface{}) (string, error) {
a := args[0].(string)
b := args[1].(string)
if diff := cmp.Diff(a, b); diff != "" {
t.Error(diff)
}
wg.Done()
return "", nil
})
wg.Wait()
})
}
}
pulumi up
the fields get populated with the memory addresses.pulumi.String()
based input whereas my pulumi up
loads it in from config.ToStringOutput()
to my test case.