diff --git a/adapter/db.go b/adapter/db.go index 5ac9385..d78ef5e 100644 --- a/adapter/db.go +++ b/adapter/db.go @@ -50,17 +50,19 @@ func NewDb(componentId ...string) *Db { // NewDbPool of db Client from pool, add context support. // usage: db := this.GetObjPool(adapter.DbClass, adapter.NewDbPool).(adapter.IDb)/(*adapter.Db) +// It is recommended to use : db := this.GetObjBox(adapter.DbClass).(adapter.IDb)/(*adapter.Db) func NewDbPool(iObj iface.IObject, componentId ...interface{}) iface.IObject { + return iObj +} + +// GetObjPool, GetObjBox fetch is performed automatically +func (d *Db) Prepare(componentId ...interface{}){ id := DefaultDbId if len(componentId) > 0 { id = componentId[0].(string) } - d := iObj.(*Db) - d.client = pgo2.App().Component(id, db.New).(*db.Client) - - return d } func (d *Db) SetMaster(v bool) { diff --git a/adapter/http.go b/adapter/http.go index adaa0b0..11d85fb 100644 --- a/adapter/http.go +++ b/adapter/http.go @@ -37,18 +37,10 @@ func NewHttp(componentId ...string) *Http { // Http of Http Client from pool, add context support. // usage: http := this.GetObjPool(adapter.HttpClass, adapter.NewHttpPool).(adapter.IHttp)/(*adapter.Http) +// It is recommended to use : http := this.GetObjBox(adapter.HttpClass).(adapter.IHttp)/(*adapter.Http) func NewHttpPool(iObj iface.IObject, componentId ...interface{}) iface.IObject { - id := DefaultHttpId - if len(componentId) > 0 { - id = componentId[0].(string) - } - - h := iObj.(*Http) - h.client = pgo2.App().Component(id, phttp.New).(*phttp.Client) - h.panicRecover = true - - return h + return iObj } type Http struct { @@ -57,6 +49,17 @@ type Http struct { panicRecover bool } +// GetObjPool, GetObjBox fetch is performed automatically +func (h *Http) Prepare(componentId ...interface{}) { + id := DefaultHttpId + if len(componentId) > 0 { + id = componentId[0].(string) + } + + h.client = pgo2.App().Component(id, phttp.New).(*phttp.Client) + h.panicRecover = true +} + func (h *Http) SetPanicRecover(v bool) { h.panicRecover = v } diff --git a/adapter/maxmind.go b/adapter/maxmind.go index 86e1f29..d378900 100644 --- a/adapter/maxmind.go +++ b/adapter/maxmind.go @@ -31,17 +31,10 @@ func NewMaxMind(componentId ...string) *MaxMind { // NewMaxMindPool of MaxMind Client from pool, add context support. // usage: mmd := this.GetObjPool(adapter.MaxMindClass, adapter.NewMaxMindPool).(adapter.IMaxMind)/(*adapter.MaxMind) +// It is recommended to use :mmd := this.GetObjBox(adapter.MaxMindClass).(adapter.IMaxMind)/(*adapter.MaxMind) func NewMaxMindPool(iObj iface.IObject, componentId ...interface{}) iface.IObject { - id := DefaultMaxMindId - if len(componentId) > 0 { - id = componentId[0].(string) - } - m := iObj.(*MaxMind) - - m.client = pgo2.App().Component(id, maxmind.New).(*maxmind.Client) - - return m + return iObj } type MaxMind struct { @@ -49,6 +42,16 @@ type MaxMind struct { client *maxmind.Client } +// GetObjPool, GetObjBox fetch is performed automatically +func (m *MaxMind) Prepare(componentId ...interface{}) { + id := DefaultMaxMindId + if len(componentId) > 0 { + id = componentId[0].(string) + } + + m.client = pgo2.App().Component(id, maxmind.New).(*maxmind.Client) +} + func (m *MaxMind) GetClient() *maxmind.Client { return m.client } diff --git a/adapter/memcache.go b/adapter/memcache.go index 126cf80..1c71e68 100644 --- a/adapter/memcache.go +++ b/adapter/memcache.go @@ -33,17 +33,10 @@ func NewMemCache(componentId ...string) *MemCache { // NewMemCachePool of MemCache Client from pool, add context support. // usage: mc := this.GetObjPool(adapter.MemCacheClass, adapter.NewMemCachePool).(adapter.IMemCache)/(*adapter.MemCache) +// It is recommended to use :mc := this.GetObjBox(adapter.MemCacheClass).(adapter.IMemCache)/(*adapter.MemCache) func NewMemCachePool(iObj iface.IObject, componentId ...interface{}) iface.IObject { - id := DefaultMemCacheId - if len(componentId) > 0 { - id = componentId[0].(string) - } - m := iObj.(*MemCache) - - m.client = pgo2.App().Component(id, memcache.New).(*memcache.Client) - - return m + return iObj } type MemCache struct { @@ -52,6 +45,16 @@ type MemCache struct { panicRecover bool } +// GetObjPool, GetObjBox fetch is performed automatically +func (m *MemCache) Prepare(componentId ...interface{}) { + id := DefaultMemCacheId + if len(componentId) > 0 { + id = componentId[0].(string) + } + + m.client = pgo2.App().Component(id, memcache.New).(*memcache.Client) +} + func (m *MemCache) SetPanicRecover(v bool) { m.panicRecover = v } diff --git a/adapter/memory.go b/adapter/memory.go index 058368f..94dfdf1 100644 --- a/adapter/memory.go +++ b/adapter/memory.go @@ -35,18 +35,10 @@ func NewMemory(componentId ...string) *Memory { // NewMemoryPool of Memory Client from object pool, add context support. // usage: memory := this.GetObjPool(adapter.MemoryClass, adapter.NewMemoryPool).(adapter.IMemory)/(*adapter.Memory) +// It is recommended to use : memory := this.GetObjBox(adapter.MemoryClass).(adapter.IMemory)/(*adapter.Memory) func NewMemoryPool(iObj iface.IObject, componentId ...interface{}) iface.IObject { - id := DefaultMemoryId - if len(componentId) > 0 { - id = componentId[0].(string) - } - - m := iObj.(*Memory) - m.client = pgo2.App().Component(id, memory.New, map[string]interface{}{"logger":pgo2.GLogger()}).(*memory.Client) - m.panicRecover = true - - return m + return iObj } type Memory struct { @@ -55,6 +47,17 @@ type Memory struct { panicRecover bool } +// GetObjPool, GetObjBox fetch is performed automatically +func (m *Memory) Prepare(componentId ...interface{}){ + id := DefaultMemoryId + if len(componentId) > 0 { + id = componentId[0].(string) + } + + m.client = pgo2.App().Component(id, memory.New, map[string]interface{}{"logger":pgo2.GLogger()}).(*memory.Client) + m.panicRecover = true +} + func (m *Memory) SetPanicRecover(v bool) { m.panicRecover = v } diff --git a/adapter/mongo.go b/adapter/mongo.go index 1c446d6..42732d9 100644 --- a/adapter/mongo.go +++ b/adapter/mongo.go @@ -34,7 +34,22 @@ func NewMongo(db, coll string, componentId ...string) *Mongo { // NewMongoPool of Mongo Client from pool, add context support. // usage: mongo := this.GetObjectPool(adapter.MongoClass, adapter.NewMongoPool,db, coll)).(adapter.IMongo)/(*adapter.Mongo) +// It is recommended to use : mongo := this.GetObjectBox(adapter.MongoClass,db, coll)).(adapter.IMongo)/(*adapter.Mongo) func NewMongoPool(iObj iface.IObject, args ...interface{}) iface.IObject { + + return iObj + +} + +type Mongo struct { + pgo2.Object + client *mongo.Client + db string + coll string +} + +// GetObjPool, GetObjBox fetch is performed automatically +func (m *Mongo) Prepare(args ...interface{}) { if len(args) < 2 { panic("need db and coll") } @@ -54,21 +69,11 @@ func NewMongoPool(iObj iface.IObject, args ...interface{}) iface.IObject { panic("db and coll must string") } - m := iObj.(*Mongo) m.client = pgo2.App().Component(id, mongo.New).(*mongo.Client) m.db = db m.coll = coll - return m - -} - -type Mongo struct { - pgo2.Object - client *mongo.Client - db string - coll string } func (m *Mongo) GetClient() *mongo.Client { diff --git a/adapter/rabbitmq.go b/adapter/rabbitmq.go index 345a3c6..4de8f07 100644 --- a/adapter/rabbitmq.go +++ b/adapter/rabbitmq.go @@ -49,8 +49,15 @@ func NewRabbitMq(dftConfig ...string) *RabbitMq { // NewRabbitMqPool of RabbitMq Client from pool, add context support. // usage: rabbitMq := this.GetObjPool(adapter.RabbitMqClass,adapter.NewRabbitMqPool).(adapter.IRabbitMq)/(*adapter.RabbitMq) +// It is recommended to use : rabbitMq := this.GetObjBox(adapter.RabbitMqClass).(adapter.IRabbitMq)/(*adapter.RabbitMq) func NewRabbitMqPool(iObj iface.IObject, dftConfig ...interface{}) iface.IObject { + return iObj +} + +// GetObjPool, GetObjBox fetch is performed automatically +func (r *RabbitMq) Prepare(dftConfig ...interface{}) { + id := DefaultRabbitId l := len(dftConfig) if l > 0 { @@ -63,11 +70,9 @@ func NewRabbitMqPool(iObj iface.IObject, dftConfig ...interface{}) iface.IObject op["pass"] = dftConfig[2] } - r := iObj.(*RabbitMq) r.client = pgo2.App().Component(id, rabbitmq.New, op).(*rabbitmq.Client) r.panicRecover = true - return r } func (r *RabbitMq) SetPanicRecover(v bool) { diff --git a/adapter/redis.go b/adapter/redis.go index 735b071..33213b4 100644 --- a/adapter/redis.go +++ b/adapter/redis.go @@ -33,18 +33,10 @@ func NewRedis(componentId ...string) *Redis { // NewRedisPool of Redis Client from pool, add context support. // usage: redis := this.GetObjPool(adapter.RedisClass, adapter.NewRedisPool).(*adapter.Redis) +// It is recommended to use : redis := this.GetObjBox(adapter.RedisClass).(*adapter.Redis) func NewRedisPool(iObj iface.IObject, componentId ...interface{}) iface.IObject { - id := DefaultRedisId - if len(componentId) > 0 { - id = componentId[0].(string) - } - - c := iObj.(*Redis) - c.client = pgo2.App().Component(id, redis.New, map[string]interface{}{"logger":pgo2.GLogger()}).(*redis.Client) - c.panicRecover = true - - return c + return iObj } type Redis struct { @@ -53,6 +45,18 @@ type Redis struct { panicRecover bool } +// GetObjPool, GetObjBox fetch is performed automatically +func (r *Redis) Prepare(componentId ...interface{}) { + id := DefaultRedisId + if len(componentId) > 0 { + id = componentId[0].(string) + } + + r.client = pgo2.App().Component(id, redis.New, map[string]interface{}{"logger":pgo2.GLogger()}).(*redis.Client) + r.panicRecover = true + +} + func (r *Redis) SetPanicRecover(v bool) { r.panicRecover = v } diff --git a/container.go b/container.go index c6d9126..cca43b8 100644 --- a/container.go +++ b/container.go @@ -42,7 +42,7 @@ type Container struct { // Bind bind template object to class, // param i must be a pointer of struct. -func (c *Container) Bind(i interface{}) string{ +func (c *Container) Bind(i interface{}) string { iv := reflect.ValueOf(i) if iv.Kind() != reflect.Ptr { panic("Container: invalid type, need pointer") @@ -117,7 +117,7 @@ func (c *Container) GetType(name string) reflect.Type { } // getNew get new class object -func (c *Container) getNew(name string) reflect.Value{ +func (c *Container) getNew(name string) reflect.Value { item, ok := c.items[name] if !ok { panic("Container: class not found, " + name) @@ -151,9 +151,12 @@ func (c *Container) Get(name string, ctx iface.IContext, params ...interface{}) // call Prepare() if item.pmIdx != -1 { if im := rv.Method(item.pmIdx); im.IsValid() { - in := make([]reflect.Value, len(params)) - for k, arg := range params { - in[k] = reflect.ValueOf(arg) + in := make([]reflect.Value, 0) + if im.Type().NumIn() > 0 { + in = make([]reflect.Value, len(params)) + for k, arg := range params { + in[k] = reflect.ValueOf(arg) + } } im.Call(in) } diff --git a/object.go b/object.go index 10de848..9c4a532 100644 --- a/object.go +++ b/object.go @@ -52,11 +52,9 @@ func (o *Object) GetObjCtx(ctx iface.IContext, obj iface.IObject) iface.IObject // Recommended: Use GetObjBoxCtx instead. func (o *Object) GetObjPoolCtx(ctx iface.IContext, className string, funcName iface.IObjPoolFunc, params ...interface{}) iface.IObject { var obj iface.IObject + obj = App().GetObjPool(className, ctx, params...) if funcName != nil { - obj = App().GetObjPool(className, ctx) return funcName(obj, params...) - }else{ - obj = App().GetObjPool(className, ctx, params...) } return obj }