bbgo: add doc comment for parseStructAndInject

This commit is contained in:
c9s 2022-03-05 12:59:47 +08:00
parent bdcae5b763
commit c1ac738ca0
3 changed files with 27 additions and 15 deletions

View File

@ -584,7 +584,7 @@ func (environ *Environment) SyncSession(ctx context.Context, session *ExchangeSe
} }
func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error { func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSession, defaultSymbols ...string) error {
symbols, err := getSessionSymbols(session, defaultSymbols...) symbols, err := session.getSessionSymbols(defaultSymbols...)
if err != nil { if err != nil {
return err return err
} }
@ -594,17 +594,6 @@ func (environ *Environment) syncSession(ctx context.Context, session *ExchangeSe
return environ.SyncService.SyncSessionSymbols(ctx, session.Exchange, environ.syncStartTime, symbols...) return environ.SyncService.SyncSessionSymbols(ctx, session.Exchange, environ.syncStartTime, symbols...)
} }
func getSessionSymbols(session *ExchangeSession, defaultSymbols ...string) ([]string, error) {
if session.IsolatedMargin {
return []string{session.IsolatedMarginSymbol}, nil
}
if len(defaultSymbols) > 0 {
return defaultSymbols, nil
}
return session.FindPossibleSymbols()
}
func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) error { func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) error {
environ.Notifiability = Notifiability{ environ.Notifiability = Notifiability{
@ -654,6 +643,10 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro
return nil return nil
} }
// getAuthStoreID returns the authentication store id
// if telegram bot token is defined, the bot id will be used.
// if not, env var $USER will be used.
// if both are not defined, a default "default" will be used.
func getAuthStoreID() string { func getAuthStoreID() string {
telegramBotToken := viper.GetString("telegram-bot-token") telegramBotToken := viper.GetString("telegram-bot-token")
if len(telegramBotToken) > 0 { if len(telegramBotToken) > 0 {
@ -924,3 +917,15 @@ And then enter your token
`, token) `, token)
} }
func (session *ExchangeSession) getSessionSymbols(defaultSymbols ...string) ([]string, error) {
if session.IsolatedMargin {
return []string{session.IsolatedMarginSymbol}, nil
}
if len(defaultSymbols) > 0 {
return defaultSymbols, nil
}
return session.FindPossibleSymbols()
}

View File

@ -56,6 +56,9 @@ func injectField(rs reflect.Value, fieldName string, obj interface{}, pointerOnl
return nil return nil
} }
// parseStructAndInject parses the struct fields and injects the objects into the corresponding fields by its type.
// if the given object is a reference of an object, the type of the target field MUST BE a pointer field.
// if the given object is a struct value, the type of the target field CAN BE a pointer field or a struct value field.
func parseStructAndInject(f interface{}, objects ...interface{}) error { func parseStructAndInject(f interface{}, objects ...interface{}) error {
sv := reflect.ValueOf(f) sv := reflect.ValueOf(f)
st := reflect.TypeOf(f) st := reflect.TypeOf(f)
@ -95,7 +98,13 @@ func parseStructAndInject(f interface{}, objects ...interface{}) error {
if !fv.CanSet() { if !fv.CanSet() {
return fmt.Errorf("field %v of %s can not be set to %s, make sure it is an exported field", fv, sv.Type(), ot) return fmt.Errorf("field %v of %s can not be set to %s, make sure it is an exported field", fv, sv.Type(), ot)
} }
fv.Set(reflect.ValueOf(obj))
if k == reflect.Ptr && ot.Kind() == reflect.Struct {
fv.Set(reflect.ValueOf(obj).Addr())
} else {
fv.Set(reflect.ValueOf(obj))
}
} }
} }

View File

@ -245,8 +245,6 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si
return fmt.Errorf("marketDataStore of symbol %s not found", symbol) return fmt.Errorf("marketDataStore of symbol %s not found", symbol)
} }
if err := parseStructAndInject(strategy, if err := parseStructAndInject(strategy,
market, market,
indicatorSet, indicatorSet,