mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 15:55:14 +00:00
31 lines
595 B
Go
31 lines
595 B
Go
|
package dynamic
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
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()
|
||
|
}
|
||
|
|
||
|
if symbol, ok := 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() + ":"
|
||
|
}
|