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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|