improve callID fallback for persistence

This commit is contained in:
c9s 2022-06-22 15:19:22 +08:00
parent 5d72ffaa0f
commit 027f1f01cf
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 30 additions and 3 deletions

View File

@ -12,6 +12,15 @@ import (
"github.com/c9s/bbgo/pkg/types" "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 { type TestStruct struct {
*Environment *Environment
*Graceful *Graceful
@ -48,8 +57,16 @@ func preparePersistentServices() []service.PersistenceService {
} }
func Test_callID(t *testing.T) { func Test_callID(t *testing.T) {
id := callID(&TestStruct{}) t.Run("default", func(t *testing.T) {
assert.NotEmpty(t, id) 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) { func Test_loadPersistenceFields(t *testing.T) {

View File

@ -18,7 +18,17 @@ func callID(obj interface{}) string {
ret := m.Call(nil) ret := m.Call(nil)
return ret[0].String() 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) { func isSymbolBasedStrategy(rs reflect.Value) (string, bool) {