Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These functions modify the data in-place.
| ltrim | Trims spaces from the left of the data provided in the params. |
| rtrim | Trims spaces from the right of the data provided in the params. |
| set | Set the provided value. |
| slug | Converts the field to a [slug](https://github.com/gosimple/slug) |
| snake | Snake Cases the data. |
| strip_alpha | Strips all ascii characters from the data. |
| strip_alpha_unicode | Strips all unicode characters from the data. |
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gosimple/slug v1.13.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
Expand Down
1 change: 1 addition & 0 deletions modifiers/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func New() *mold.Transformer {
mod.Register("rtrim", trimRight)
mod.Register("set", setValue)
mod.Register("snake", snakeCase)
mod.Register("slug", slugCase)
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
mod.Register("strip_alpha", stripAlphaCase)
mod.Register("strip_num_unicode", stripNumUnicodeCase)
Expand Down
10 changes: 10 additions & 0 deletions modifiers/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/text/language"

"github.com/go-playground/mold/v4"
"github.com/gosimple/slug"
"github.com/segmentio/go-camelcase"
"github.com/segmentio/go-snakecase"
)
Expand Down Expand Up @@ -89,6 +90,15 @@ func snakeCase(ctx context.Context, fl mold.FieldLevel) error {
return nil
}

// slug converts string to a slug
func slugCase(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
case reflect.String:
fl.Field().SetString(slug.Make(fl.Field().String()))
}
return nil
}

// titleCase converts string to title case, e.g. "this is a sentence" -> "This Is A Sentence"
func titleCase(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
Expand Down
46 changes: 46 additions & 0 deletions modifiers/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,52 @@ func TestTitleCase(t *testing.T) {
}
}

func TestSlugCase(t *testing.T) {
conform := New()

s := "this-is +a SentencE9"
expected := "this-is-a-sentence9"

type Test struct {
String string `mod:"slug"`
}

tt := Test{String: s}
err := conform.Struct(context.Background(), &tt)
if err != nil {
log.Fatal(err)
}
if tt.String != expected {
t.Fatalf("Unexpected value '%s'\n", tt.String)
}

err = conform.Field(context.Background(), &s, "slug")
if err != nil {
log.Fatal(err)
}
if s != expected {
t.Fatalf("Unexpected value '%s'\n", s)
}

var iface interface{}
err = conform.Field(context.Background(), &iface, "slug")
if err != nil {
log.Fatal(err)
}
if iface != nil {
t.Fatalf("Unexpected value '%v'\n", nil)
}

iface = s
err = conform.Field(context.Background(), &iface, "slug")
if err != nil {
log.Fatal(err)
}
if iface != expected {
t.Fatalf("Unexpected value '%v'\n", iface)
}
}

func TestNameCase(t *testing.T) {
conform := New()

Expand Down