Skip to content

Commit

Permalink
Update generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Nov 14, 2022
1 parent ea5c64c commit 4616eb7
Show file tree
Hide file tree
Showing 5 changed files with 538 additions and 404 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: sdk/go
description: Allow sane conversions for `As*Map*` and `As*Array*` conversions.
4 changes: 1 addition & 3 deletions sdk/go/pulumi/generate/main.go
Expand Up @@ -213,6 +213,7 @@ func makeBuiltins(primitives []*builtin) []*builtin {
// Augment primitives with array and map types.
var builtins []*builtin
for _, p := range primitives {
p.Strategy = "primitive"
name := ""
if p.Name != "Input" {
builtins = append(builtins, p)
Expand Down Expand Up @@ -242,9 +243,6 @@ func makeBuiltins(primitives []*builtin) []*builtin {

InnerElementType: p.Type,
}
if p.Type != "interface{}" {
arrType.Strategy = "array-contravariance"
}
builtins = append(builtins, arrType)
mapType := &builtin{
Name: name + "Map",
Expand Down
37 changes: 13 additions & 24 deletions sdk/go/pulumi/generate/templates/types_builtins.go.template
Expand Up @@ -19,7 +19,6 @@ package pulumi

import (
"context"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -207,36 +206,26 @@ func getResolvedValue(input Input) (reflect.Value, bool) {
}

{{range .Builtins}}
{{ if eq .Strategy "array-contravariance" }}
{{ if eq .Strategy "primitive" }}
// As{{.Name}}Output asserts that the type of the AnyOutput's underlying interface{} value is
// {{.ElementType}} or []interface{} and returns a `{{.Name}}Output` with that value.
// As{{.Name}}Output panics if the value was not the expected type or a compatible array.
// {{.ElementType}} and returns a `{{.Name}}Output` with that value. As{{.Name}}Output panics if the value
// was not the expected type.
func (a AnyOutput) As{{.Name}}Output() {{.Name}}Output {
return a.ApplyT(func(i interface{}) ({{.ElementType}}, error) {
if array, ok := i.([]interface{}); ok {
if len(array) == 0 {
return nil, nil
}
out := make([]{{.InnerElementType}}, len(array))
for i, v := range array {
value, ok := v.({{.InnerElementType}})
if !ok {
return nil, fmt.Errorf("[%d]: expected value of type {{.InnerElementType}}, got %T", i, v)
}
out[i] = value
}
return out, nil
}
return i.({{.ElementType}}), nil
return a.ApplyT(func(i interface{}) {{.ElementType}} {
return i.({{.ElementType}})
}).({{.Name}}Output)
}
{{else}}
// As{{.Name}}Output asserts that the type of the AnyOutput's underlying interface{} value is
// {{.ElementType}} and returns a `{{.Name}}Output` with that value. As{{.Name}}Output panics if the value
// was not the expected type.
// {{.ElementType}} or a compatible type and returns a `{{.Name}}Output` with that value.
// As{{.Name}}Output panics if the value was not the expected type or a compatible type.
func (a AnyOutput) As{{.Name}}Output() {{.Name}}Output {
return a.ApplyT(func(i interface{}) {{.ElementType}} {
return i.({{.ElementType}})
return a.ApplyT(func(i interface{}) ({{.ElementType}}, error) {
v, err := coerceTypeConversion(i, reflect.TypeOf((*{{.ElementType}})(nil)).Elem())
if err != nil {
return nil, err
}
return v.({{.ElementType}}), nil
}).({{.Name}}Output)
}
{{end}}
Expand Down

0 comments on commit 4616eb7

Please sign in to comment.