Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetTriggerParameters enhancment: validate interfaces #45

Closed
delwaterman opened this issue May 15, 2022 · 4 comments · Fixed by #46
Closed

SetTriggerParameters enhancment: validate interfaces #45

delwaterman opened this issue May 15, 2022 · 4 comments · Fixed by #46

Comments

@delwaterman
Copy link

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.

@qmuntal
Copy link
Owner

qmuntal commented May 16, 2022

Could you share and code snippet example with what you want to achieve?

@delwaterman
Copy link
Author

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
		})

@delwaterman
Copy link
Author

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))

}

@qmuntal
Copy link
Owner

qmuntal commented May 17, 2022

Implemented in #46

@qmuntal qmuntal closed this as completed May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants