Skip to content

Commit

Permalink
Revert "Go and Python codegen support symbols with hyphens in their n…
Browse files Browse the repository at this point in the history
…ames"
  • Loading branch information
iwahbe committed Oct 15, 2022
1 parent 301dafb commit aaf547e
Show file tree
Hide file tree
Showing 44 changed files with 675 additions and 1,279 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdkgen/go,python
description: Revert 10738, fixing python class generation
63 changes: 0 additions & 63 deletions pkg/codegen/cgstrings/cgstrings.go

This file was deleted.

46 changes: 0 additions & 46 deletions pkg/codegen/cgstrings/cgstrings_test.go

This file was deleted.

72 changes: 43 additions & 29 deletions pkg/codegen/go/gen.go
Expand Up @@ -30,9 +30,9 @@ import (
"strconv"
"strings"
"sync"
"unicode"

"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/cgstrings"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
Expand Down Expand Up @@ -93,9 +93,24 @@ func Title(s string) string {
if s[0] == '$' {
return Title(s[1:])
}
s = cgstrings.UppercaseFirst(s)
s = cgstrings.Unhyphenate(s)
return s
runes := []rune(s)
return string(append([]rune{unicode.ToUpper(runes[0])}, runes[1:]...))
}

func camel(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
res := make([]rune, 0, len(runes))
for i, r := range runes {
if unicode.IsLower(r) {
res = append(res, runes[i:]...)
break
}
res = append(res, unicode.ToLower(r))
}
return string(res)
}

func tokenToPackage(pkg *schema.Package, overrides map[string]string, tok string) string {
Expand Down Expand Up @@ -1201,8 +1216,7 @@ func (pkg *pkgContext) genEnumOutputTypes(w io.Writer, name, elementArgsType, el
func (pkg *pkgContext) genEnumInputTypes(w io.Writer, name string, enumType *schema.EnumType, elementGoType string) {
pkg.genInputInterface(w, name)

typeName := cgstrings.Camel(name)
fmt.Fprintf(w, "var %sPtrType = reflect.TypeOf((**%s)(nil)).Elem()\n", typeName, name)
fmt.Fprintf(w, "var %sPtrType = reflect.TypeOf((**%s)(nil)).Elem()\n", camel(name), name)
fmt.Fprintln(w)

fmt.Fprintf(w, "type %sPtrInput interface {\n", name)
Expand All @@ -1212,25 +1226,25 @@ func (pkg *pkgContext) genEnumInputTypes(w io.Writer, name string, enumType *sch
fmt.Fprintf(w, "}\n")
fmt.Fprintln(w)

fmt.Fprintf(w, "type %sPtr %s\n", typeName, elementGoType)
fmt.Fprintf(w, "type %sPtr %s\n", camel(name), elementGoType)
fmt.Fprintln(w)

fmt.Fprintf(w, "func %[1]sPtr(v %[2]s) %[1]sPtrInput {\n", name, elementGoType)
fmt.Fprintf(w, "return (*%sPtr)(&v)\n", typeName)
fmt.Fprintf(w, "return (*%sPtr)(&v)\n", camel(name))
fmt.Fprintf(w, "}\n")
fmt.Fprintln(w)

fmt.Fprintf(w, "func (*%sPtr) ElementType() reflect.Type {\n", typeName)
fmt.Fprintf(w, "return %sPtrType\n", typeName)
fmt.Fprintf(w, "func (*%sPtr) ElementType() reflect.Type {\n", camel(name))
fmt.Fprintf(w, "return %sPtrType\n", camel(name))
fmt.Fprintf(w, "}\n")
fmt.Fprintln(w)

fmt.Fprintf(w, "func (in *%[1]sPtr) To%[2]sPtrOutput() %[2]sPtrOutput {\n", typeName, name)
fmt.Fprintf(w, "func (in *%[1]sPtr) To%[2]sPtrOutput() %[2]sPtrOutput {\n", camel(name), name)
fmt.Fprintf(w, "return pulumi.ToOutput(in).(%sPtrOutput)\n", name)
fmt.Fprintf(w, "}\n")
fmt.Fprintln(w)

fmt.Fprintf(w, "func (in *%[1]sPtr) To%[2]sPtrOutputWithContext(ctx context.Context) %[2]sPtrOutput {\n", cgstrings.Camel(name), name)
fmt.Fprintf(w, "func (in *%[1]sPtr) To%[2]sPtrOutputWithContext(ctx context.Context) %[2]sPtrOutput {\n", camel(name), name)
fmt.Fprintf(w, "return pulumi.ToOutputWithContext(ctx, in).(%sPtrOutput)\n", name)
fmt.Fprintf(w, "}\n")
fmt.Fprintln(w)
Expand Down Expand Up @@ -1297,7 +1311,7 @@ func (pkg *pkgContext) assignProperty(w io.Writer, p *schema.Property, object, v
}
fmt.Fprintf(w, "\t%s.%s = %s\n", object, Title(p.Name), value)
} else if indirectAssign {
tmpName := cgstrings.Camel(p.Name) + "_"
tmpName := camel(p.Name) + "_"
fmt.Fprintf(w, "%s := %s\n", tmpName, value)
fmt.Fprintf(w, "%s.%s = &%s\n", object, Title(p.Name), tmpName)
} else {
Expand Down Expand Up @@ -1403,7 +1417,7 @@ func (pkg *pkgContext) genInputTypes(w io.Writer, t *schema.ObjectType, details
if details.ptrInput {
pkg.genInputInterface(w, name+"Ptr")

ptrTypeName := cgstrings.Camel(name) + "PtrType"
ptrTypeName := camel(name) + "PtrType"

fmt.Fprintf(w, "type %s %sArgs\n\n", ptrTypeName, name)

Expand Down Expand Up @@ -1813,7 +1827,7 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso

// Emit the state types for get methods.
fmt.Fprintf(w, "// Input properties used for looking up and filtering %s resources.\n", name)
fmt.Fprintf(w, "type %sState struct {\n", cgstrings.Camel(name))
fmt.Fprintf(w, "type %sState struct {\n", camel(name))
if r.StateInputs != nil {
for _, p := range r.StateInputs.Properties {
printCommentWithDeprecationMessage(w, p.Comment, p.DeprecationMessage, true)
Expand All @@ -1832,12 +1846,12 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
fmt.Fprintf(w, "}\n\n")

fmt.Fprintf(w, "func (%sState) ElementType() reflect.Type {\n", name)
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%sState)(nil)).Elem()\n", cgstrings.Camel(name))
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%sState)(nil)).Elem()\n", camel(name))
fmt.Fprintf(w, "}\n\n")
}

// Emit the args types.
fmt.Fprintf(w, "type %sArgs struct {\n", cgstrings.Camel(name))
fmt.Fprintf(w, "type %sArgs struct {\n", camel(name))
for _, p := range r.InputProperties {
printCommentWithDeprecationMessage(w, p.Comment, p.DeprecationMessage, true)
fmt.Fprintf(w, "\t%s %s `pulumi:\"%s\"`\n", Title(p.Name), pkg.typeString(codegen.ResolvedType(p.Type)), p.Name)
Expand All @@ -1863,7 +1877,7 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
fmt.Fprintf(w, "}\n\n")

fmt.Fprintf(w, "func (%sArgs) ElementType() reflect.Type {\n", name)
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%sArgs)(nil)).Elem()\n", cgstrings.Camel(name))
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%sArgs)(nil)).Elem()\n", camel(name))
fmt.Fprintf(w, "}\n")

// Emit resource methods.
Expand Down Expand Up @@ -1915,7 +1929,7 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
outputsType := "pulumi.AnyOutput"
if f.Outputs != nil {
if shouldLiftReturn {
outputsType = fmt.Sprintf("%s%sResultOutput", cgstrings.Camel(name), methodName)
outputsType = fmt.Sprintf("%s%sResultOutput", camel(name), methodName)
} else {
outputsType = fmt.Sprintf("%s%sResultOutput", name, methodName)
}
Expand All @@ -1930,7 +1944,7 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
fmt.Fprintf(w, "\t}\n")

// Get the name of the method to return the output
fmt.Fprintf(w, "\treturn %s.(%s).%s(), nil\n", resultVar, cgstrings.Camel(outputsType), Title(f.Outputs.Properties[0].Name))
fmt.Fprintf(w, "\treturn %s.(%s).%s(), nil\n", resultVar, camel(outputsType), Title(f.Outputs.Properties[0].Name))
} else {
// Check the error before proceeding.
fmt.Fprintf(w, "\tif err != nil {\n")
Expand All @@ -1945,7 +1959,7 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
// If there are argument and/or return types, emit them.
if len(args) > 0 {
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "type %s%sArgs struct {\n", cgstrings.Camel(name), methodName)
fmt.Fprintf(w, "type %s%sArgs struct {\n", camel(name), methodName)
for _, p := range args {
printCommentWithDeprecationMessage(w, p.Comment, p.DeprecationMessage, true)
fmt.Fprintf(w, "\t%s %s `pulumi:\"%s\"`\n", Title(p.Name), pkg.typeString(codegen.ResolvedType(p.Type)),
Expand All @@ -1962,15 +1976,15 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
fmt.Fprintf(w, "}\n\n")

fmt.Fprintf(w, "func (%s%sArgs) ElementType() reflect.Type {\n", name, methodName)
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s%sArgs)(nil)).Elem()\n", cgstrings.Camel(name), methodName)
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s%sArgs)(nil)).Elem()\n", camel(name), methodName)
fmt.Fprintf(w, "}\n\n")
}
if f.Outputs != nil {
outputStructName := name

// Don't export the result struct if we're lifting the value
if shouldLiftReturn {
outputStructName = cgstrings.Camel(name)
outputStructName = camel(name)
}

fmt.Fprintf(w, "\n")
Expand Down Expand Up @@ -2638,7 +2652,7 @@ func (pkg *pkgContext) genResourceRegistrations(w io.Writer, r *schema.Resource,
for _, method := range r.Methods {
if method.Function.Outputs != nil {
if pkg.liftSingleValueMethodReturns && len(method.Function.Outputs.Properties) == 1 {
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%s%sResultOutput{})\n", cgstrings.Camel(name), Title(method.Name))
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%s%sResultOutput{})\n", camel(name), Title(method.Name))
} else {
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%s%sResultOutput{})\n", name, Title(method.Name))
}
Expand Down Expand Up @@ -2859,7 +2873,7 @@ func (pkg *pkgContext) genConfig(w io.Writer, variables []*schema.Property) erro
}

