fixed string to objectid in filter

This commit is contained in:
Sebastian Frank 2020-11-28 21:21:24 +01:00
parent ef51f2446b
commit 5fcd080a33

14
crud.go
View File

@ -54,12 +54,14 @@ type PipelineModifierFunction func(pipeline []bson.M) []bson.M
func convertIDValue(v reflect.Value) reflect.Value {
v = reflect.ValueOf(v.Interface())
vKind := v.Kind() // mapVal.Kind() does not work, no idea why ;(
spew.Dump(v.Interface())
spew.Dump(vKind)
//spew.Dump(v.Interface())
//spew.Dump(vKind)
switch vKind {
case reflect.String:
return reflect.ValueOf(bson.ObjectIdHex(v.Interface().(string)))
if hex, ok := v.Interface().(string); ok {
return reflect.ValueOf(bson.ObjectIdHex(hex))
}
case reflect.Map:
for _, key := range v.MapKeys() {
v.SetMapIndex(key, convertIDValue(v.MapIndex(key)))
@ -74,7 +76,7 @@ func convertIDValue(v reflect.Value) reflect.Value {
}
func idToObjectID(filter interface{}) {
spew.Dump(filter)
//spew.Dump(filter)
val := reflect.ValueOf(filter)
switch reflect.TypeOf(filter).Kind() {
case reflect.Slice:
@ -130,7 +132,7 @@ func idToObjectID(filter interface{}) {
}
// ReadCollection gets the filtered collection of the model
func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err *ErrorWithStack) {
func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) {
defer func() {
if r := recover(); r != nil {
err = &ErrorWithStack{Message: fmt.Sprintf("%v", r), StackTrace: string(debug.Stack())}
@ -234,7 +236,7 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select
if _err != nil {
return &ErrorWithStack{Message: _err.Error(), StackTrace: string(debug.Stack())}
}
return err
return nil
}
// ReadCollectionCount gets the count of elements in filtered collection