mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
bbgo: move inject function to injection.go
This commit is contained in:
parent
cd6b37ac3b
commit
a6053e0e59
|
@ -3,6 +3,8 @@ package bbgo
|
|||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {
|
||||
|
@ -53,3 +55,70 @@ func injectField(rs reflect.Value, fieldName string, obj interface{}, pointerOnl
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseStructAndInject(f interface{}, objects ...interface{}) error {
|
||||
sv := reflect.ValueOf(f)
|
||||
st := reflect.TypeOf(f)
|
||||
|
||||
if st.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("f needs to be a pointer of a struct, %s given", st)
|
||||
}
|
||||
|
||||
// solve the pointer
|
||||
st = st.Elem()
|
||||
sv = sv.Elem()
|
||||
|
||||
if st.Kind() != reflect.Struct {
|
||||
return fmt.Errorf("f needs to be a struct, %s given", st)
|
||||
}
|
||||
|
||||
for i := 0; i < sv.NumField(); i++ {
|
||||
fv := sv.Field(i)
|
||||
ft := fv.Type()
|
||||
|
||||
// skip unexported fields
|
||||
if !st.Field(i).IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch k := fv.Kind(); k {
|
||||
|
||||
case reflect.Ptr, reflect.Struct:
|
||||
for oi := 0; oi < len(objects); oi++ {
|
||||
obj := objects[oi]
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ot := reflect.TypeOf(obj)
|
||||
if ft.AssignableTo(ot) {
|
||||
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)
|
||||
}
|
||||
fv.Set(reflect.ValueOf(obj))
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Interface:
|
||||
for oi := 0; oi < len(objects); oi++ {
|
||||
obj := objects[oi]
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
objT := reflect.TypeOf(obj)
|
||||
logrus.Debugln(
|
||||
ft.PkgPath(),
|
||||
ft.Name(),
|
||||
objT, "implements", ft, "=", objT.Implements(ft),
|
||||
)
|
||||
|
||||
if objT.Implements(ft) {
|
||||
fv.Set(reflect.ValueOf(obj))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -388,69 +388,3 @@ func (trader *Trader) ReportPnL() *PnLReporterManager {
|
|||
return NewPnLReporter(&trader.environment.Notifiability)
|
||||
}
|
||||
|
||||
func parseStructAndInject(f interface{}, objects ...interface{}) error {
|
||||
sv := reflect.ValueOf(f)
|
||||
st := reflect.TypeOf(f)
|
||||
|
||||
if st.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("f needs to be a pointer of a struct, %s given", st)
|
||||
}
|
||||
|
||||
// solve the pointer
|
||||
st = st.Elem()
|
||||
sv = sv.Elem()
|
||||
|
||||
if st.Kind() != reflect.Struct {
|
||||
return fmt.Errorf("f needs to be a struct, %s given", st)
|
||||
}
|
||||
|
||||
for i := 0; i < sv.NumField(); i++ {
|
||||
fv := sv.Field(i)
|
||||
ft := fv.Type()
|
||||
|
||||
// skip unexported fields
|
||||
if !st.Field(i).IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch k := fv.Kind(); k {
|
||||
|
||||
case reflect.Ptr, reflect.Struct:
|
||||
for oi := 0; oi < len(objects); oi++ {
|
||||
obj := objects[oi]
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ot := reflect.TypeOf(obj)
|
||||
if ft.AssignableTo(ot) {
|
||||
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)
|
||||
}
|
||||
fv.Set(reflect.ValueOf(obj))
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Interface:
|
||||
for oi := 0; oi < len(objects); oi++ {
|
||||
obj := objects[oi]
|
||||
if obj == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
objT := reflect.TypeOf(obj)
|
||||
log.Debugln(
|
||||
ft.PkgPath(),
|
||||
ft.Name(),
|
||||
objT, "implements", ft, "=", objT.Implements(ft),
|
||||
)
|
||||
|
||||
if objT.Implements(ft) {
|
||||
fv.Set(reflect.ValueOf(obj))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user