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

Support common google apis and fix issue with ";" in package names #26

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN mkdir /stubs
RUN apk -U --no-cache add git protobuf

RUN go get -u -v github.com/golang/protobuf/protoc-gen-go \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First I need to know whether this is a common package or not, otherwise I'll find a way how to add this kind of custom dependecies

Copy link
Author

@li-peng-dd li-peng-dd Sep 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to get the same protos from this repo instead: https://github.com/googleapis/api-common-protos

These are pretty common protos advocated by Google as part of their API design guide, so it's used by a lot of folks.

A more generic solution would be to have an option to pass in a specific protodir (that's mounted on the image), instead of just defaulting the protodir to the parent directory of the mocked proto file, this lets users be able to pull in all the dependencies in their entire proto project if they want.

i.e. my folder structure is:

protos/
├── google/
│   ├── http.proto
├── identity/
│   ├── auth.proto

If I mocked auth.proto, gripmock wouldn't be able to find http.proto as it sets protodir to protos/identity/, excluding the google folder. But If I could manually set protodir to protos/, that will fix the issue and give a more flexible way for users to specify dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just created pr #28 to cover this case. please check it out. with that, user should be able to derive their own dockerfile and install their own dependencies then pass the custom protos with --imports arg

github.com/mitchellh/mapstructure \
google.golang.org/grpc \
google.golang.org/grpc/reflection \
Expand Down
5 changes: 4 additions & 1 deletion gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ func generateProtoc(param protocParam) {
if len(protodirs) > 0 {
protodir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/"
}

args := []string{"-I", protodir}
// include well-known-types
args = append(args, "-I", "/protobuf")
if os.Getenv("GOPATH") != "" {
googlesApisPath := os.Getenv("GOPATH")+"/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/"
args = append(args, "-I", googlesApisPath)
}
args = append(args, param.protoPath...)
args = append(args, "--go_out=plugins=grpc:"+param.output)
args = append(args, fmt.Sprintf("--gripmock_out=admin-port=%s,grpc-address=%s,grpc-port=%s:%s",
Expand Down
7 changes: 7 additions & 0 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ func resolveDependencies(protos []*descriptor.FileDescriptorProto) map[string]st
continue
}

// some package declarations use a semicolon, which causes a formatting error
// use the alias instead, if available
var sections = strings.Split(pkg, ";")
if len(sections) > 1 {
pkg = sections[1]
}

alias := getAlias(proto.GetName())
// in case of found same alias
if ok := aliases[alias]; ok {
Expand Down