Skip to content

Commit 328286d

Browse files
authored
Add empty modifier (#39)
1 parent 3acdfd9 commit 328286d

File tree

5 files changed

+109
-26
lines changed

5 files changed

+109
-26
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,24 @@ These functions modify the data in-place.
3737

3838
| Name | Description |
3939
|-------|--------------|
40+
| camel | Camel Cases the data. |
4041
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
41-
| trim | Trims space from the data. |
42+
| empty | Sets the field equal to the datatype default value. e.g. 0 for int. |
43+
| lcase | lowercases the data. |
4244
| ltrim | Trims spaces from the left of the data provided in the params. |
4345
| rtrim | Trims spaces from the right of the data provided in the params. |
44-
| tprefix | Trims a prefix from the value using the provided param value. |
45-
| tsuffix | Trims a suffix from the value using the provided param value. |
46-
| lcase | lowercases the data. |
47-
| ucase | Uppercases the data. |
4846
| snake | Snake Cases the data. |
49-
| camel | Camel Cases the data. |
50-
| title | Title Cases the data. |
51-
| ucfirst | Upper cases the first character of the data. |
5247
| strip_alpha | Strips all ascii characters from the data. |
53-
| strip_num | Strips all ascii numeric characters from the data. |
5448
| strip_alpha_unicode | Strips all unicode characters from the data. |
49+
| strip_num | Strips all ascii numeric characters from the data. |
5550
| strip_num_unicode | Strips all unicode numeric characters from the data. |
5651
| strip_punctuation | Strips all ascii punctuation from the data. |
52+
| title | Title Cases the data. |
53+
| tprefix | Trims a prefix from the value using the provided param value. |
54+
| trim | Trims space from the data. |
55+
| tsuffix | Trims a suffix from the value using the provided param value. |
56+
| ucase | Uppercases the data. |
57+
| ucfirst | Upper cases the first character of the data. |
5758

5859

5960

modifiers/modifiers.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ import (
77
// New returns a modifier with defaults registered
88
func New() *mold.Transformer {
99
mod := mold.New()
10-
mod.SetTagName("mod")
11-
mod.Register("trim", trimSpace)
10+
mod.Register("camel", camelCase)
11+
mod.Register("default", defaultValue)
12+
mod.Register("empty", empty)
13+
mod.Register("lcase", toLower)
1214
mod.Register("ltrim", trimLeft)
15+
mod.Register("name", nameCase)
1316
mod.Register("rtrim", trimRight)
14-
mod.Register("tprefix", trimPrefix)
15-
mod.Register("tsuffix", trimSuffix)
16-
mod.Register("lcase", toLower)
17-
mod.Register("ucase", toUpper)
1817
mod.Register("snake", snakeCase)
19-
mod.Register("title", titleCase)
20-
mod.Register("name", nameCase)
21-
mod.Register("ucfirst", uppercaseFirstCharacterCase)
18+
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
2219
mod.Register("strip_alpha", stripAlphaCase)
23-
mod.Register("strip_num", stripNumCase)
2420
mod.Register("strip_num_unicode", stripNumUnicodeCase)
25-
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
21+
mod.Register("strip_num", stripNumCase)
2622
mod.Register("strip_punctuation", stripPunctuation)
27-
mod.Register("camel", camelCase)
28-
mod.Register("default", defaultValue)
23+
mod.Register("title", titleCase)
24+
mod.Register("tprefix", trimPrefix)
25+
mod.Register("trim", trimSpace)
26+
mod.Register("tsuffix", trimSuffix)
27+
mod.Register("ucase", toUpper)
28+
mod.Register("ucfirst", uppercaseFirstCharacterCase)
29+
mod.SetTagName("mod")
2930
return mod
3031
}

modifiers/multi.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ var (
1313
durationType = reflect.TypeOf(time.Duration(0))
1414
)
1515

16-
//
1716
// defaultValue allows setting of a default value IF no value is already present.
18-
//
1917
func defaultValue(ctx context.Context, fl mold.FieldLevel) error {
2018
if !fl.Field().IsZero() {
2119
return nil
@@ -74,3 +72,10 @@ func defaultValue(ctx context.Context, fl mold.FieldLevel) error {
7472
}
7573
return nil
7674
}
75+
76+
// empty sets the field to the zero value of the field type
77+
func empty(ctx context.Context, fl mold.FieldLevel) error {
78+
zeroValue := reflect.Zero(fl.Field().Type())
79+
fl.Field().Set(zeroValue)
80+
return nil
81+
}

modifiers/multi_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,78 @@ func TestDefault(t *testing.T) {
112112
})
113113
}
114114
}
115+
116+
func TestEmpty(t *testing.T) {
117+
118+
type State int
119+
const FINISHED State = 5
120+
121+
var state State
122+
123+
conform := New()
124+
125+
tests := []struct {
126+
name string
127+
field interface{}
128+
tags string
129+
expected interface{}
130+
expectError bool
131+
}{
132+
{
133+
name: "empty enum",
134+
field: FINISHED,
135+
tags: "empty",
136+
expected: state,
137+
},
138+
{
139+
name: "empty string",
140+
field: "test",
141+
tags: "empty",
142+
expected: "",
143+
},
144+
{
145+
name: "empty int",
146+
field: 10,
147+
tags: "empty",
148+
expected: 0,
149+
},
150+
{
151+
name: "empty uint",
152+
field: uint(10),
153+
tags: "empty",
154+
expected: uint(0),
155+
},
156+
{
157+
name: "empty float",
158+
field: float32(10),
159+
tags: "empty",
160+
expected: float32(0),
161+
},
162+
{
163+
name: "empty bool",
164+
field: true,
165+
tags: "empty",
166+
expected: false,
167+
},
168+
{
169+
name: "empty time.Duration",
170+
field: time.Duration(10),
171+
tags: "empty",
172+
expected: time.Duration(0),
173+
},
174+
}
175+
176+
for _, tc := range tests {
177+
tc := tc
178+
t.Run(tc.name, func(t *testing.T) {
179+
t.Parallel()
180+
err := conform.Field(context.Background(), &tc.field, tc.tags)
181+
if tc.expectError {
182+
NotEqual(t, err, nil)
183+
return
184+
}
185+
Equal(t, err, nil)
186+
Equal(t, tc.field, tc.expected)
187+
})
188+
}
189+
}

modifiers/string.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package modifiers
33
import (
44
"bytes"
55
"context"
6-
"golang.org/x/text/cases"
7-
"golang.org/x/text/language"
86
"reflect"
97
"regexp"
108
"strings"
119
"unicode"
1210
"unicode/utf8"
1311

12+
"golang.org/x/text/cases"
13+
"golang.org/x/text/language"
14+
1415
"github.com/go-playground/mold/v4"
1516
"github.com/segmentio/go-camelcase"
1617
"github.com/segmentio/go-snakecase"

0 commit comments

Comments
 (0)