add Test_injectField

This commit is contained in:
c9s 2021-02-16 15:49:57 +08:00
parent 67a3c49081
commit 1c2646b0af
3 changed files with 70 additions and 41 deletions

View File

@ -3,8 +3,6 @@ package bbgo
import (
"fmt"
"reflect"
"github.com/sirupsen/logrus"
)
func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {
@ -31,8 +29,6 @@ func injectField(rs reflect.Value, fieldName string, obj interface{}, pointerOnl
return nil
}
logrus.Infof("found %s in %s, injecting %T...", fieldName, rs.Type(), obj)
if !field.CanSet() {
return fmt.Errorf("field %s of %s can not be set", fieldName, rs.Type())
}

View File

@ -0,0 +1,31 @@
package bbgo
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/service"
)
func Test_injectField(t *testing.T) {
type TT struct {
TradeService *service.TradeService
}
// only pointer object can be set.
var tt = &TT{}
// get the value of the pointer, or it can not be set.
var rv = reflect.ValueOf(tt).Elem()
_, ret := hasField(rv, "TradeService")
assert.True(t, ret)
ts := &service.TradeService{}
err := injectField(rv, "TradeService", ts, true)
assert.NoError(t, err)
}

View File

@ -249,52 +249,54 @@ func (trader *Trader) Run(ctx context.Context) error {
for _, strategy := range trader.crossExchangeStrategies {
rs := reflect.ValueOf(strategy)
if rs.Elem().Kind() == reflect.Struct {
// get the struct element
rs = rs.Elem()
if field, ok := hasField(rs, "Persistence"); ok {
if trader.environment.PersistenceServiceFacade == nil {
log.Warnf("strategy has Persistence field but persistence service is not defined")
if rs.Elem().Kind() != reflect.Struct {
continue
}
// get the struct element from the struct pointer
rs = rs.Elem()
if field, ok := hasField(rs, "Persistence"); ok {
if trader.environment.PersistenceServiceFacade == nil {
log.Warnf("strategy has Persistence field but persistence service is not defined")
} else {
log.Infof("found Persistence field, injecting...")
if field.IsNil() {
field.Set(reflect.ValueOf(&Persistence{
PersistenceSelector: &PersistenceSelector{
StoreID: "default",
Type: "memory",
},
Facade: trader.environment.PersistenceServiceFacade,
}))
} else {
log.Infof("found Persistence field, injecting...")
if field.IsNil() {
field.Set(reflect.ValueOf(&Persistence{
PersistenceSelector: &PersistenceSelector{
StoreID: "default",
Type: "memory",
},
Facade: trader.environment.PersistenceServiceFacade,
}))
} else {
elem := field.Elem()
if elem.Kind() != reflect.Struct {
return fmt.Errorf("the field Persistence is not a struct element")
}
elem := field.Elem()
if elem.Kind() != reflect.Struct {
return fmt.Errorf("the field Persistence is not a struct element")
}
if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil {
log.WithError(err).Errorf("strategy Persistence injection failed")
return err
}
if err := injectField(elem, "Facade", trader.environment.PersistenceServiceFacade, true); err != nil {
log.WithError(err).Errorf("strategy Persistence injection failed")
return err
}
}
}
}
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
log.WithError(err).Errorf("strategy Graceful injection failed")
return err
}
if err := injectField(rs, "Graceful", &trader.Graceful, true); err != nil {
log.WithError(err).Errorf("strategy Graceful injection failed")
return err
}
if err := injectField(rs, "Logger", &trader.logger, false); err != nil {
log.WithError(err).Errorf("strategy Logger injection failed")
return err
}
if err := injectField(rs, "Notifiability", &trader.environment.Notifiability, false); err != nil {
log.WithError(err).Errorf("strategy Notifiability injection failed")
return err
}
if err := injectField(rs, "Logger", &trader.logger, false); err != nil {
log.WithError(err).Errorf("strategy Logger injection failed")
return err
}
if err := injectField(rs, "Notifiability", &trader.environment.Notifiability, false); err != nil {
log.WithError(err).Errorf("strategy Notifiability injection failed")
return err
}
if err := strategy.CrossRun(ctx, router, trader.environment.sessions); err != nil {