Skip to content

Commit

Permalink
Merge pull request #89 from pinguo-yangbing/master
Browse files Browse the repository at this point in the history
adapter support Prepare
  • Loading branch information
pinguo-yangbing authored Oct 30, 2020
2 parents f0e4180 + 1b664be commit ac06c64
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 72 deletions.
10 changes: 6 additions & 4 deletions adapter/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 13 additions & 10 deletions adapter/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
21 changes: 12 additions & 9 deletions adapter/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ 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 {
pgo2.Object
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
}
Expand Down
21 changes: 12 additions & 9 deletions adapter/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
23 changes: 13 additions & 10 deletions adapter/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
25 changes: 15 additions & 10 deletions adapter/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions adapter/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions adapter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
13 changes: 8 additions & 5 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 1 addition & 3 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ac06c64

Please sign in to comment.