From 62b7717da74eae7c248cdb3d519f93654cd07fda Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 28 Nov 2019 11:28:26 +0100 Subject: [PATCH] better loggin, handle ModelInterface with values if set --- crud.go | 20 ++++++++++++++++---- interface.go | 4 ++-- setup.go | 9 +++++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crud.go b/crud.go index 039bca5..2cb3afc 100644 --- a/crud.go +++ b/crud.go @@ -45,10 +45,22 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select } }() - // get pointer to model (element of slice in results) to get collection name - m := reflect.New(reflect.TypeOf( - reflect.Indirect(reflect.ValueOf(results)).Interface(), // get indirection of slice pointer - ).Elem()).Interface().(ModelInterface) // it must be a ModelInterface here + mSlice := reflect.Indirect(reflect.ValueOf(results)) // results is *[]ModelInterface, so we need to indirect + var m ModelInterface + if mSlice.Len() > 0 { + // use existing first element + m = mSlice.Index(0).Interface().(ModelInterface) + } else { + // create new element to get collection name + m = reflect.New(reflect.TypeOf(mSlice.Interface()).Elem()).Interface().(ModelInterface) + } + + /* + // get pointer to model (element of slice in results) to get collection name + m := reflect.New(reflect.TypeOf( + reflect.Indirect(reflect.ValueOf(results)).Interface(), // get indirection of slice pointer + ).Elem()).Interface().(ModelInterface) // it must be a ModelInterface here + */ c := db.C(GetCollectionName(m)) diff --git a/interface.go b/interface.go index ec8ddf3..161365d 100644 --- a/interface.go +++ b/interface.go @@ -21,8 +21,8 @@ type ModelInterface interface { // Model is model with default fields type Model struct { ID *bson.ObjectId `json:"id,omitempty" bson:"_id" mapstructure:"id"` - InsertTime *time.Time `json:"insertTime,omitempty" bson:"insertTime,omitempty" mapstructure:"Datum"` - UpdateTime *time.Time `json:"updateTime,omitempty" bson:"updateTime,omitempty" mapstructure:"Datum"` + InsertTime *time.Time `json:"insertTime,omitempty" bson:"insertTime,omitempty" index:"single" mapstructure:"Datum"` + UpdateTime *time.Time `json:"updateTime,omitempty" bson:"updateTime,omitempty" index:"single" mapstructure:"Datum"` Ident []string `json:"ident,omitempty" bson:"-" mapstructure:"-"` } diff --git a/setup.go b/setup.go index 2a58a4a..cb6f634 100644 --- a/setup.go +++ b/setup.go @@ -10,7 +10,8 @@ import ( // EnsureIndex ensured mongodb index reflecting model struct index tag func EnsureIndex(db *mgo.Database, m ModelInterface) error { - col := db.C(GetCollectionName(m)) + colName := GetCollectionName(m) + col := db.C(colName) mType := reflect.TypeOf(m) @@ -56,13 +57,13 @@ func EnsureIndex(db *mgo.Database, m ModelInterface) error { case indexEl == "text": textFields = append(textFields, "$text:"+fieldbase+bsonField) default: - return fmt.Errorf("invalid index tag for field %s in model %+v", bsonField, t) + return fmt.Errorf("invalid index tag on collection %s.%s for field %s%s in model %+v", db.Name, colName, fieldbase, bsonField, t) } } if len(index.Key) > 0 { // fmt.Println(bsonField, index) - fmt.Printf("ensure index on collection %s for field %s\n", GetCollectionName(m), bsonField) + fmt.Printf("ensure index on collection %s.%s for field %s%s\n", db.Name, colName, fieldbase, bsonField) err := col.EnsureIndex(index) if err != nil { return err @@ -92,7 +93,7 @@ func EnsureIndex(db *mgo.Database, m ModelInterface) error { if len(textFields) > 0 { // fmt.Println("$text", textFields) - fmt.Printf("ensure text index on collection %s for fields %v\n", GetCollectionName(m), textFields) + fmt.Printf("ensure text index on collection %s.%s for fields %v\n", db.Name, GetCollectionName(m), textFields) err := col.EnsureIndex(mgo.Index{ Name: "textindex", Key: textFields,