Skip to content

Commit a63be57

Browse files
author
Dean Karn
authored
V4 release (#31)
1 parent b02a779 commit a63be57

File tree

13 files changed

+350
-357
lines changed

13 files changed

+350
-357
lines changed

README.md

Lines changed: 47 additions & 167 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-3.1.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-4.0.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)
@@ -16,175 +16,55 @@ Installation
1616

1717
Use go get.
1818
```shell
19-
go get -u github.com/go-playground/mold/v3
19+
go get -u github.com/go-playground/mold/v4
2020
```
2121

22-
Then import the form package into your own code.
22+
Examples
23+
----------
24+
| Example | Description |
25+
|----------------------------------|--------------------------------------------------------------------|
26+
| [simple](_examples/mold/main.go) | A basic example with custom function. |
27+
| [full](_examples/full/main.go) | A more real life example combining the usage of multiple packages. |
28+
29+
30+
Modifiers
31+
----------
32+
These functions modify the data in-place.
33+
34+
| Name | Description |
35+
|-------|--------------|
36+
| default | Sets the provided default value only if the data is equal to it's default datatype value. |
37+
| trim | Trims space from the data. |
38+
| ltrim | Trims spaces from the left of the data provided in the params. |
39+
| rtrim | Trims spaces from the right of the data provided in the params. |
40+
| tprefix | Trims a prefix from the value using the provided param value. |
41+
| tsuffix | Trims a suffix from the value using the provided param value. |
42+
| lcase | lowercases the data. |
43+
| ucase | Uppercases the data. |
44+
| snake | Snake Cases the data. |
45+
| camel | Camel Cases the data. |
46+
| title | Title Cases the data. |
47+
| ucfirst | Upper cases the first character of the data. |
48+
| strip_alpha | Strips all ascii characters from the data. |
49+
| strip_num | Strips all ascii numeric characters from the data. |
50+
| strip_alpha_unicode | Strips all unicode characters from the data. |
51+
| strip_num_unicode | Strips all unicode numeric characters from the data. |
52+
53+
54+
55+
Scrubbers
56+
----------
57+
These functions obfuscate the specified types within the data for pii purposes.
58+
59+
| Name | Description |
60+
|-------|--------------|
61+
| emails | Scrubs multiple emails from data. |
62+
| email | Scrubs the data from and specifies the sha name of the same name. |
63+
| text | Scrubs the data from and specifies the sha name of the same name. |
64+
| name | Scrubs the data from and specifies the sha name of the same name. |
65+
| fname | Scrubs the data from and specifies the sha name of the same name. |
66+
| lname | Scrubs the data from and specifies the sha name of the same name. |
2367

