Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 71 additions & 20 deletions pkg/gen/filters/common/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,153 @@ import (
"github.com/ettle/strcase"
)

func Preserve_(s string, handler func(string) string) string {
if strings.HasSuffix(s, "_") {
// remove trailing underscore and add it back
s = strings.TrimSuffix(s, "_")
s = handler(s)
s = s + "_"
return s
}
return handler(s)
}

// SnakeCaseLower returns a string representation of the value in snake_case.
func SnakeCaseLower(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '_')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '_')
})
}

// SnakeTitleCase returns a string representation of the value in snake_case.
func SnakeTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '_')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '_')
})
}

// First returns the first character of the value.
func SnakeUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '_')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '_')
})
}

// CamelCase returns a string representation of the value in CamelCase.
func CamelLowerCase(s string) string {
return strcase.ToCase(s, strcase.CamelCase, '\x00')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.CamelCase, '\x00')
})
}

// CamelTitleCase returns a string representation of the value in CamelTitleCase.
func CamelTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '\x00')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '\x00')
})
}

// CamelUpperCase returns a string representation of the value in CamelCase.
func CamelUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '\x00')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '\x00')
})
}

// DotLowerCase returns a string representation of the value in dot.case
func DotLowerCase(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '.')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '.')
})
}

// DotTitleCase returns a string representation of the value in dot.case
func DotTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '.')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '.')
})
}

// DotUpperCase returns a string representation of the value in DOT.CASE
func DotUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '.')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '.')
})
}

// KebabLowerCase returns a string representation of the value in kebap-case.
func KebabLowerCase(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '-')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '-')
})
}

// KebabTitleCase returns a string representation of the value in kebab-case.
func KebabTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
})
}

// KebapCaseUpper returns a string representation of the value in KEBAP-CASE.
func KebabUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '-')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '-')
})
}

// PathLowerCase returns a string representation of the value in path/case.
func PathLowerCase(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '/')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.LowerCase, '/')
})
}

// PathTitleCase returns a string representation of the value in path/case.
func PathTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '/')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '/')
})
}

// PathUpperCase returns a string representation of the value in PATH/CASE.
func PathUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '/')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, '/')
})
}

// LowerCase returns a string representation of the value in lowercase.
func LowerCase(s string) string {
return strings.ToLower(s)
return Preserve_(s, func(s string) string {
return strings.ToLower(s)
})
}

// UpperCase returns a string representation of the value in UPPER CASE.
func UpperCase(s string) string {
return strings.ToUpper(s)
return Preserve_(s, func(s string) string {
return strings.ToUpper(s)
})
}

// SpaceTitleCase returns a string representation of the value in Space Title Case.
func SpaceTitleCase(s string) string {
return strcase.ToCase(s, strcase.TitleCase, ' ')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.TitleCase, ' ')
})
}

// SpaceUpperCase returns a string representation of the value in SPACE UPPER CASE.
func SpaceUpperCase(s string) string {
return strcase.ToCase(s, strcase.UpperCase, ' ')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.UpperCase, ' ')
})
}

