Skip to content

Commit 1d5da9d

Browse files
author
Dean Karn
authored
add set modifier (#41)
1 parent 328286d commit 1d5da9d

File tree

4 files changed

+154
-31
lines changed

4 files changed

+154
-31
lines changed

README.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package mold
22
============
3-
![Project status](https://img.shields.io/badge/version-4.2.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-4.3.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/mold.svg?branch=v2)](https://travis-ci.org/go-playground/mold)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/mold/badge.svg?branch=v2)](https://coveralls.io/github/go-playground/mold?branch=v2)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/mold)](https://goreportcard.com/report/github.com/go-playground/mold)
@@ -35,41 +35,42 @@ Modifiers
3535
----------
3636
These functions modify the data in-place.
3737

38-
| Name | Description |
39-
|-------|--------------|
40-
| camel | Camel Cases the data. |
41-
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
42-
| empty | Sets the field equal to the datatype default value. e.g. 0 for int. |
43-
| lcase | lowercases the data. |
44-
| ltrim | Trims spaces from the left of the data provided in the params. |
45-
| rtrim | Trims spaces from the right of the data provided in the params. |
46-
| snake | Snake Cases the data. |
47-
| strip_alpha | Strips all ascii characters from the data. |
48-
| strip_alpha_unicode | Strips all unicode characters from the data. |
49-
| strip_num | Strips all ascii numeric characters from the data. |
50-
| strip_num_unicode | Strips all unicode numeric characters from the data. |
51-
| 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. |
38+
| Name | Description |
39+
|---------------------|-------------------------------------------------------------------------------------------|
40+
| camel | Camel Cases the data. |
41+
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
42+
| empty | Sets the field equal to the datatype default value. e.g. 0 for int. |
43+
| lcase | lowercases the data. |
44+
| ltrim | Trims spaces from the left of the data provided in the params. |
45+
| rtrim | Trims spaces from the right of the data provided in the params. |
46+
| set | Set the provided value. |
47+
| snake | Snake Cases the data. |
48+
| strip_alpha | Strips all ascii characters from the data. |
49+
| strip_alpha_unicode | Strips all unicode characters from the data. |
50+
| strip_num | Strips all ascii numeric characters from the data. |
51+
| strip_num_unicode | Strips all unicode numeric characters from the data. |
52+
| strip_punctuation | Strips all ascii punctuation from the data. |
53+
| title | Title Cases the data. |
54+
| tprefix | Trims a prefix from the value using the provided param value. |
55+
| trim | Trims space from the data. |
56+
| tsuffix | Trims a suffix from the value using the provided param value. |
57+
| ucase | Uppercases the data. |
58+
| ucfirst | Upper cases the first character of the data. |
5859

5960

6061

6162
Scrubbers
6263
----------
6364
These functions obfuscate the specified types within the data for pii purposes.
6465

65-
| Name | Description |
66-
|-------|--------------|
67-
| emails | Scrubs multiple emails from data. |
68-
| email | Scrubs the data from and specifies the sha name of the same name. |
69-
| text | Scrubs the data from and specifies the sha name of the same name. |
70-
| name | Scrubs the data from and specifies the sha name of the same name. |
71-
| fname | Scrubs the data from and specifies the sha name of the same name. |
72-
| lname | Scrubs the data from and specifies the sha name of the same name. |
66+
| Name | Description |
67+
|--------|-------------------------------------------------------------------|
68+
| emails | Scrubs multiple emails from data. |
69+
| email | Scrubs the data from and specifies the sha name of the same name. |
70+
| text | Scrubs the data from and specifies the sha name of the same name. |
71+
| name | Scrubs the data from and specifies the sha name of the same name. |
72+
| fname | Scrubs the data from and specifies the sha name of the same name. |
73+
| lname | Scrubs the data from and specifies the sha name of the same name. |
7374

7475

7576
Special Information
@@ -78,8 +79,8 @@ Special Information
7879

7980
Contributing
8081
------------
81-
I am definitly interested in the communities help in adding more scrubbers and modifiers.
82-
Please send a PR with tests, and prefereably no extra dependencies, at lease until a solid base
82+
I am definitely interested in the communities help in adding more scrubbers and modifiers.
83+
Please send a PR with tests, and preferably no extra dependencies, at lease until a solid base
8384
has been built.
8485

8586
Complimentary Software

modifiers/modifiers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func New() *mold.Transformer {
1414
mod.Register("ltrim", trimLeft)
1515
mod.Register("name", nameCase)
1616
mod.Register("rtrim", trimRight)
17+
mod.Register("set", setValue)
1718
mod.Register("snake", snakeCase)
1819
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
1920
mod.Register("strip_alpha", stripAlphaCase)

modifiers/multi.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func defaultValue(ctx context.Context, fl mold.FieldLevel) error {
1818
if !fl.Field().IsZero() {
1919
return nil
2020
}
21+
return setValue(ctx, fl)
22+
}
2123

24+
// setValue allows setting of a specified value
25+
func setValue(ctx context.Context, fl mold.FieldLevel) error {
2226
switch fl.Field().Kind() {
2327
case reflect.String:
2428
fl.Field().SetString(fl.Param())

modifiers/multi_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,117 @@ import (
88
. "github.com/go-playground/assert/v2"
99
)
1010

11+
func TestSet(t *testing.T) {
12+
13+
type State int
14+
const FINISHED State = 5
15+
16+
var state State
17+
18+
conform := New()
19+
20+
tests := []struct {
21+
name string
22+
field interface{}
23+
tags string
24+
expected interface{}
25+
expectError bool
26+
}{
27+
{
28+
name: "set State (although enum default value should be the default in practice)",
29+
field: state,
30+
tags: "set=5",
31+
expected: FINISHED,
32+
},
33+
{
34+
name: "set string",
35+
field: "",
36+
tags: "set=test",
37+
expected: "test",
38+
},
39+
{
40+
name: "set string",
41+
field: "existing_value",
42+
tags: "set=test",
43+
expected: "test",
44+
},
45+
{
46+
name: "set int",
47+
field: 0,
48+
tags: "set=3",
49+
expected: 3,
50+
},
51+
{
52+
name: "set uint",
53+
field: uint(0),
54+
tags: "default=4",
55+
expected: uint(4),
56+
},
57+
{
58+
name: "set float",
59+
field: float32(0),
60+
tags: "set=5",
61+
expected: float32(5),
62+
},
63+
{
64+
name: "set bool",
65+
field: false,
66+
tags: "set=true",
67+
expected: true,
68+
},
69+
{
70+
name: "set time.Duration",
71+
field: time.Duration(0),
72+
tags: "set=1s",
73+
expected: time.Duration(1_000_000_000),
74+
},
75+
{
76+
name: "bad set time.Duration",
77+
field: time.Duration(0),
78+
tags: "set=rex",
79+
expectError: true,
80+
},
81+
{
82+
name: "set default int",
83+
field: 0,
84+
tags: "set=abc",
85+
expectError: true,
86+
},
87+
{
88+
name: "bad set uint",
89+
field: uint(0),
90+
tags: "set=abc",
91+
expectError: true,
92+
},
93+
{
94+
name: "bad set float",
95+
field: float32(0),
96+
tags: "default=abc",
97+
expectError: true,
98+
},
99+
{
100+
name: "bad set bool",
101+
field: false,
102+
tags: "default=blue",
103+
expectError: true,
104+
},
105+
}
106+
107+
for _, tc := range tests {
108+
tc := tc
109+
t.Run(tc.name, func(t *testing.T) {
110+
t.Parallel()
111+
err := conform.Field(context.Background(), &tc.field, tc.tags)
112+
if tc.expectError {
113+
NotEqual(t, err, nil)
114+
return
115+
}
116+
Equal(t, err, nil)
117+
Equal(t, tc.field, tc.expected)
118+
})
119+
}
120+
}
121+
11122
func TestDefault(t *testing.T) {
12123

13124
type State int
@@ -36,6 +147,12 @@ func TestDefault(t *testing.T) {
36147
tags: "default=test",
37148
expected: "test",
38149
},
150+
{
151+
name: "default string",
152+
field: "existing_value",
153+
tags: "default=test",
154+
expected: "existing_value",
155+
},
39156
{
40157
name: "default int",
41158
field: 0,

0 commit comments

Comments
 (0)