protocol-buffersgrpc-goprotobuf-go

protobuf validator command generates file in wrong path


I am trying to include request validation for grpc. I modified the protobuf command like this. pkg/test/test.proto contains my schema.

If i run the below command :

protoc --go_out=. \
--proto_path=${GOPATH}/src \
--proto_path=${GOPATH}/src/github.com/gogo/protobuf/gogoproto/ \
--proto_path=${GOPATH}/src/github.com/mwitkow/go-proto-validators/ \
--proto_path=. \ --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative pkg/test/test.proto --govalidators_out=. 

The validator.go file generated file is not generated inside pkg/test instead it is getting generated inside a new folder created {source relative pkg}/pkg/test/test.proto/validator.go.

How to generate validator.go file without the folder structure in pkg/test?


Solution

  • Analysis

    It looks like the *.validator.pb.go files are generated in the wrong directory.

    Using the pkg/test/test.proto file with the following content:

    syntax = "proto3";
    
    option go_package = "github.com/example-user/example-repository";
    
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    

    Produced the file system contents:

    $ find .
    .
    ./github.com
    ./github.com/example-user
    ./github.com/example-user/example-repository
    ./github.com/example-user/example-repository/test.validator.pb.go
    ./pkg
    ./pkg/test
    ./pkg/test/test_grpc.pb.go
    ./pkg/test/test.proto
    ./pkg/test/test.pb.go
    

    Solution

    Add the --govalidators_opt=paths=source_relative command line argument.

    Please, note the parameter name:

    --govalidators_opt

    The complete command line:

    protoc --go_out=. \
        --proto_path=. \
        --go_opt=paths=source_relative \
        --go-grpc_out=. \
        --go-grpc_opt=paths=source_relative \
        --govalidators_out=. \
        --govalidators_opt=paths=source_relative \
        pkg/test/test.proto
    

    Produced the file system contents:

    $ find .
    .
    ./pkg
    ./pkg/test
    ./pkg/test/test_grpc.pb.go
    ./pkg/test/test.proto
    ./pkg/test/test.pb.go
    ./pkg/test/test.validator.pb.go
    

    Additional references