24-
import "github.com/go-playground/mold/v3"
25-
26-
Simple example
27-
-----
28-
```go
29-
package main
30-
31-
import (
32-
"context"
33-
"fmt"
34-
"log"
35-
"reflect"
36-
37-
"github.com/go-playground/mold/v3"
38-
)
39-
40-
var tform *mold.Transformer
41-
42-
func main() {
43-
tform = mold.New()
44-
tform.Register("set", transformMyData)
45-
46-
type Test struct {
47-
String string `mold:"set"`
48-
}
49-
50-
var tt Test
51-
52-
err := tform.Struct(context.Background(), &tt)
53-
if err != nil {
54-
log.Fatal(err)
55-
}
56-
fmt.Printf("%+v\n", tt)
57-
58-
var myString string
59-
err = tform.Field(context.Background(), &myString, "set")
60-
if err != nil {
61-
log.Fatal(err)
62-
}
63-
fmt.Println(myString)
64-
}
65-
66-
func transformMyData(ctx context.Context, t *mold.Transformer, value reflect.Value, param string) error {
67-
value.SetString("test")
68-
return nil
69-
}
70-
```
71-
72-
Full example
73-
-----
74-
```go
75-
package main
76-
77-
import (
78-
"context"
79-
"fmt"
80-
"log"
81-
"net/url"
82-
83-
"github.com/go-playground/form"
84-
85-
"github.com/go-playground/mold/v3/modifiers"
86-
"github.com/go-playground/mold/v3/scrubbers"
87-
88-
"gopkg.in/go-playground/validator.v9"
89-
)
90-
91-
// This example is centered around a form post, but doesn't have to be
92-
// just trying to give a well rounded real life example.
93-
94-
// <form method="POST">
95-
// <input type="text" name="Name" value="joeybloggs"/>
96-
// <input type="text" name="Age" value="3"/>
97-
// <input type="text" name="Gender" value="Male"/>
98-
// <input type="text" name="Address[0].Name" value="26 Here Blvd."/>
99-
// <input type="text" name="Address[0].Phone" value="9(999)999-9999"/>
100-
// <input type="text" name="Address[1].Name" value="26 There Blvd."/>
101-
// <input type="text" name="Address[1].Phone" value="1(111)111-1111"/>
102-
// <input type="text" name="active" value="true"/>
103-
// <input type="submit"/>
104-
// </form>
105-
106-
var (
107-
conform = modifiers.New()
108-
scrub = scrubbers.New()
109-
validate = validator.New()
110-
decoder = form.NewDecoder()
111-
)
112-
113-
// Address contains address information
114-
type Address struct {
115-
Name string `mod:"trim" validate:"required"`
116-
Phone string `mod:"trim" validate:"required"`
117-
}
118-
119-
// User contains user information
120-
type User struct {
121-
Name string `mod:"trim" validate:"required" scrub:"name"`
122-
Age uint8 ` validate:"required,gt=0,lt=130"`
123-
Gender string ` validate:"required"`
124-
Email string `mod:"trim" validate:"required,email" scrub:"emails"`
125-
Address []Address ` validate:"required,dive"`
126-
Active bool `form:"active"`
127-
}
128-
129-
func main() {
130-
// this simulates the results of http.Request's ParseForm() function
131-
values := parseForm()
132-
133-
var user User
134-
135-
// must pass a pointer
136-
err := decoder.Decode(&user, values)
137-
if err != nil {
138-
log.Panic(err)
139-
}
140-
fmt.Printf("Decoded:%+v\n\n", user)
141-
142-
// great not lets conform our values, after all a human input the data
143-
// nobody's perfect
144-
err = conform.Struct(context.Background(), &user)
145-
if err != nil {
146-
log.Panic(err)
147-
}
148-
fmt.Printf("Conformed:%+v\n\n", user)
149-
150-
// that's better all those extra spaces are gone
151-
// let's validate the data
152-
err = validate.Struct(user)
153-
if err != nil {
154-
log.Panic(err)
155-
}
156-
157-
// ok now we know our data is good, let's do something with it like:
158-
// save to database
159-
// process request
160-
// etc....
161-
162-
// ok now I'm done working with my data
163-
// let's log or store it somewhere
164-
// oh wait a minute, we have some sensitive PII data
165-
// let's make sure that's de-identified first
166-
err = scrub.Struct(context.Background(), &user)
167-
if err != nil {
168-
log.Panic(err)
169-
}
170-
fmt.Printf("Scrubbed:%+v\n\n", user)
171-
}
172-
173-
// this simulates the results of http.Request's ParseForm() function
174-
func parseForm() url.Values {
175-
return url.Values{
176-
"Name": []string{" joeybloggs "},
177-
"Age": []string{"3"},
178-
"Gender": []string{"Male"},
179-
"Email": []string{"[email protected] "},
180-
"Address[0].Name": []string{"26 Here Blvd."},
181-
"Address[0].Phone": []string{"9(999)999-9999"},
182-
"Address[1].Name": []string{"26 There Blvd."},
183-
"Address[1].Phone": []string{"1(111)111-1111"},
184-
"active": []string{"true"},
185-
}
186-
}
187-
```
18868

18969
Special Information
19070
-------------------

_examples/full/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import (
77
"net/url"
88

99
"github.com/go-playground/form/v4"
10-
11-
"github.com/go-playground/mold/v3/modifiers"
12-
"github.com/go-playground/mold/v3/scrubbers"
13-
10+
"github.com/go-playground/mold/v4/modifiers"
11+
"github.com/go-playground/mold/v4/scrubbers"
1412
"github.com/go-playground/validator/v10"
1513
)
1614

_examples/mold/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"reflect"
87

9-
"github.com/go-playground/mold/v3"
8+
"github.com/go-playground/mold/v4"
109
)
1110

