better loggin, handle ModelInterface with values if set
This commit is contained in:
parent
64a4178975
commit
62b7717da7
20
crud.go
20
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
|
mSlice := reflect.Indirect(reflect.ValueOf(results)) // results is *[]ModelInterface, so we need to indirect
|
||||||
m := reflect.New(reflect.TypeOf(
|
var m ModelInterface
|
||||||
reflect.Indirect(reflect.ValueOf(results)).Interface(), // get indirection of slice pointer
|
if mSlice.Len() > 0 {
|
||||||
).Elem()).Interface().(ModelInterface) // it must be a ModelInterface here
|
// 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))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ type ModelInterface interface {
|
|||||||
// Model is model with default fields
|
// Model is model with default fields
|
||||||
type Model struct {
|
type Model struct {
|
||||||
ID *bson.ObjectId `json:"id,omitempty" bson:"_id" mapstructure:"id"`
|
ID *bson.ObjectId `json:"id,omitempty" bson:"_id" mapstructure:"id"`
|
||||||
InsertTime *time.Time `json:"insertTime,omitempty" bson:"insertTime,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" mapstructure:"Datum"`
|
UpdateTime *time.Time `json:"updateTime,omitempty" bson:"updateTime,omitempty" index:"single" mapstructure:"Datum"`
|
||||||
Ident []string `json:"ident,omitempty" bson:"-" mapstructure:"-"`
|
Ident []string `json:"ident,omitempty" bson:"-" mapstructure:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
setup.go
9
setup.go
@ -10,7 +10,8 @@ import (
|
|||||||
|
|
||||||
// EnsureIndex ensured mongodb index reflecting model struct index tag
|
// EnsureIndex ensured mongodb index reflecting model struct index tag
|
||||||
func EnsureIndex(db *mgo.Database, m ModelInterface) error {
|
func EnsureIndex(db *mgo.Database, m ModelInterface) error {
|
||||||
col := db.C(GetCollectionName(m))
|
colName := GetCollectionName(m)
|
||||||
|
col := db.C(colName)
|
||||||
|
|
||||||
mType := reflect.TypeOf(m)
|
mType := reflect.TypeOf(m)
|
||||||
|
|
||||||
@ -56,13 +57,13 @@ func EnsureIndex(db *mgo.Database, m ModelInterface) error {
|
|||||||
case indexEl == "text":
|
case indexEl == "text":
|
||||||
textFields = append(textFields, "$text:"+fieldbase+bsonField)
|
textFields = append(textFields, "$text:"+fieldbase+bsonField)
|
||||||
default:
|
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 {
|
if len(index.Key) > 0 {
|
||||||
// fmt.Println(bsonField, index)
|
// 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)
|
err := col.EnsureIndex(index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -92,7 +93,7 @@ func EnsureIndex(db *mgo.Database, m ModelInterface) error {
|
|||||||
|
|
||||||
if len(textFields) > 0 {
|
if len(textFields) > 0 {
|
||||||
// fmt.Println("$text", textFields)
|
// 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{
|
err := col.EnsureIndex(mgo.Index{
|
||||||
Name: "textindex",
|
Name: "textindex",
|
||||||
Key: textFields,
|
Key: textFields,
|
||||||
|
Loading…
Reference in New Issue
Block a user