bbgo: add ExchangeSession param to the subscribe method

This commit is contained in:
c9s 2022-06-29 15:16:56 +08:00
parent cb1c5634a2
commit 84083f56b7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 13 additions and 3 deletions

View File

@ -14,7 +14,7 @@ type ExitMethod struct {
CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"` CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"`
} }
func (m *ExitMethod) Subscribe() { func (m *ExitMethod) Subscribe(session *ExchangeSession) {
// TODO: pull out this implementation as a simple function to reflect.go // TODO: pull out this implementation as a simple function to reflect.go
rv := reflect.ValueOf(m) rv := reflect.ValueOf(m)
rt := reflect.TypeOf(m) rt := reflect.TypeOf(m)
@ -22,11 +22,13 @@ func (m *ExitMethod) Subscribe() {
rv = rv.Elem() rv = rv.Elem()
rt = rt.Elem() rt = rt.Elem()
infType := reflect.TypeOf((*types.Subscriber)(nil)).Elem() infType := reflect.TypeOf((*types.Subscriber)(nil)).Elem()
argValues := toReflectValues(session)
for i := 0; i < rt.NumField(); i++ { for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i) fieldType := rt.Field(i)
if fieldType.Type.Implements(infType) { if fieldType.Type.Implements(infType) {
method := rv.Field(i).MethodByName("Subscribe") method := rv.Field(i).MethodByName("Subscribe")
method.Call(nil) method.Call(argValues)
} }
} }
} }

View File

@ -4,5 +4,5 @@ import "testing"
func TestExitMethod(t *testing.T) { func TestExitMethod(t *testing.T) {
em := &ExitMethod{} em := &ExitMethod{}
em.Subscribe() em.Subscribe(&ExchangeSession{})
} }

View File

@ -110,3 +110,11 @@ func newTypeValueInterface(typ reflect.Type) interface{} {
dst := reflect.New(typ) dst := reflect.New(typ)
return dst.Interface() return dst.Interface()
} }
func toReflectValues(args ...interface{}) (values []reflect.Value) {
for _, arg := range args {
values = append(values, reflect.ValueOf(arg))
}
return values
}