bbgo_origin/pkg/bbgo/reflect.go

34 lines
637 B
Go
Raw Normal View History

package bbgo
import (
"reflect"
2022-06-29 10:51:22 +00:00
"github.com/c9s/bbgo/pkg/dynamic"
)
2022-05-17 10:10:37 +00:00
type InstanceIDProvider interface {
InstanceID() string
}
func callID(obj interface{}) string {
sv := reflect.ValueOf(obj)
st := reflect.TypeOf(obj)
if st.Implements(reflect.TypeOf((*InstanceIDProvider)(nil)).Elem()) {
m := sv.MethodByName("InstanceID")
ret := m.Call(nil)
return ret[0].String()
}
2022-06-29 10:51:22 +00:00
if symbol, ok := dynamic.LookupSymbolField(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() + ":"
}