File tree Expand file tree Collapse file tree 6 files changed +64
-0
lines changed
Expand file tree Collapse file tree 6 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ These functions modify the data in-place.
4444| ltrim | Trims spaces from the left of the data provided in the params. |
4545| rtrim | Trims spaces from the right of the data provided in the params. |
4646| set | Set the provided value. |
47+ | slug | Converts the field to a [ slug] ( https://github.com/gosimple/slug ) |
4748| snake | Snake Cases the data. |
4849| strip_alpha | Strips all ascii characters from the data. |
4950| strip_alpha_unicode | Strips all unicode characters from the data. |
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ require (
1212
1313require (
1414 github.com/davecgh/go-spew v1.1.0 // indirect
15+ github.com/gosimple/slug v1.13.1 // indirect
16+ github.com/gosimple/unidecode v1.0.1 // indirect
1517 github.com/pmezard/go-difflib v1.0.0 // indirect
1618 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1719)
Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
33github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A =
44github.com/go-playground/assert/v2 v2.0.1 /go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4 =
5+ github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q =
6+ github.com/gosimple/slug v1.13.1 /go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ =
7+ github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o =
8+ github.com/gosimple/unidecode v1.0.1 /go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc =
59github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
610github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
711github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM =
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ func New() *mold.Transformer {
1616 mod .Register ("rtrim" , trimRight )
1717 mod .Register ("set" , setValue )
1818 mod .Register ("snake" , snakeCase )
19+ mod .Register ("slug" , slugCase )
1920 mod .Register ("strip_alpha_unicode" , stripAlphaUnicodeCase )
2021 mod .Register ("strip_alpha" , stripAlphaCase )
2122 mod .Register ("strip_num_unicode" , stripNumUnicodeCase )
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313 "golang.org/x/text/language"
1414
1515 "github.com/go-playground/mold/v4"
16+ "github.com/gosimple/slug"
1617 "github.com/segmentio/go-camelcase"
1718 "github.com/segmentio/go-snakecase"
1819)
@@ -89,6 +90,15 @@ func snakeCase(ctx context.Context, fl mold.FieldLevel) error {
8990 return nil
9091}
9192
93+ // slug converts string to a slug
94+ func slugCase (ctx context.Context , fl mold.FieldLevel ) error {
95+ switch fl .Field ().Kind () {
96+ case reflect .String :
97+ fl .Field ().SetString (slug .Make (fl .Field ().String ()))
98+ }
99+ return nil
100+ }
101+
92102// titleCase converts string to title case, e.g. "this is a sentence" -> "This Is A Sentence"
93103func titleCase (ctx context.Context , fl mold.FieldLevel ) error {
94104 switch fl .Field ().Kind () {
Original file line number Diff line number Diff line change @@ -458,6 +458,52 @@ func TestTitleCase(t *testing.T) {
458458 }
459459}
460460
461+ func TestSlugCase (t * testing.T ) {
462+ conform := New ()
463+
464+ s := "this-is +a SentencE9"
465+ expected := "this-is-a-sentence9"
466+
467+ type Test struct {
468+ String string `mod:"slug"`
469+ }
470+
471+ tt := Test {String : s }
472+ err := conform .Struct (context .Background (), & tt )
473+ if err != nil {
474+ log .Fatal (err )
475+ }
476+ if tt .String != expected {
477+ t .Fatalf ("Unexpected value '%s'\n " , tt .String )
478+ }
479+
480+ err = conform .Field (context .Background (), & s , "slug" )
481+ if err != nil {
482+ log .Fatal (err )
483+ }
484+ if s != expected {
485+ t .Fatalf ("Unexpected value '%s'\n " , s )
486+ }
487+
488+ var iface interface {}
489+ err = conform .Field (context .Background (), & iface , "slug" )
490+ if err != nil {
491+ log .Fatal (err )
492+ }
493+ if iface != nil {
494+ t .Fatalf ("Unexpected value '%v'\n " , nil )
495+ }
496+
497+ iface = s
498+ err = conform .Field (context .Background (), & iface , "slug" )
499+ if err != nil {
500+ log .Fatal (err )
501+ }
502+ if iface != expected {
503+ t .Fatalf ("Unexpected value '%v'\n " , iface )
504+ }
505+ }
506+
461507func TestNameCase (t * testing.T ) {
462508 conform := New ()
463509
You can’t perform that action at this time.
0 commit comments