printCommentWithDeprecationMessage(w, p.Comment, p.DeprecationMessage, false)
configKey := fmt.Sprintf("\"%s:%s\"", pkg.pkg.Name, cgstrings.Camel(p.Name))
configKey := fmt.Sprintf("\"%s:%s\"", pkg.pkg.Name, camel(p.Name))

fmt.Fprintf(w, "func Get%s(ctx *pulumi.Context) %s {\n", Title(p.Name), getType)
if p.DefaultValue != nil {
Expand Down Expand Up @@ -3216,11 +3230,11 @@ func generatePackageContextMap(tool string, pkg *schema.Package, goInfo GoPackag
names = append(names, rawResourceName(r)+suffix+"Input")
names = append(names, rawResourceName(r)+suffix+"Output")
names = append(names, rawResourceName(r)+suffix+"Args")
names = append(names, cgstrings.Camel(rawResourceName(r))+suffix+"Args")
names = append(names, camel(rawResourceName(r))+suffix+"Args")
names = append(names, "New"+rawResourceName(r)+suffix)
if !r.IsProvider && !r.IsComponent {
names = append(names, rawResourceName(r)+suffix+"State")
names = append(names, cgstrings.Camel(rawResourceName(r))+suffix+"State")
names = append(names, camel(rawResourceName(r))+suffix+"State")
names = append(names, "Get"+rawResourceName(r)+suffix)
}
return names
Expand Down Expand Up @@ -3630,7 +3644,7 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
return nil, err
}

setFile(path.Join(mod, cgstrings.Camel(rawResourceName(r))+".go"), buffer.String())
setFile(path.Join(mod, camel(rawResourceName(r))+".go"), buffer.String())
}

