Skip to content

Commit

Permalink
add set modifier (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Feb 18, 2023
1 parent 328286d commit 1d5da9d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 31 deletions.
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package mold
============
![Project status](https://img.shields.io/badge/version-4.2.1-green.svg)
![Project status](https://img.shields.io/badge/version-4.3.0-green.svg)
[![Build Status](https://travis-ci.org/go-playground/mold.svg?branch=v2)](https://travis-ci.org/go-playground/mold)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/mold/badge.svg?branch=v2)](https://coveralls.io/github/go-playground/mold?branch=v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/mold)](https://goreportcard.com/report/github.com/go-playground/mold)
Expand Down Expand Up @@ -35,41 +35,42 @@ Modifiers
----------
These functions modify the data in-place.

| Name | Description |
|-------|--------------|
| camel | Camel Cases the data. |
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
| empty | Sets the field equal to the datatype default value. e.g. 0 for int. |
| lcase | lowercases the data. |
| 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. |
| snake | Snake Cases the data. |
| strip_alpha | Strips all ascii characters from the data. |
| strip_alpha_unicode | Strips all unicode characters from the data. |
| strip_num | Strips all ascii numeric characters from the data. |
| strip_num_unicode | Strips all unicode numeric characters from the data. |
| strip_punctuation | Strips all ascii punctuation from the data. |
| title | Title Cases the data. |
| tprefix | Trims a prefix from the value using the provided param value. |
| trim | Trims space from the data. |
| tsuffix | Trims a suffix from the value using the provided param value. |
| ucase | Uppercases the data. |
| ucfirst | Upper cases the first character of the data. |
| Name | Description |
|---------------------|-------------------------------------------------------------------------------------------|
| camel | Camel Cases the data. |
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
| empty | Sets the field equal to the datatype default value. e.g. 0 for int. |
| lcase | lowercases the data. |
| 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. |
| snake | Snake Cases the data. |
| strip_alpha | Strips all ascii characters from the data. |
| strip_alpha_unicode | Strips all unicode characters from the data. |
| strip_num | Strips all ascii numeric characters from the data. |
| strip_num_unicode | Strips all unicode numeric characters from the data. |
| strip_punctuation | Strips all ascii punctuation from the data. |
| title | Title Cases the data. |
| tprefix | Trims a prefix from the value using the provided param value. |
| trim | Trims space from the data. |
| tsuffix | Trims a suffix from the value using the provided param value. |
| ucase | Uppercases the data. |
| ucfirst | Upper cases the first character of the data. |



Scrubbers
----------
These functions obfuscate the specified types within the data for pii purposes.

| Name | Description |
|-------|--------------|
| emails | Scrubs multiple emails from data. |
| email | Scrubs the data from and specifies the sha name of the same name. |
| text | Scrubs the data from and specifies the sha name of the same name. |
| name | Scrubs the data from and specifies the sha name of the same name. |
| fname | Scrubs the data from and specifies the sha name of the same name. |
| lname | Scrubs the data from and specifies the sha name of the same name. |
| Name | Description |
|--------|-------------------------------------------------------------------|
| emails | Scrubs multiple emails from data. |
| email | Scrubs the data from and specifies the sha name of the same name. |
| text | Scrubs the data from and specifies the sha name of the same name. |
| name | Scrubs the data from and specifies the sha name of the same name. |
| fname | Scrubs the data from and specifies the sha name of the same name. |
| lname | Scrubs the data from and specifies the sha name of the same name. |


Special Information
Expand All @@ -78,8 +79,8 @@ Special Information

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

Complimentary Software
Expand Down
1 change: 1 addition & 0 deletions modifiers/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func New() *mold.Transformer {
mod.Register("ltrim", trimLeft)
mod.Register("name", nameCase)
mod.Register("rtrim", trimRight)
mod.Register("set", setValue)
mod.Register("snake", snakeCase)
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
mod.Register("strip_alpha", stripAlphaCase)
Expand Down
4 changes: 4 additions & 0 deletions modifiers/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func defaultValue(ctx context.Context, fl mold.FieldLevel) error {
if !fl.Field().IsZero() {
return nil
}
return setValue(ctx, fl)
}

// setValue allows setting of a specified value
func setValue(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
case reflect.String:
fl.Field().SetString(fl.Param())
Expand Down
117 changes: 117 additions & 0 deletions modifiers/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,117 @@ import (
. "github.com/go-playground/assert/v2"
)

func TestSet(t *testing.T) {

type State int
const FINISHED State = 5

var state State

conform := New()

tests := []struct {
name string
field interface{}
tags string
expected interface{}
expectError bool
}{
{
name: "set State (although enum default value should be the default in practice)",
field: state,
tags: "set=5",
expected: FINISHED,
},
{
name: "set string",
field: "",
tags: "set=test",
expected: "test",
},
{
name: "set string",
field: "existing_value",
tags: "set=test",
expected: "test",
},
{
name: "set int",
field: 0,
tags: "set=3",
expected: 3,
},
{
name: "set uint",
field: uint(0),
tags: "default=4",
expected: uint(4),
},
{
name: "set float",
field: float32(0),
tags: "set=5",
expected: float32(5),
},
{
name: "set bool",
field: false,
tags: "set=true",
expected: true,
},
{
name: "set time.Duration",
field: time.Duration(0),
tags: "set=1s",
expected: time.Duration(1_000_000_000),
},
{
name: "bad set time.Duration",
field: time.Duration(0),
tags: "set=rex",
expectError: true,
},
{
name: "set default int",
field: 0,
tags: "set=abc",
expectError: true,
},
{
name: "bad set uint",
field: uint(0),
tags: "set=abc",
expectError: true,
},
{
name: "bad set float",
field: float32(0),
tags: "default=abc",
expectError: true,
},
{
name: "bad set bool",
field: false,
tags: "default=blue",
expectError: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := conform.Field(context.Background(), &tc.field, tc.tags)
if tc.expectError {
NotEqual(t, err, nil)
return
}
Equal(t, err, nil)
Equal(t, tc.field, tc.expected)
})
}
}

func TestDefault(t *testing.T) {

type State int
Expand Down Expand Up @@ -36,6 +147,12 @@ func TestDefault(t *testing.T) {
tags: "default=test",
expected: "test",
},
{
name: "default string",
field: "existing_value",
tags: "default=test",
expected: "existing_value",
},
{
name: "default int",
field: 0,
Expand Down

0 comments on commit 1d5da9d

Please sign in to comment.