SetTriggerParameters enhancment: validate interfaces
delwaterman opened this issue · comments
Orion Delwaterman commented
Currently SetTriggerParameters
only matches types exactly. It would be great if instead of requiring the exact type, you could pass SetTriggerParameters
and validateParameters
used reflect.Value#Implements to validate the parameter.
Quim Muntal commented
Could you share and code snippet example with what you want to achieve?
Orion Delwaterman commented
I have a few states where I want to pass an error through a transition
errIntType := reflect.TypeOf((*error)(nil)).Elem()
sm.SetTriggerParameters(ActionReturnError, errIntType)
sm.Configure(StateFinished).
OnEntryFrom(ActionReturnError, func(ctx context.Context, args ...interface{}) error {
err := args[0].(error)
r := &VolunteerResult{
Err: err,
}
chResult <- r
close(chResult)
return nil
})
Orion Delwaterman commented
From the example of https://pkg.go.dev/reflect#TypeOf you can then actually check if a type implements an interace:
mport (
"fmt"
"io"
"os"
"reflect"
)
func main() {
// As interface types are only used for static typing, a
// common idiom to find the reflection Type for an interface
// type Foo is to use a *Foo value.
writerType := reflect.TypeOf((*io.Writer)(nil)).Elem()
fileType := reflect.TypeOf((*os.File)(nil))
fmt.Println(fileType.Implements(writerType))
}
Quim Muntal commented
Implemented in #46