58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
type FilterIn struct {
|
|
Key string
|
|
Values []interface{}
|
|
}
|
|
|
|
func newFilterIn(key string, values interface{}) FilterIn {
|
|
fi := FilterIn{Key: key}
|
|
switch values.(type) {
|
|
case []interface{}:
|
|
fi.Values = values.([]interface{})
|
|
if len(fi.Values) == 0 {
|
|
return newFilterIn(key, nil)
|
|
}
|
|
case []string:
|
|
value := values.([]string)
|
|
fi.Values = make([]interface{}, len(value))
|
|
for i := range value {
|
|
fi.Values[i] = value[i]
|
|
}
|
|
if len(fi.Values) == 0 {
|
|
return newFilterIn(key, nil)
|
|
}
|
|
case []int:
|
|
value := values.([]int)
|
|
fi.Values = make([]interface{}, len(value))
|
|
for i := range value {
|
|
fi.Values[i] = value[i]
|
|
}
|
|
if len(fi.Values) == 0 {
|
|
return newFilterIn(key, nil)
|
|
}
|
|
case nil:
|
|
fi.Key = ""
|
|
default:
|
|
panic(fmt.Sprintf("cannot convert values to filter in: %T", values))
|
|
}
|
|
return fi
|
|
}
|
|
|
|
func (fi FilterIn) MarshalBSON() ([]byte, error) {
|
|
if len(fi.Key) == 0 {
|
|
return bson.Marshal(map[string]interface{}{})
|
|
}
|
|
return bson.Marshal(map[string]map[string][]interface{}{
|
|
fi.Key: map[string][]interface{}{
|
|
"$in": fi.Values,
|
|
},
|
|
})
|
|
}
|