Make names case insensitive for API

This commit is contained in:
breel
2020-07-25 13:58:02 -06:00
parent 34075cf19a
commit ee4f23a9f4
6 changed files with 75 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package operator
import (
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
)
@@ -15,6 +16,36 @@ func (re Regex) MarshalBSON() ([]byte, error) {
return filterMarshal("$regex", re.Key, re.Value)
}
type CaseInsensitives struct {
Key string
Values []string
}
func (cis CaseInsensitives) MarshalBSON() ([]byte, error) {
values := cis.Values
if len(cis.Values) == 0 {
values = []string{".*"}
}
ci := CaseInsensitive{
Key: cis.Key,
Value: fmt.Sprintf("^(%s)$", strings.Join(values, "|")),
}
return bson.Marshal(ci)
}
type CaseInsensitive struct {
Key string
Value string
}
func (ci CaseInsensitive) MarshalBSON() ([]byte, error) {
value := ci.Value
if value == "" {
value = "^$"
}
return bson.Marshal(Regex{Key: ci.Key, Value: "(?i)" + ci.Value})
}
type FilterIn struct {
Key string
Values []interface{}