Add SnakeCase
This commit is contained in:
parent
c533c90aed
commit
15ea874a75
3 changed files with 48 additions and 1 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/jinzhu/inflection"
|
||||
)
|
||||
|
@ -138,6 +139,22 @@ func CamelCase(name string) string {
|
|||
return strings.Join(splits, "")
|
||||
}
|
||||
|
||||
// SnakeCase converts TitleCase variable names to snake_case format.
|
||||
func SnakeCase(name string) string {
|
||||
runes := []rune(name)
|
||||
length := len(runes)
|
||||
|
||||
var out []rune
|
||||
for i := 0; i < length; i++ {
|
||||
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1]) || unicode.IsDigit(runes[i-1])) {
|
||||
out = append(out, '_')
|
||||
}
|
||||
out = append(out, unicode.ToLower(runes[i]))
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// MakeStringMap converts a map[string]string into the format:
|
||||
// "key": "value", "key": "value"
|
||||
func MakeStringMap(types map[string]string) string {
|
||||
|
|
|
@ -185,6 +185,35 @@ func TestCamelCase(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSnakeCase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
In string
|
||||
Out string
|
||||
}{
|
||||
{"ID", "id"},
|
||||
{"ID9", "id9"},
|
||||
{"Thing", "thing"},
|
||||
{"Thing92", "thing92"},
|
||||
{"ThingGUID", "thing_guid"},
|
||||
{"ThingGUIDTwo", "thing_guid_two"},
|
||||
{"ThingStuff", "thing_stuff"},
|
||||
{"ThingStuff1GUID", "thing_stuff1_guid"},
|
||||
{"ThingStuff12GUID", "thing_stuff12_guid"},
|
||||
{"ThingStuff123GUID", "thing_stuff123_guid"},
|
||||
{"GUIDThingStuffID123", "guid_thing_stuff_id123"},
|
||||
{"GUIDThingStuffID12", "guid_thing_stuff_id12"},
|
||||
{"GUIDThingStuffID1", "guid_thing_stuff_id1"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
if out := SnakeCase(test.In); out != test.Out {
|
||||
t.Errorf("%d) Want: %q, got %q", i, test.Out, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakeStringMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
@ -119,6 +119,7 @@ var templateStringMappers = map[string]func(string) string{
|
|||
"toUpper": strings.ToUpper,
|
||||
"titleCase": strmangle.TitleCase,
|
||||
"camelCase": strmangle.CamelCase,
|
||||
"snakeCase": strmangle.SnakeCase,
|
||||
}
|
||||
|
||||
// templateFunctions is a map of all the functions that get passed into the
|
||||
|
@ -168,7 +169,7 @@ var templateFunctions = template.FuncMap{
|
|||
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
||||
"filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault,
|
||||
"filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement,
|
||||
"filterColumnsByValidated": bdb.FilterColumnsByValidated,
|
||||
"filterColumnsByValidated": bdb.FilterColumnsByValidated,
|
||||
"autoIncPrimaryKey": bdb.AutoIncPrimaryKey,
|
||||
"sqlColDefinitions": bdb.SQLColDefinitions,
|
||||
"sqlColDefStrings": bdb.SQLColDefStrings,
|
||||
|
|
Loading…
Reference in a new issue