micro/broker/wrap.go
Gorbunov Kirill Andreevich 0d497ca0df
Some checks failed
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled
#97 add As for Broker.
2024-02-27 21:05:42 +03:00

35 lines
680 B
Go

package broker
import (
"reflect"
)
func As(b Broker, target any) bool {
if b == nil {
return false
}
if target == nil {
return false
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
return false
}
targetType := typ.Elem()
if targetType.Kind() != reflect.Interface && !targetType.Implements(brokerType) {
return false
}
return as(b, val, targetType)
}
func as(b Broker, targetVal reflect.Value, targetType reflect.Type) bool {
if reflect.TypeOf(b).AssignableTo(targetType) {
targetVal.Elem().Set(reflect.ValueOf(b))
return true
}
return false
}
var brokerType = reflect.TypeOf((*Broker)(nil)).Elem()