Fix escaped RE to prevent delete .*

master
breel 2020-07-25 19:05:44 -06:00
parent ee4f23a9f4
commit a1d59a0248
1 changed files with 13 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package operator
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -25,6 +26,10 @@ func (cis CaseInsensitives) MarshalBSON() ([]byte, error) {
values := cis.Values values := cis.Values
if len(cis.Values) == 0 { if len(cis.Values) == 0 {
values = []string{".*"} values = []string{".*"}
} else {
for i := range values {
values[i] = escapeRegex(values[i])
}
} }
ci := CaseInsensitive{ ci := CaseInsensitive{
Key: cis.Key, Key: cis.Key,
@ -42,6 +47,8 @@ func (ci CaseInsensitive) MarshalBSON() ([]byte, error) {
value := ci.Value value := ci.Value
if value == "" { if value == "" {
value = "^$" value = "^$"
} else {
value = escapeRegex(value)
} }
return bson.Marshal(Regex{Key: ci.Key, Value: "(?i)" + ci.Value}) return bson.Marshal(Regex{Key: ci.Key, Value: "(?i)" + ci.Value})
} }
@ -100,3 +107,9 @@ func filterMarshal(op, key string, value interface{}) ([]byte, error) {
} }
return bson.Marshal(m) return bson.Marshal(m)
} }
func escapeRegex(s string) string {
re := regexp.MustCompile(`[^a-zA-Z0-9]`)
s = re.ReplaceAllString(s, `.`)
return s
}