diff --git a/pkg/bbgo/persistence_test.go b/pkg/bbgo/persistence_test.go index c58cd9eda..ebc5314f0 100644 --- a/pkg/bbgo/persistence_test.go +++ b/pkg/bbgo/persistence_test.go @@ -12,6 +12,15 @@ import ( "github.com/c9s/bbgo/pkg/types" ) +type TestStructWithoutInstanceID struct { + Symbol string +} + +func (s *TestStructWithoutInstanceID) ID() string { + return "test-struct-no-instance-id" +} + + type TestStruct struct { *Environment *Graceful @@ -48,8 +57,16 @@ func preparePersistentServices() []service.PersistenceService { } func Test_callID(t *testing.T) { - id := callID(&TestStruct{}) - assert.NotEmpty(t, id) + t.Run("default", func(t *testing.T) { + id := callID(&TestStruct{}) + assert.NotEmpty(t, id) + assert.Equal(t, "test-struct", id) + }) + + t.Run("fallback", func(t *testing.T) { + id := callID(&TestStructWithoutInstanceID{Symbol: "BTCUSDT"}) + assert.Equal(t, "test-struct-no-instance-id:BTCUSDT", id) + }) } func Test_loadPersistenceFields(t *testing.T) { diff --git a/pkg/bbgo/reflect.go b/pkg/bbgo/reflect.go index 4f72e29ed..c9cb5a33b 100644 --- a/pkg/bbgo/reflect.go +++ b/pkg/bbgo/reflect.go @@ -18,7 +18,17 @@ func callID(obj interface{}) string { ret := m.Call(nil) return ret[0].String() } - return "" + + if symbol, ok := isSymbolBasedStrategy(sv); ok { + m := sv.MethodByName("ID") + ret := m.Call(nil) + return ret[0].String() + ":" + symbol + } + + // fallback to just ID + m := sv.MethodByName("ID") + ret := m.Call(nil) + return ret[0].String() + ":" } func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {