Skip to content

Commit cb68049

Browse files
Merge pull request #1 from LeandroSNunes/ln-sanitize
sanitize string for match_with operator
2 parents 268ef8a + 029afb6 commit cb68049

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/enjoei/pkg
22

33
go 1.13
4+
5+
require golang.org/x/text v0.3.2

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
2+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
3+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

querybuilder/rule.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Rule struct {
1414
Type string
1515
Input string
1616
Operator string
17+
Sanitize bool
1718
Value interface{}
1819
}
1920

@@ -77,7 +78,13 @@ func (r *Rule) getInputValue(dataset map[string]interface{}) interface{} {
7778
}
7879
}
7980

80-
return r.parseValue(result)
81+
iv := r.parseValue(result)
82+
if r.Sanitize && r.Type == "string" {
83+
v := iv.(string)
84+
return sanitize(&v)
85+
}
86+
87+
return iv
8188
}
8289

8390
func (r *Rule) parseValue(v interface{}) interface{} {

querybuilder/rule_group.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,20 @@ func (rg *RuleGroup) getChecker(rule map[string]interface{}) Checker {
4848
return &RuleGroup{Condition: rule["condition"], Rules: rule["rules"]}
4949
}
5050

51-
return &Rule{
51+
r := &Rule{
5252
ID: rule["id"].(string),
5353
Field: rule["field"].(string),
5454
Type: rule["type"].(string),
5555
Input: rule["input"].(string),
5656
Operator: rule["operator"].(string),
5757
Value: rule["value"],
5858
}
59+
60+
if _, ok := rule["sanitize"]; ok {
61+
r.Sanitize = rule["sanitize"].(bool)
62+
}
63+
64+
return r
5965
}
6066

6167
func (rg *RuleGroup) evaluateRules(res chan<- bool, rules []interface{}, dataset map[string]interface{}) {

querybuilder/rule_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var ruleInputs = []struct {
1919
{&Rule{ID: "string07", Field: "string", Type: "string", Input: "text", Operator: "greater_or_equal", Value: "my text for tests"}, true},
2020
{&Rule{ID: "string08", Field: "string_empty", Type: "string", Input: "text", Operator: "is_empty", Value: "a"}, true},
2121
{&Rule{ID: "string09", Field: "string", Type: "string", Input: "text", Operator: "match_with", Value: `/text\sfor/`}, true},
22+
{&Rule{ID: "string10", Field: "string", Type: "string", Input: "text", Operator: "match_with", Sanitize: true, Value: `/textfor/`}, true},
2223
{&Rule{ID: "double01", Field: "double", Type: "double", Input: "text", Operator: "between", Value: []interface{}{1.0, 2.0}}, true},
2324
{&Rule{ID: "double02", Field: "double", Type: "double", Input: "text", Operator: "equal", Value: 1.2}, true},
2425
{&Rule{ID: "double03", Field: "double", Type: "double", Input: "text", Operator: "greater", Value: 1.3}, false},

querybuilder/sanitize.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package querybuilder
2+
3+
import (
4+
"golang.org/x/text/runes"
5+
"golang.org/x/text/transform"
6+
"golang.org/x/text/unicode/norm"
7+
"regexp"
8+
"unicode"
9+
)
10+
11+
const NoSymbolsPattern = "[^a-zA-Z0-9]+"
12+
13+
func sanitize(s *string) string {
14+
output := removeAccents(s)
15+
return *removeSymbols(output)
16+
}
17+
18+
func removeAccents(s *string) *string {
19+
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
20+
output, _, err := transform.String(t, *s)
21+
if err != nil {
22+
return s
23+
}
24+
return &output
25+
}
26+
27+
func removeSymbols(s *string) *string {
28+
reg, err := regexp.Compile(NoSymbolsPattern)
29+
if err != nil {
30+
return s
31+
}
32+
processedString := reg.ReplaceAllString(*s, "")
33+
34+
return &processedString
35+
}

querybuilder/sanitize_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package querybuilder
2+
3+
import "testing"
4+
5+
func TestRemoveAccents(t *testing.T) {
6+
aText := "ßàáâãäåæçèéêëìíîïðłñńòóôõōöøśùúûūüýþÿżœ"
7+
rText := "ßaaaaaaæceeeeiiiiðłnnooooooøsuuuuuyþyzœ"
8+
9+
if text := removeAccents(&aText); *text != rText {
10+
t.Errorf("removeAccents(%s) = %s -- want: %s", aText, *text, rText)
11+
}
12+
}
13+
14+
func TestRemoveSymbols(t *testing.T) {
15+
aText := "a.b,c!?d:(e)'\"f-_g"
16+
rText := "abcdefg"
17+
18+
if text := removeSymbols(&aText); *text != rText {
19+
t.Errorf("removeSymbols(%s) = %s -- want: %s", aText, *text, rText)
20+
}
21+
}
22+
23+
func TestSanitize(t *testing.T) {
24+
aText := "ßàáâãäåæçèéêëìíîïðłñńòóôõōöøśùúûūüýþÿżœa.b,c!?d:(e)'\"f-_g"
25+
rText := "aaaaaaceeeeiiiinnoooooosuuuuuyyzabcdefg"
26+
27+
if text := sanitize(&aText); text != rText {
28+
t.Errorf("removeSymbols(%s) = %s -- want: %s", aText, text, rText)
29+
}
30+
}

0 commit comments

Comments
 (0)