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

Dispatcher to handle anon func returned in closure #27

Merged
merged 1 commit into from
Oct 25, 2024
Merged

Conversation

dfava
Copy link
Contributor

@dfava dfava commented Oct 25, 2024

Take this higher order function that returns an anonymous function:

func ReturnAnonFunc(component string) func(string, float64) {
	return func(label string, time float64) {
		TestFunctionsCalled[fmt.Sprintf("ReturnAnonFunc.%s", component)] = true
	}
}

We can now register with dispatcher like so:

	ctx = querysql.WithDispatcher(ctx, querysql.GoMSSQLDispatcher([]interface{}{
		testhelper.ReturnAnonFunc("myComponent"),
	}))

That's because the anon function is named in go as ReturnAnonFunc.func1. So, we can use the ReturnAnonFunc part of the anonymous function name in order to dispatch the correct function at runtime.

This means we can have one anonymous function per high-order function. Meaning, the dispatcher cannot disambiguate between:

testhelper.ReturnAnonFunc("myComponent"),
testhelper.ReturnAnonFunc("myComponent2"),

Therefore, if the user tries to record more than one anon functions from the same higher-order function, the dispatcher will panic.

@dfava dfava merged commit 531f756 into main Oct 25, 2024
1 check passed
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 this pull request may close these issues.

2 participants