diff --git a/pkg/bbgo/exit.go b/pkg/bbgo/exit.go index 91c0d1b2f..1cb46e25d 100644 --- a/pkg/bbgo/exit.go +++ b/pkg/bbgo/exit.go @@ -1,8 +1,6 @@ package bbgo import ( - "reflect" - "github.com/pkg/errors" "github.com/c9s/bbgo/pkg/dynamic" @@ -32,21 +30,7 @@ type ExitMethod struct { // Inherit is used for inheriting properties from the given strategy struct // for example, some exit method requires the default interval and symbol name from the strategy param object func (m *ExitMethod) Inherit(parent interface{}) { - // we need to pass some information from the strategy configuration to the exit methods, like symbol, interval and window - rt := reflect.TypeOf(m).Elem() - rv := reflect.ValueOf(m).Elem() - for j := 0; j < rv.NumField(); j++ { - if !rt.Field(j).IsExported() { - continue - } - - fieldValue := rv.Field(j) - if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() { - continue - } - - dynamic.InheritStructValues(fieldValue.Interface(), parent) - } + dynamic.StructFieldsInherit(m, parent) } func (m *ExitMethod) Subscribe(session *ExchangeSession) { diff --git a/pkg/dynamic/merge.go b/pkg/dynamic/merge.go index 8e44bd333..406cd981f 100644 --- a/pkg/dynamic/merge.go +++ b/pkg/dynamic/merge.go @@ -2,6 +2,29 @@ package dynamic import "reflect" + + +// StructFieldsInherit is used for inheriting properties from the given strategy struct +// for example, some exit method requires the default interval and symbol name from the strategy param object +func StructFieldsInherit(m interface{}, parent interface{}) { + // we need to pass some information from the strategy configuration to the exit methods, like symbol, interval and window + rt := reflect.TypeOf(m).Elem() + rv := reflect.ValueOf(m).Elem() + for j := 0; j < rv.NumField(); j++ { + if !rt.Field(j).IsExported() { + continue + } + + fieldValue := rv.Field(j) + if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() { + continue + } + + InheritStructValues(fieldValue.Interface(), parent) + } +} + + // InheritStructValues merges the field value from the source struct to the dest struct. // Only fields with the same type and the same name will be updated. func InheritStructValues(dst, src interface{}) {