An open API service indexing awesome lists of open source software.

https://github.com/alexandrevilain/protoc-gen-moq

protoc plugin which generates client structs mocks
https://github.com/alexandrevilain/protoc-gen-moq

golang grpc mock protobuf protocol-buffers

Last synced: 4 months ago
JSON representation

protoc plugin which generates client structs mocks

Awesome Lists containing this project

README

          

# protoc-gen-moq

protoc-gen-moq is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a mocked service client structs.
It generates its mocked struct using the [matryer/moq](https://github.com/matryer/moq) template.

## How to use it ?

You first need to install the plugin by placing it in your `$PATH`:
```bash
$ go install github.com/alexandrevilain/protoc-gen-moq@latest
```

Make sure that your `$GOBIN` is in your `$PATH`.

Then run it with protoc alongside the protoc-gen-go-grpc plugin:
```bash
$ protoc \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
--moq_out=. \
--moq_opt=paths=source_relative \
hello.proto
```

## Running tests

To run both unit and conformance tests run:

```bash
$ make test
```

## Why not using the template package of github.com/matryer/moq ?

I can't use the template package from [moq](https://github.com/matryer/moq) for two reasons:
- the `template` package is inside the `internal` package which prevents external usage of the code inside.
- the `template` package makes an extensive usage of `go/types` which is not used by protogen.

To ensure the 100% compatibility between struct generated by protoc-gen-moq and moq I created conformance tests which generate the mock for [https://github.com/grpc/grpc-go/blob/master/test/grpc_testing/test.proto](https://github.com/grpc/grpc-go/blob/master/test/grpc_testing/test.proto). The rendered moq file is then checked using go vet which will report an error if the generated struct does not implement the interface methods.