-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
11049: Go & Python handle hyphen in sdk gen (cgstrings take 2) r=iwahbe a=iwahbe <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes pulumi/crd2pulumi#43 ## Checklist <!--- Please provide details if the checkbox below is to be left unchecked. --> - [X] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [X] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Service, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. --> 11055: feat(programtest): Enable ProgramTest for Azure-Native split modules r=AaronFriel a=AaronFriel Unblocks Azure Native usage of ProgramTests in Go, resolves #11050's example by permitting usage such as: ```go func genReplace(t *testing.T, subPkg string) string { moduleDir, err := filepath.Abs(fmt.Sprintf("../pulumi-azure-native-sdk/%s", subPkg)) require.NoError(t, err) return fmt.Sprintf("github.com/pulumi/pulumi-azure-native-sdk/%s=%s", subPkg, moduleDir) } func getGoBaseOptions(t *testing.T) integration.ProgramTestOptions { base := getBaseOptions(t) baseGo := base.With(integration.ProgramTestOptions{ Dependencies: map[string]string{ genReplace("aad"), }, }) return baseGo } ``` Resolves #11050 Co-authored-by: Ian Wahbe <[email protected]> Co-authored-by: Aaron Friel <[email protected]>
- Loading branch information
Showing
30 changed files
with
1,381 additions
and
48 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelog/pending/20221017--sdkgen-go-python--handle-hyphenated-names.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
changes: | ||
- type: fix | ||
scope: sdkgen/go,python | ||
description: Handle hypheneated names in go and python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// package cgstrings has various string processing functions that are useful during code generation. | ||
package cgstrings | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// Unhyphenate removes all hyphens from s, then uppercasing the letter following each hyphen. | ||
// For example, "abc-def-ghi" becomes "abcDefGhi". | ||
func Unhyphenate(str string) string { | ||
return ModifyStringAroundDelimeter(str, "-", UppercaseFirst) | ||
} | ||
|
||
// Camel converts s to camelCase. | ||
func Camel(s string) string { | ||
if s == "" { | ||
return "" | ||
} | ||
s = Unhyphenate(s) | ||
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) | ||
} | ||
|
||
// UppercaseFirst uppercases the first letter of s. | ||
// E.g. "abc" -> "Abc" | ||
func UppercaseFirst(s string) string { | ||
if s == "" { | ||
return "" | ||
} | ||
runes := []rune(s) | ||
runes[0] = unicode.ToUpper(runes[0]) | ||
return string(runes) | ||
} | ||
|
||
func ModifyStringAroundDelimeter(str, delim string, modifyNext func(next string) string) string { | ||
if delim == "" { | ||
return str | ||
} | ||
i := strings.Index(str, delim) | ||
if i < 0 { | ||
return str | ||
} | ||
nextIdx := i + len(delim) | ||
if nextIdx >= len(str) { | ||
// Nothing left after the delimeter, it's at the end of the string. | ||
return str[:len(str)-len(delim)] | ||
} | ||
prev := str[:nextIdx-1] | ||
next := str[nextIdx:] | ||
if next != "" { | ||
next = modifyNext(next) | ||
} | ||
return prev + ModifyStringAroundDelimeter(next, delim, modifyNext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cgstrings | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCamel(t *testing.T) { | ||
t.Parallel() | ||
assert := assert.New(t) | ||
|
||
assert.Equal("", Camel("")) | ||
assert.Equal("plugh", Camel("plugh")) | ||
assert.Equal("waldoThudFred", Camel("WaldoThudFred")) | ||
assert.Equal("graultBaz", Camel("Grault-Baz")) | ||
assert.Equal("graultBaz", Camel("grault-baz")) | ||
assert.Equal("graultBaz", Camel("graultBaz")) | ||
assert.Equal("grault_Baz", Camel("Grault_Baz")) | ||
assert.Equal("graultBaz", Camel("Grault-baz")) | ||
} | ||
|
||
func TestUnhyphenate(t *testing.T) { | ||
t.Parallel() | ||
testcases := []struct { | ||
input, expected string | ||
}{ | ||
{"", ""}, | ||
{"waldo", "waldo"}, | ||
{"waldo-thud-fred", "waldoThudFred"}, | ||
{"waldo-Thud-Fred", "waldoThudFred"}, | ||
{"waldo-Thud-Fred-", "waldoThudFred"}, | ||
{"-waldo-Thud-Fred", "WaldoThudFred"}, | ||
{"waldoThudFred", "waldoThudFred"}, | ||
{"WaldoThudFred", "WaldoThudFred"}, | ||
} | ||
for _, tc := range testcases { | ||
tc := tc | ||
t.Run(fmt.Sprintf("Subtest:%q", tc.input), func(t *testing.T) { | ||
t.Parallel() | ||
assert := assert.New(t) | ||
assert.Equal(tc.expected, Unhyphenate(tc.input)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.