carvalhorr / protoc-gen-mock

47 stars 11 forks source link

cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage #19

Closed zlymeda closed 4 years ago

zlymeda commented 4 years ago

I am having issues running tests/compiling the code:

cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
    *Request does not implement protoreflect.ProtoMessage (missing ProtoReflect method)

I believe the main issue is proto.MessageV2 vs proto.MessageV1. go version go1.14.4 linux/amd64

I also ran it in docker using go 1.13: go version go1.13.12 linux/amd64

Steps to reproduce:

git checkout V1.1.11

mkdir greeter-service

protoc --plugin `which protoc-gen-mock` --go_out=plugins=grpc:greeter-service --mock_out=greeter-service greeter.proto

docker run -v $PWD:/mock  -it --rm golang:1.13-alpine sh -c "cd /mock; CGO_ENABLED=0 go test ./..."

Output:

# github.com/carvalhorr/protoc-gen-mock/grpchandler [github.com/carvalhorr/protoc-gen-mock/grpchandler.test]
grpchandler/handler_test.go:46:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:51:48: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
    *MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
        have Match(string, string) *stub.Stub
        want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:63:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:66:14: cannot use "return an error" (type string) as type *stub.ErrorResponse in field value
grpchandler/handler_test.go:70:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
    *MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
        have Match(string, string) *stub.Stub
        want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:82:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
    *MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
        have Match(string, string) *stub.Stub
        want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:94:4: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
grpchandler/handler_test.go:99:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
    *MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
        have Match(string, string) *stub.Stub
        want Match(context.Context, string, string) *stub.Stub
grpchandler/handler_test.go:109:42: cannot use mockStubsMatcher (type *MockStubsMatcher) as type stub.StubsMatcher in argument to MockHandler:
    *MockStubsMatcher does not implement stub.StubsMatcher (wrong type for Match method)
        have Match(string, string) *stub.Stub
        want Match(context.Context, string, string) *stub.Stub
# github.com/carvalhorr/protoc-gen-mock/greeter-service
greeter-service/greeter.mock.pb.go:41:57: cannot use new(Request) (type *Request) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
    *Request does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
greeter-service/greeter.mock.pb.go:46:56: cannot use new(Response) (type *Response) as type protoreflect.ProtoMessage in argument to stub.CreateStubExample:
    *Response does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
# github.com/carvalhorr/protoc-gen-mock/restcontrollers [github.com/carvalhorr/protoc-gen-mock/restcontrollers.test]
restcontrollers/examples_test.go:32:5: cannot use stub.StubRequest literal (type stub.StubRequest) as type *stub.StubRequest in field value
restcontrollers/examples_test.go:35:6: cannot use map[string]interface {} literal (type map[string]interface {}) as type map[string][]string in field value
restcontrollers/examples_test.go:37:5: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
restcontrollers/examples_test.go:40:15: cannot use "error1" (type string) as type *stub.ErrorResponse in field value
restcontrollers/examples_test.go:50:68: response.Header().Values undefined (type http.Header has no field or method Values)
restcontrollers/stubs_add_test.go:16:3: unknown field 'SupportedMethods' in struct literal of type StubsController
restcontrollers/stubs_add_test.go:50:3: unknown field 'SupportedMethods' in struct literal of type StubsController
restcontrollers/stubs_delete_test.go:16:3: cannot use stub.StubRequest literal (type stub.StubRequest) as type *stub.StubRequest in field value
restcontrollers/stubs_delete_test.go:20:3: cannot use stub.StubResponse literal (type stub.StubResponse) as type *stub.StubResponse in field value
restcontrollers/stubs_delete_test.go:23:13: cannot use "error1" (type string) as type *stub.ErrorResponse in field value
restcontrollers/stubs_delete_test.go:20:3: too many errors
?       github.com/carvalhorr/protoc-gen-mock/bootstrap [no test files]
FAIL    github.com/carvalhorr/protoc-gen-mock/grpchandler [build failed]
FAIL    github.com/carvalhorr/protoc-gen-mock/restcontrollers [build failed]
--- FAIL: TestJsonString_Matches_TwoEqualJsonStrings (0.00s)
panic: runtime error: comparing uncomparable type map[string]interface {} [recovered]
    panic: runtime error: comparing uncomparable type map[string]interface {}

goroutine 19 [running]:
testing.tRunner.func1(0xc000152100)
    /usr/local/go/src/testing/testing.go:874 +0x3a3
panic(0x85a540, 0xc00008f3d0)
    /usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/carvalhorr/protoc-gen-mock/stub.jsonStringMatches(0xc000093710, 0xc000093770, 0x0, 0x8274e0)
    /mock/stub/model.go:162 +0x2db
github.com/carvalhorr/protoc-gen-mock/stub.(*JsonString).Matches(0xc000046f50, 0x8e9601, 0x32, 0x5efaed9d)
    /mock/stub/model.go:95 +0x12c
github.com/carvalhorr/protoc-gen-mock/stub.TestJsonString_Matches_TwoEqualJsonStrings(0xc000152100)
    /mock/stub/model_test.go:11 +0x4e
testing.tRunner(0xc000152100, 0x8f2db0)
    /usr/local/go/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:960 +0x350
FAIL    github.com/carvalhorr/protoc-gen-mock/stub  0.005s
zlymeda commented 4 years ago

I could not find a way how to generate proto.MessageV2 when running protoc.

carvalhorr commented 4 years ago

Could you provide what version of protoc-gen-go you are using? I believe it should work with version 1.21.0 forward, but I would need to double-check that.

zlymeda commented 4 years ago

you are right! thanks.

I installed protoc with snap (on ubuntu). protoc-gen-go comes with it, but I can't get the version, as protoc-gen-go --version returns error (version arg is not supported).

I changed my PATH to take my go/bin first and now it works. now my versions are:

// versions:
//  protoc-gen-go v1.25.0
//  protoc        v3.11.4