// SpaceLowerCase returns a string representation of the value in space lower case.
func SpaceLowerCase(s string) string {
return strcase.ToCase(s, strcase.LowerCase, ' ')
return Preserve_(s, func(s string) string {
return strcase.ToCase(s, strcase.LowerCase, ' ')
})
}
81 changes: 29 additions & 52 deletions pkg/gen/filters/common/cases_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package common

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSnakeCase(t *testing.T) {
t.Parallel()
Expand All @@ -16,22 +20,17 @@ func TestSnakeCase(t *testing.T) {
{"foo_bar", "foo_bar", "Foo_Bar", "FOO_BAR"},
{"foo.bar", "foo_bar", "Foo_Bar", "FOO_BAR"},
{"foo1bar", "foo1bar", "Foo1bar", "FOO1BAR"},
{"fooBar_", "foo_bar_", "Foo_Bar_", "FOO_BAR_"},
}
for _, tt := range tests {
t.Run(tt.lower, func(t *testing.T) {
if got := SnakeCaseLower(tt.in); got != tt.lower {
t.Errorf("SnakeCaseLower(%q) = %q, want %q", tt.in, got, tt.lower)
}
assert.Equal(t, tt.lower, SnakeCaseLower(tt.in))
})
t.Run(tt.title, func(t *testing.T) {
if got := SnakeTitleCase(tt.in); got != tt.title {
t.Errorf("SnakeTitleCase(%q) = %q, want %q", tt.in, got, tt.title)
}
assert.Equal(t, tt.title, SnakeTitleCase(tt.in))
})
t.Run(tt.upper, func(t *testing.T) {
if got := SnakeUpperCase(tt.in); got != tt.upper {
t.Errorf("SnakeUpperCase(%q) = %q, want %q", tt.in, got, tt.upper)
}
assert.Equal(t, tt.upper, SnakeUpperCase(tt.in))
})
}
}
Expand All @@ -50,22 +49,17 @@ func TestCamelCase(t *testing.T) {
{"foo_bar", "fooBar", "FooBar", "FOOBAR"},
{"foo.bar", "fooBar", "FooBar", "FOOBAR"},
{"foo1bar", "foo1bar", "Foo1bar", "FOO1BAR"},
{"fooBar_", "fooBar_", "FooBar_", "FOOBAR_"},
}
for _, tt := range tests {
t.Run(tt.lower, func(t *testing.T) {
if got := CamelLowerCase(tt.in); got != tt.lower {
t.Errorf("CamelLowerCase(%q) = %q, want %q", tt.in, got, tt.lower)
}
assert.Equal(t, tt.lower, CamelLowerCase(tt.in))
})
t.Run(tt.title, func(t *testing.T) {
if got := CamelTitleCase(tt.in); got != tt.title {
t.Errorf("CamelTitleCase(%q) = %q, want %q", tt.in, got, tt.title)
}
assert.Equal(t, tt.title, CamelTitleCase(tt.in))
})
t.Run(tt.upper, func(t *testing.T) {
if got := CamelUpperCase(tt.in); got != tt.upper {
t.Errorf("CamelUpperCase(%q) = %q, want %q", tt.in, got, tt.upper)
}
assert.Equal(t, tt.upper, CamelUpperCase(tt.in))
})
}
}
Expand All @@ -84,22 +78,17 @@ func TestDotCase(t *testing.T) {
{"foo_bar", "foo.bar", "Foo.Bar", "FOO.BAR"},
{"foo.bar", "foo.bar", "Foo.Bar", "FOO.BAR"},
{"foo1bar", "foo1bar", "Foo1bar", "FOO1BAR"},
{"fooBar_", "foo.bar_", "Foo.Bar_", "FOO.BAR_"},
}
for _, tt := range tests {
t.Run(tt.lower, func(t *testing.T) {
if got := DotLowerCase(tt.in); got != tt.lower {
t.Errorf("DotLowerCase(%q) = %q, want %q", tt.in, got, tt.lower)
}
assert.Equal(t, tt.lower, DotLowerCase(tt.in))
})
t.Run(tt.title, func(t *testing.T) {
if got := DotTitleCase(tt.in); got != tt.title {
t.Errorf("DotTitleCase(%q) = %q, want %q", tt.in, got, tt.title)
}
assert.Equal(t, tt.title, DotTitleCase(tt.in))
})
t.Run(tt.upper, func(t *testing.T) {
if got := DotUpperCase(tt.in); got != tt.upper {
t.Errorf("DotUpperCase(%q) = %q, want %q", tt.in, got, tt.upper)
}
assert.Equal(t, tt.upper, DotUpperCase(tt.in))
})
}
}
Expand All @@ -118,22 +107,17 @@ func TestKebabCase(t *testing.T) {
{"foo_bar", "foo-bar", "Foo-Bar", "FOO-BAR"},
{"foo.bar", "foo-bar", "Foo-Bar", "FOO-BAR"},
{"foo1bar", "foo1bar", "Foo1bar", "FOO1BAR"},
{"fooBar_", "foo-bar_", "Foo-Bar_", "FOO-BAR_"},
}
for _, tt := range tests {
t.Run(tt.lower, func(t *testing.T) {
if got := KebabLowerCase(tt.in); got != tt.lower {
t.Errorf("KebabLowerCase(%q) = %q, want %q", tt.in, got, tt.lower)
}
assert.Equal(t, tt.lower, KebabLowerCase(tt.in))
})
t.Run(tt.title, func(t *testing.T) {
if got := KebabTitleCase(tt.in); got != tt.title {
t.Errorf("KebabTitleCase(%q) = %q, want %q", tt.in, got, tt.title)
}
assert.Equal(t, tt.title, KebabTitleCase(tt.in))
})
t.Run(tt.upper, func(t *testing.T) {
if got := KebabUpperCase(tt.in); got != tt.upper {
t.Errorf("KebabUpperCase(%q) = %q, want %q", tt.in, got, tt.upper)
}
assert.Equal(t, tt.upper, KebabUpperCase(tt.in))
})
}
}
Expand All @@ -152,22 +136,17 @@ func TestPathCase(t *testing.T) {
{"foo_bar", "foo/bar", "Foo/Bar", "FOO/BAR"},
{"foo.bar", "foo/bar", "Foo/Bar", "FOO/BAR"},
{"foo1bar", "foo1bar", "Foo1bar", "FOO1BAR"},
{"fooBar_", "foo/bar_", "Foo/Bar_", "FOO/BAR_"},
}
for _, tt := range tests {
t.Run(tt.lower, func(t *testing.T) {
if got := PathLowerCase(tt.in); got != tt.lower {
t.Errorf("PathLowerCase(%q) = %q, want %q", tt.in, got, tt.lower)
}
assert.Equal(t, tt.lower, PathLowerCase(tt.in))
})
t.Run(tt.title, func(t *testing.T) {
if got := PathTitleCase(tt.in); got != tt.title {
t.Errorf("PathTitleCase(%q) = %q, want %q", tt.in, got, tt.title)
}
assert.Equal(t, tt.title, PathTitleCase(tt.in))
})
t.Run(tt.upper, func(t *testing.T) {
if got := PathUpperCase(tt.in); got != tt.upper {
t.Errorf("PathUpperCase(%q) = %q, want %q", tt.in, got, tt.upper)
}
assert.Equal(t, tt.upper, PathUpperCase(tt.in))
})
}
}
Expand All @@ -184,12 +163,11 @@ func TestUpperCase(t *testing.T) {
{"foo_bar", "FOO_BAR"},
{"foo.bar", "FOO.BAR"},
{"foo1bar", "FOO1BAR"},
{"fooBar_", "FOOBAR_"},
}
for _, tt := range tests {
t.Run(tt.up, func(t *testing.T) {
if got := UpperCase(tt.in); got != tt.up {
t.Errorf("Upper(%q) = %q, want %q", tt.in, got, tt.up)
}
assert.Equal(t, tt.up, UpperCase(tt.in))
})
}
}
Expand All @@ -206,12 +184,11 @@ func TestLowerCase(t *testing.T) {
{"foo_bar", "foo_bar"},
{"foo.bar", "foo.bar"},
{"foo1bar", "foo1bar"},
{"fooBar_", "foobar_"},
}
for _, tt := range tests {
t.Run(tt.lw, func(t *testing.T) {
if got := LowerCase(tt.in); got != tt.lw {
t.Errorf("Lower(%q) = %q, want %q", tt.in, got, tt.lw)
}
assert.Equal(t, tt.lw, LowerCase(tt.in))
})
}
}
2 changes: 1 addition & 1 deletion pkg/gen/filters/common/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TrimSuffix(s, postfix string) string {
}

func Replace(s, old, new string) string {
return strings.Replace(s, old, new, -1)
return strings.ReplaceAll(s, old, new)
}

func Split(s, sep string) []string {
Expand Down
Loading