Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update go codegen to include usage hints on Input types #4279

Merged
merged 4 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
## HEAD (unreleased)
- Fix handling of `nil` values in Outputs in Go.
[#4268](https://github.com/pulumi/pulumi/pull/4268)
- Include usage hints for Input types in Go SDK
[#4279](https://github.com/pulumi/pulumi/pull/4279)

## 1.14.0 (2020-04-01)
- Fix error related to side-by-side versions of `@pulumi/pulumi`.
Expand Down
20 changes: 20 additions & 0 deletions pkg/codegen/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,33 @@ func printComment(w io.Writer, comment string, indent bool) {
}

func genInputInterface(w io.Writer, name string) {
printComment(w, getInputUsage(name), false)
fmt.Fprintf(w, "type %sInput interface {\n", name)
fmt.Fprintf(w, "\tpulumi.Input\n\n")
fmt.Fprintf(w, "\tTo%sOutput() %sOutput\n", title(name), name)
fmt.Fprintf(w, "\tTo%sOutputWithContext(context.Context) %sOutput\n", title(name), name)
fmt.Fprintf(w, "}\n\n")
}

func getInputUsage(name string) string {
EvanBoyle marked this conversation as resolved.
Show resolved Hide resolved
if strings.HasSuffix(name, "Array") {
baseTypeName := name[:strings.LastIndex(name, "Array")]
return fmt.Sprintf("Construct a concrete instance of %sInput via:\n\t%s{ %sArgs{...} }", name, name, baseTypeName)
}

if strings.HasSuffix(name, "Map") {
baseTypeName := name[:strings.LastIndex(name, "Map")]
return fmt.Sprintf("Construct a concrete instance of %sInput via:\n\t%s{ \"key\": %sArgs{...} }", name, name, baseTypeName)
}

if strings.HasSuffix(name, "Ptr") {
baseTypeName := name[:strings.LastIndex(name, "Ptr")]
return fmt.Sprintf("Construct a concrete instance of %sInput via:\n\t%sArgs{...}.To%sOutput()", name, baseTypeName, name)
EvanBoyle marked this conversation as resolved.
Show resolved Hide resolved
}

return fmt.Sprintf("Construct a concrete instance of %sInput via:\n\t%sArgs{...}", name, name)
}

func genInputMethods(w io.Writer, name, receiverType, elementType string, ptrMethods bool) {
fmt.Fprintf(w, "func (%s) ElementType() reflect.Type {\n", receiverType)
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil)).Elem()\n", elementType)
Expand Down
21 changes: 21 additions & 0 deletions pkg/codegen/go/gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gen

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInputUsage(t *testing.T) {
arrayUsage := getInputUsage("FooArray")
assert.Equal(t, "Construct a concrete instance of FooArrayInput via:\n\tFooArray{ FooArgs{...} }", arrayUsage)

mapUsage := getInputUsage("FooMap")
assert.Equal(t, "Construct a concrete instance of FooMapInput via:\n\tFooMap{ \"key\": FooArgs{...} }", mapUsage)

ptrUsage := getInputUsage("FooPtr")
assert.Equal(t, "Construct a concrete instance of FooPtrInput via:\n\tFooArgs{...}.ToFooPtrOutput()", ptrUsage)

usage := getInputUsage("Foo")
assert.Equal(t, "Construct a concrete instance of FooInput via:\n\tFooArgs{...}", usage)
}
1 change: 1 addition & 0 deletions pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down