Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
camel case variants being converted into snake case variants now gain…
Browse files Browse the repository at this point in the history
… underscores before strings of numbers

Signed-off-by: Holly Hunt <holly.hunt1101@hotmail.com>
  • Loading branch information
Wyvinar committed Jun 7, 2024
1 parent fdfd7f3 commit 9c6b519
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion openapi2beans/pkg/utils/caseChanging.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ func snakeVariantsToPascal(inputString string) string {
// To snake functions
func camelVariantsToSnake(camelString string) string {
var snakeString string
var previousChar rune

for i, char := range camelString {
if unicode.IsUpper(char) && i != 0 {
if ((unicode.IsUpper(char)) || (unicode.IsNumber(char) && !unicode.IsNumber(previousChar))) && i != 0 {
snakeString += "_"
}
snakeString += string(char)
previousChar = char
}

return strings.ToLower(snakeString)
Expand Down
24 changes: 24 additions & 0 deletions openapi2beans/pkg/utils/caseChanging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func TestPascalToCamel(t *testing.T) {
camelString := pascalToCamel(pascalString)
assert.Equal(t, "pascalString", camelString)

pascalString = "PascalString123"
camelString = pascalToCamel(pascalString)
assert.Equal(t, "pascalString123", camelString)

pascalString = "P"
camelString = pascalToCamel(pascalString)
assert.Equal(t, "p", camelString)
Expand All @@ -33,6 +37,10 @@ func TestSnakeVariantsToCamel(t *testing.T) {
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "snakeString", camelString)

snakeString = "snake_string_123"
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "snakeString123", camelString)

snakeString = "p"
camelString = snakeVariantsToCamel(snakeString)
assert.Equal(t, "p", camelString)
Expand All @@ -51,6 +59,10 @@ func TestCamelToPascal(t *testing.T) {
pascalString := camelToPascal(camelString)
assert.Equal(t, "CamelString", pascalString)

camelString = "camelString123"
pascalString = camelToPascal(camelString)
assert.Equal(t, "CamelString123", pascalString)

camelString = "c"
pascalString = camelToPascal(camelString)
assert.Equal(t, "C", pascalString)
Expand All @@ -73,6 +85,10 @@ func TestSnakeVariantsToPascal(t *testing.T) {
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "SnakeString", pascalString)

snakeString = "snake_string_123"
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "SnakeString123", pascalString)

snakeString = "p"
pascalString = snakeVariantsToPascal(snakeString)
assert.Equal(t, "P", pascalString)
Expand All @@ -95,6 +111,10 @@ func TestCamelVariantsToSnake(t *testing.T) {
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "pascal_string", snakeString)

camelString = "camelString123"
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "camel_string_123", snakeString)

camelString = "c"
snakeString = camelVariantsToSnake(camelString)
assert.Equal(t, "c", snakeString)
Expand Down Expand Up @@ -143,6 +163,10 @@ func TestCamelVariantsToScreamingSnake(t *testing.T) {
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "PASCAL_STRING", snakeString)

camelString = "camelString123"
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "CAMEL_STRING_123", snakeString)

camelString = "c"
snakeString = camelVariantsToScreamingSnake(camelString)
assert.Equal(t, "C", snakeString)
Expand Down

0 comments on commit 9c6b519

Please sign in to comment.