-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert atlas.rpc.Identifier to snake case instead on lowercase (#99)
* Move CamelToSnake to util package * Convert atlas.rpc.Identifier to snake case instead on lowercase
- Loading branch information
Showing
7 changed files
with
78 additions
and
65 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package util | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") | ||
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") | ||
|
||
// CamelToSnake takes a camelcase string and returns a snake case string. | ||
func CamelToSnake(str string) string { | ||
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") | ||
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") | ||
return strings.ToLower(snake) | ||
} |
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,52 @@ | ||
package util | ||
|
||
import "testing" | ||
|
||
// TestUnaryServerInterceptor_ValidationErrors will run mock validation errors to see if it parses correctly. | ||
func TestCamelToSnake(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
actual string | ||
expected string | ||
}{ | ||
// Test cases | ||
{ | ||
"Testing CamelCase", | ||
"CamelCase", | ||
"camel_case", | ||
}, | ||
{ | ||
"Testing AnotherCamel123", | ||
"AnotherCamel123", | ||
"another_camel123", | ||
}, | ||
{ | ||
"Testing testCase", | ||
"testCase", | ||
"test_case", | ||
}, | ||
{ | ||
"Testing testcase", | ||
"testcase", | ||
"testcase", | ||
}, | ||
{ | ||
"Testing JSONData", | ||
"TestCaseUUID", | ||
"test_case_uuid", | ||
}, | ||
{ | ||
"Testing JSONData", | ||
"JSONData", | ||
"json_data", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
actual := CamelToSnake(tt.actual) | ||
expected := tt.expected | ||
if actual != expected { | ||
t.Errorf("CamelToSnake failed for test %s, expected: \"%s\", actual: \"%s\"", tt.name, expected, actual) | ||
} | ||
|
||
} | ||
} |