1211
var tform *mold.Transformer
@@ -35,7 +34,7 @@ func main() {
3534
fmt.Println(myString)
3635
}
3736

38-
func transformMyData(ctx context.Context, t *mold.Transformer, value reflect.Value, param string) error {
39-
value.SetString("test")
37+
func transformMyData(ctx context.Context, fl mold.FieldLevel) error {
38+
fl.Field().SetString("test")
4039
return nil
4140
}

field_level.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package mold
2+
3+
import "reflect"
4+
5+
// FieldLevel represents the interface for field level modifier function
6+
type FieldLevel interface {
7+
// Transformer represents a subset of the current *Transformer that is executing the current transformation.
8+
Transformer() Transform
9+
10+
//
11+
// Parent returns the top level parent of the current value return by Field()
12+
//
13+
// This is used primarily for having the ability to nil out pointer type values.
14+
//
15+
// NOTE: that is there are several layers of abstractions eg. interface{} of interface{} of interface{} this
16+
// function returns the first interface{}
17+
//
18+
Parent() reflect.Value
19+
20+
// Field returns the current field value being modified.
21+
Field() reflect.Value
22+
23+
// Param returns the param associated wth the given function modifier.
24+
Param() string
25+
}
26+
27+
var (
28+
_ FieldLevel = (*fieldLevel)(nil)
29+
)
30+
31+
type fieldLevel struct {
32+
transformer *Transformer
33+
parent reflect.Value
34+
current reflect.Value
35+
param string
36+
}
37+
38+
func (f fieldLevel) Transformer() Transform {
39+
return f.transformer
40+
}
41+
42+
func (f fieldLevel) Parent() reflect.Value {
43+
return f.parent
44+
}
45+
46+
func (f fieldLevel) Field() reflect.Value {
47+
return f.current
48+
}
49+
50+
func (f fieldLevel) Param() string {
51+
return f.param
52+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/go-playground/mold/v3
1+
module github.com/go-playground/mold/v4
22

33
go 1.15
44

modifiers/modifiers.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package modifiers
22

33
import (
4-
"github.com/go-playground/mold/v3"
4+
"github.com/go-playground/mold/v4"
55
)
66

77
// New returns a modifier with defaults registered
88
func New() *mold.Transformer {
99
mod := mold.New()
1010
mod.SetTagName("mod")
11-
mod.Register("trim", TrimSpace)
12-
mod.Register("ltrim", TrimLeft)
13-
mod.Register("rtrim", TrimRight)
14-
mod.Register("tprefix", TrimPrefix)
15-
mod.Register("tsuffix", TrimSuffix)
16-
mod.Register("lcase", ToLower)
17-
mod.Register("ucase", ToUpper)
18-
mod.Register("snake", SnakeCase)
19-
mod.Register("title", TitleCase)
20-
mod.Register("name", NameCase)
21-
mod.Register("ucfirst", UppercaseFirstCharacterCase)
22-
mod.Register("strip_alpha", StripAlphaCase)
23-
mod.Register("strip_num", StripNumCase)
24-
mod.Register("strip_num_unicode", StripNumUnicodeCase)
25-
mod.Register("strip_alpha_unicode", StripAlphaUnicodeCase)
26-
mod.Register("camel", CamelCase)
27-
mod.Register("default", Default)
11+
mod.Register("trim", trimSpace)
12+
mod.Register("ltrim", trimLeft)
13+
mod.Register("rtrim", trimRight)
14+
mod.Register("tprefix", trimPrefix)
15+
mod.Register("tsuffix", trimSuffix)
16+
mod.Register("lcase", toLower)
17+
mod.Register("ucase", toUpper)
18+
mod.Register("snake", snakeCase)
19+
mod.Register("title", titleCase)
20+
mod.Register("name", nameCase)
21+
mod.Register("ucfirst", uppercaseFirstCharacterCase)
22+
mod.Register("strip_alpha", stripAlphaCase)
23+
mod.Register("strip_num", stripNumCase)
24+
mod.Register("strip_num_unicode", stripNumUnicodeCase)
25+
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
26+
mod.Register("camel", camelCase)
27+
mod.Register("default", defaultValue)
2828
return mod
2929
}

0 commit comments

Comments
 (0)