package machine
import (
"errors"
"reflect"
"runtime"
)
func (m *Machine ) HandlersBind (handlers any , opts ...BindOpts ) (string , error ) {
v := reflect .ValueOf (handlers )
if v .Kind () != reflect .Pointer || v .Elem ().Kind () != reflect .Struct {
return "" , errors .New ("BindHandlers expects a pointer to a struct" )
}
return m .bindHandlers (m .newHandlerStruct (handlers , &v ), optBindOpts (opts ))
}
func (c *handlerCall ) Exec () *handlerReturn {
ret := handlerReturn {}
if c .refl .fn != nil {
callRet := c .refl .fn .Call ([]reflect .Value {reflect .ValueOf (c .event )})
ret .called = true
if len (callRet ) > 0 {
v1 , ok1 := callRet [0 ].Interface ().(bool )
v2 , ok2 := callRet [0 ].Interface ().(S )
if ok1 {
ret .retBool = v1
} else if ok2 {
ret .retStates = v2
ret .partialNeg = true
}
} else {
ret .retBool = true
}
} else if c .negotiation != nil {
ret .retBool = c .negotiation (c .event )
ret .called = true
} else if c .negotiationPartial != nil {
ret .retStates = c .negotiationPartial (c .event )
ret .partialNeg = true
ret .called = true
} else if c .final != nil {
c .final (c .event )
ret .called = true
} else {
panic ("invalid handler call " + c .name )
}
return &ret
}
func (m *Machine ) newHandlerStruct (
handlers any , methods *reflect .Value ,
) *handler {
if m .disposing .Load () {
return &handler {}
}
e := &handler {
isMap : false ,
refl : handlerReflect {
h : handlers ,
methods : methods ,
methodCache : make (map [string ]reflect .Value ),
missingCache : make (map [string ]struct {}),
},
}
return e
}
func tracerValidate(tracer Tracer ) error {
v := reflect .ValueOf (tracer )
if v .Kind () != reflect .Pointer || v .Elem ().Kind () != reflect .Struct {
return errors .New ("BindTracer expects a pointer to a struct" )
}
return nil
}
func (m *Machine ) SetGroups (groups any , optStates States ) {
m .schemaMx .Lock ()
defer m .schemaMx .Unlock ()
list := map [string ][]int {}
order := []string {}
index := m .stateNames
if groups != nil {
var walk func (reflect .Value )
walk = func (val reflect .Value ) {
if val .Kind () == reflect .Pointer {
if val .IsNil () {
return
}
val = val .Elem ()
}
if val .Kind () != reflect .Struct {
return
}
typ := val .Type ()
for i := 0 ; i < val .NumField (); i ++ {
field := typ .Field (i )
if field .Type .Kind () != reflect .Slice {
continue
}
value := val .Field (i )
if !value .CanInterface () {
continue
}
states , ok := value .Interface ().(S )
if !ok {
continue
}
if _ , ok := list [field .Name ]; ok {
continue
}
list [field .Name ] = index .Index (states )
order = append (order , field .Name )
}
for i := 0 ; i < val .NumField (); i ++ {
field := typ .Field (i )
if !field .Anonymous {
continue
}
kind := field .Type .Kind ()
if kind == reflect .Struct ||
(kind == reflect .Pointer &&
field .Type .Elem ().Kind () == reflect .Struct ) {
walk (val .Field (i ))
}
}
}
walk (reflect .ValueOf (groups ))
}
if optStates != nil {
groups , order2 := optStates .StateGroups ()
for _ , name := range order2 {
list [name ] = groups [name ]
order = append (order , name )
}
}
m .groups = list
m .groupsOrder = order
}
func funcName(fn func ()) string {
funcPtr := reflect .ValueOf (fn ).Pointer ()
funcObj := runtime .FuncForPC (funcPtr )
if funcObj == nil {
return ""
}
funcName := funcObj .Name ()
return funcName
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .