11Package 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
1717Use 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
18969Special Information
19070-------------------
0 commit comments