2020-10-27 12:42:48 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasField(rs reflect.Value, fieldName string) bool {
|
|
|
|
field := rs.FieldByName(fieldName)
|
|
|
|
return field.IsValid()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-28 09:44:37 +00:00
|
|
|
logrus.Infof("found %s in %s, injecting %T...", fieldName, rs.Type(), obj)
|
2020-10-27 12:42:48 +00:00
|
|
|
|
|
|
|
if !field.CanSet() {
|
2020-10-28 09:44:37 +00:00
|
|
|
return errors.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() {
|
|
|
|
return errors.Errorf("field type mismatches: %s != %s", field.Type(), rv.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return errors.Errorf("field %s %s does not allow value assignment (pointer type only)", field.Type(), rv.Type())
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:42:48 +00:00
|
|
|
field.Set(rv.Elem())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|