2020-10-27 12:42:48 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
2020-11-09 08:34:35 +00:00
|
|
|
"fmt"
|
2020-10-27 12:42:48 +00:00
|
|
|
"reflect"
|
2022-03-04 19:20:20 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-27 12:42:48 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {
|
|
|
|
field := rs.FieldByName("Symbol")
|
|
|
|
if !field.IsValid() {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Kind() != reflect.String {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return field.String(), true
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:43:17 +00:00
|
|
|
func hasField(rs reflect.Value, fieldName string) (field reflect.Value, ok bool) {
|
|
|
|
field = rs.FieldByName(fieldName)
|
|
|
|
return field, field.IsValid()
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:49:06 +00:00
|
|
|
func injectField(rs reflect.Value, fieldName string, obj interface{}, pointerOnly bool) error {
|
2020-10-27 12:42:48 +00:00
|
|
|
field := rs.FieldByName(fieldName)
|
|
|
|
if !field.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !field.CanSet() {
|
2020-11-09 08:34:35 +00:00
|
|
|
return fmt.Errorf("field %s of %s can not be set", fieldName, rs.Type())
|
2020-10-27 12:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(obj)
|
|
|
|
if field.Kind() == reflect.Ptr {
|
|
|
|
if field.Type() != rv.Type() {
|
2020-11-09 08:34:35 +00:00
|
|
|
return fmt.Errorf("field type mismatches: %s != %s", field.Type(), rv.Type())
|
2020-10-27 12:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
field.Set(rv)
|
|
|
|
} else if field.Kind() == reflect.Interface {
|
|
|
|
field.Set(rv)
|
|
|
|
} else {
|
|
|
|
// set as value
|
2020-10-28 23:49:06 +00:00
|
|
|
if pointerOnly {
|
2020-11-09 08:34:35 +00:00
|
|
|
return fmt.Errorf("field %s %s does not allow value assignment (pointer type only)", field.Type(), rv.Type())
|
2020-10-28 23:49:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:42:48 +00:00
|
|
|
field.Set(rv.Elem())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-04 19:20:20 +00:00
|
|
|
|
2022-03-05 04:59:47 +00:00
|
|
|
// 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.
|
2022-03-04 19:20:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:39:39 +00:00
|
|
|
// solve the reference
|
2022-03-04 19:20:20 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-03-05 04:59:47 +00:00
|
|
|
|
|
|
|
if k == reflect.Ptr && ot.Kind() == reflect.Struct {
|
|
|
|
fv.Set(reflect.ValueOf(obj).Addr())
|
|
|
|
} else {
|
|
|
|
fv.Set(reflect.ValueOf(obj))
|
|
|
|
}
|
|
|
|
|
2022-03-04 19:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|