You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing a simple package for wrapping functions, similarly to HTTP middleware, which allow adding metrics, logging etc without having to resort to code generation.
package wrappers
import "context"
type Func[A, B any] func(ctx context.Context, req A) (resp B, err error)
type Wrapper[A, B any] func(src Func[A, B]) Func[A, B]
func Wrap[A, B any](src Func[A, B], wrappers ...Wrapper[A, B]) Func[A, B] {
for i := len(wrappers) - 1; i >= 0; i-- {
src = wrappers[i](src)
}
return src
}
Testing the functionality I've wrote following code:
type testA struct {
x int
}
type testB struct {
y string
}
func testWrapper[A, B any](dest io.Writer) wrappers.Wrapper[A, B] {
return func(src wrappers.Func[A, B]) wrappers.Func[A, B] {
return func(ctx context.Context, a A) (B, error) {
fmt.Fprintf(dest, "calling with %#v\n", a)
res, err := src(ctx, a)
fmt.Fprintf(dest, "got: %#v, %+v", res, err)
return res, err
}
}
}
func TestWrappers_Wrap(t *testing.T) {
f := func(ctx context.Context, req *testA) (resp *testB, err error) {
return &testB{
y: fmt.Sprintf("(%d)", req.x),
}, nil
}
buf := new(bytes.Buffer)
f2 := wrappers.Wrap(f, testWrapper(buf))
f2(context.TODO(), &testA{10})
t.Logf("%s", buf.String())
}
What did you expect to see?
I expected to see the test to pass:
$go test -v
=== RUN TestWrappers_Wrap
wrapper_test.go:40: calling with &wrappers_test.testA{x:10}
got: &wrappers_test.testB{y:"(10)"}, <nil>
--- PASS: TestWrappers_Wrap (0.00s)
PASS
ok github.com/andviro/wrappers 0.003s
What did you see instead?
$go test -v
# github.com/andviro/wrappers_test [github.com/andviro/wrappers.test]
./wrapper_test.go:38:36: cannot infer A (/home/andrew/go/src/github.com/andviro/wrappers/w
rapper_test.go:21:18)
FAIL github.com/andviro/wrappers [build failed]
I think that it's a bug in type inference system, because type A is clearly inferred from first parameter of wrappers.Wrap, the function f. Without second parameter code compiles without error.
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm writing a simple package for wrapping functions, similarly to HTTP middleware, which allow adding metrics, logging etc without having to resort to code generation.
Testing the functionality I've wrote following code:
What did you expect to see?
I expected to see the test to pass:
What did you see instead?
I think that it's a bug in type inference system, because type A is clearly inferred from first parameter of wrappers.Wrap, the function f. Without second parameter code compiles without error.
The text was updated successfully, but these errors were encountered: