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.
44
44
| ltrim | Trims spaces from the left of the data provided in the params. |
45
45
| rtrim | Trims spaces from the right of the data provided in the params. |
46
46
| set | Set the provided value. |
47
+ | slug | Converts the field to a [ slug] ( https://github.com/gosimple/slug ) |
47
48
| snake | Snake Cases the data. |
48
49
| strip_alpha | Strips all ascii characters from the data. |
49
50
| 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 (
12
12
13
13
require (
14
14
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
15
17
github.com/pmezard/go-difflib v1.0.0 // indirect
16
18
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
17
19
)
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
2
2
github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
3
3
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A =
4
4
github.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 =
5
9
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
6
10
github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
7
11
github.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 {
16
16
mod .Register ("rtrim" , trimRight )
17
17
mod .Register ("set" , setValue )
18
18
mod .Register ("snake" , snakeCase )
19
+ mod .Register ("slug" , slugCase )
19
20
mod .Register ("strip_alpha_unicode" , stripAlphaUnicodeCase )
20
21
mod .Register ("strip_alpha" , stripAlphaCase )
21
22
mod .Register ("strip_num_unicode" , stripNumUnicodeCase )
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
"golang.org/x/text/language"
14
14
15
15
"github.com/go-playground/mold/v4"
16
+ "github.com/gosimple/slug"
16
17
"github.com/segmentio/go-camelcase"
17
18
"github.com/segmentio/go-snakecase"
18
19
)
@@ -89,6 +90,15 @@ func snakeCase(ctx context.Context, fl mold.FieldLevel) error {
89
90
return nil
90
91
}
91
92
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
+
92
102
// titleCase converts string to title case, e.g. "this is a sentence" -> "This Is A Sentence"
93
103
func titleCase (ctx context.Context , fl mold.FieldLevel ) error {
94
104
switch fl .Field ().Kind () {
Original file line number Diff line number Diff line change @@ -458,6 +458,52 @@ func TestTitleCase(t *testing.T) {
458
458
}
459
459
}
460
460
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
+
461
507
func TestNameCase (t * testing.T ) {
462
508
conform := New ()
463
509
You can’t perform that action at this time.
0 commit comments