From 3efbc3705d19887f7cd777a9363684a83cb0e95f Mon Sep 17 00:00:00 2001 From: Evan Boyle Date: Thu, 2 Apr 2020 19:59:08 -0700 Subject: [PATCH] Update go codegen to include usage hints on Input types (#4279) --- CHANGELOG.md | 2 ++ pkg/codegen/go/gen.go | 49 ++++++++++++++++++++++++++++++++++++++ pkg/codegen/go/gen_test.go | 37 ++++++++++++++++++++++++++++ pkg/go.sum | 1 + 4 files changed, 89 insertions(+) create mode 100644 pkg/codegen/go/gen_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a7fceed1d..cf2382a22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) - Fix secretness propagation in Python `apply`. [#4273](https://github.com/pulumi/pulumi/pull/4273) diff --git a/pkg/codegen/go/gen.go b/pkg/codegen/go/gen.go index 353d3967a..70eb869df 100644 --- a/pkg/codegen/go/gen.go +++ b/pkg/codegen/go/gen.go @@ -311,6 +311,7 @@ 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) @@ -318,6 +319,54 @@ func genInputInterface(w io.Writer, name string) { fmt.Fprintf(w, "}\n\n") } +func getInputUsage(name string) string { + if strings.HasSuffix(name, "Array") { + baseTypeName := name[:strings.LastIndex(name, "Array")] + return strings.Join([]string{ + fmt.Sprintf("%sInput is an input type that accepts %s and %sOutput values.", name, name, name), + fmt.Sprintf("You can construct a concrete instance of `%sInput` via:", name), + "", + fmt.Sprintf("\t\t %s{ %sArgs{...} }", name, baseTypeName), + " ", + }, "\n") + + } + + if strings.HasSuffix(name, "Map") { + baseTypeName := name[:strings.LastIndex(name, "Map")] + return strings.Join([]string{ + fmt.Sprintf("%sInput is an input type that accepts %s and %sOutput values.", name, name, name), + fmt.Sprintf("You can construct a concrete instance of `%sInput` via:", name), + "", + fmt.Sprintf("\t\t %s{ \"key\": %sArgs{...} }", name, baseTypeName), + " ", + }, "\n") + } + + if strings.HasSuffix(name, "Ptr") { + baseTypeName := name[:strings.LastIndex(name, "Ptr")] + return strings.Join([]string{ + fmt.Sprintf("%sInput is an input type that accepts %sArgs, %s and %sOutput values.", name, baseTypeName, name, name), + fmt.Sprintf("You can construct a concrete instance of `%sInput` via:", name), + "", + fmt.Sprintf("\t\t %sArgs{...}", baseTypeName), + "", + " or:", + "", + "\t\t nil", + " ", + }, "\n") + } + + return strings.Join([]string{ + fmt.Sprintf("%sInput is an input type that accepts %sArgs and %sOutput values.", name, name, name), + fmt.Sprintf("You can construct a concrete instance of `%sInput` via:", name), + "", + fmt.Sprintf("\t\t %sArgs{...}", name), + " ", + }, "\n") +} + 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) diff --git a/pkg/codegen/go/gen_test.go b/pkg/codegen/go/gen_test.go new file mode 100644 index 000000000..419e85ad2 --- /dev/null +++ b/pkg/codegen/go/gen_test.go @@ -0,0 +1,37 @@ +package gen + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestInputUsage(t *testing.T) { + arrayUsage := getInputUsage("FooArray") + assert.Equal( + t, + "FooArrayInput is an input type that accepts FooArray and FooArrayOutput values.\nYou can construct a "+ + "concrete instance of `FooArrayInput` via:\n\n\t\t FooArray{ FooArgs{...} }\n ", + arrayUsage) + + mapUsage := getInputUsage("FooMap") + assert.Equal( + t, + "FooMapInput is an input type that accepts FooMap and FooMapOutput values.\nYou can construct a concrete"+ + " instance of `FooMapInput` via:\n\n\t\t FooMap{ \"key\": FooArgs{...} }\n ", + mapUsage) + + ptrUsage := getInputUsage("FooPtr") + assert.Equal( + t, + "FooPtrInput is an input type that accepts FooArgs, FooPtr and FooPtrOutput values.\nYou can construct a "+ + "concrete instance of `FooPtrInput` via:\n\n\t\t FooArgs{...}\n\n or:\n\n\t\t nil\n ", + ptrUsage) + + usage := getInputUsage("Foo") + assert.Equal( + t, + "FooInput is an input type that accepts FooArgs and FooOutput values.\nYou can construct a concrete instance"+ + " of `FooInput` via:\n\n\t\t FooArgs{...}\n ", + usage) +} diff --git a/pkg/go.sum b/pkg/go.sum index e88d45dc4..9a669a333 100644 --- a/pkg/go.sum +++ b/pkg/go.sum @@ -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=