// Functions
Expand All @@ -3640,7 +3654,7 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
continue
}

fileName := path.Join(mod, cgstrings.Camel(tokenToName(f.Token))+".go")
fileName := path.Join(mod, camel(tokenToName(f.Token))+".go")
code, err := pkg.genFunctionCodeFile(f)
if err != nil {
return nil, err
Expand Down
13 changes: 0 additions & 13 deletions pkg/codegen/go/gen_test.go
Expand Up @@ -403,16 +403,3 @@ import (
}
assert.Truef(t, found, `Didn't find a line that complies with "%v"`, autogenerated)
}
func TestTitle(t *testing.T) {
t.Parallel()
assert := assert.New(t)

assert.Equal("", Title(""))
assert.Equal("Plugh", Title("plugh"))
assert.Equal("WaldoThudFred", Title("WaldoThudFred"))
assert.Equal("WaldoThudFred", Title("waldoThudFred"))
assert.Equal("WaldoThudFred", Title("waldo-Thud-Fred"))
assert.Equal("WaldoThudFred", Title("waldo-ThudFred"))
assert.Equal("WaldoThud_Fred", Title("waldo-Thud_Fred"))
assert.Equal("WaldoThud_Fred", Title("waldo-thud_Fred"))
}
20 changes: 1 addition & 19 deletions pkg/codegen/go/utilities.go
Expand Up @@ -21,7 +21,6 @@ import (
"unicode"

"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/cgstrings"
)

// isReservedWord returns true if s is a Go reserved word as per
Expand Down Expand Up @@ -88,8 +87,7 @@ func makeSafeEnumName(name, typeName string) (string, error) {
}

// Capitalize and make a valid identifier.
safeName = enumTitle(safeName)
safeName = makeValidIdentifier(safeName)
safeName = makeValidIdentifier(Title(safeName))

// If there are multiple underscores in a row, replace with one.
regex := regexp.MustCompile(`_+`)
Expand All @@ -104,19 +102,3 @@ func makeSafeEnumName(name, typeName string) (string, error) {

return safeName, nil
}

// Title converts the input string to a title case
// where only the initial letter is upper-cased.
// It also removes $-prefix if any.
func enumTitle(s string) string {
if s == "" {
return ""
}
if s[0] == '$' {
return Title(s[1:])
}
s = cgstrings.UppercaseFirst(s)
return cgstrings.ModifyStringAroundDelimeter(s, "-", func(next string) string {
return "_" + cgstrings.UppercaseFirst(next)
})
}

0 comments on commit aaf547e

Please sign in to comment.