Fix escaped RE to prevent delete .*
parent
ee4f23a9f4
commit
a1d59a0248
|
|
@ -2,6 +2,7 @@ package operator
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
|
@ -25,6 +26,10 @@ func (cis CaseInsensitives) MarshalBSON() ([]byte, error) {
|
|||
values := cis.Values
|
||||
if len(cis.Values) == 0 {
|
||||
values = []string{".*"}
|
||||
} else {
|
||||
for i := range values {
|
||||
values[i] = escapeRegex(values[i])
|
||||
}
|
||||
}
|
||||
ci := CaseInsensitive{
|
||||
Key: cis.Key,
|
||||
|
|
@ -42,6 +47,8 @@ func (ci CaseInsensitive) MarshalBSON() ([]byte, error) {
|
|||
value := ci.Value
|
||||
if value == "" {
|
||||
value = "^$"
|
||||
} else {
|
||||
value = escapeRegex(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)
|
||||
}
|
||||
|
||||
func escapeRegex(s string) string {
|
||||
re := regexp.MustCompile(`[^a-zA-Z0-9]`)
|
||||
s = re.ReplaceAllString(s, `.`)
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue