protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

PGV is unable to import another generated go file in generated validator file

Open hamed-yousefi-tb opened this issue 5 years ago • 3 comments

I have three proto files A.proto, B.proto and common.proto. I put some common messages between A and B into common.proto and I import it in A and B proto files. I use the below command to generate the go files:

protoc 
-I example-grpc-spec 
-I ${GOPATH}/src 
-I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate --go_out=plugins=grpc:$GOPATH/src 
--validate_out=lang=go:$GOPATH/src example-grpc-spec/src/api/message/A/v1/A.proto

Also, I'm using go_package to generate A.pb.go and A.pb.validate.go in a specific package. I defined go_package for each as below: In A.proto option go_package = "github.com/myrepo/example-grpc-go/pkg/api/message/A/v1;v1"; In B.proto option go_package = "github.com/myrepo/example-grpc-go/pkg/api/message/B/v1;v1"; In common.proto option go_package = "github.com/myrepo/example-grpc-go/pkg/api/message/v1;v1";

Build command works fine and generates everything properly, but there is a problem with generated validator files. There's no import for that common file in the generated validator file and I because of that I cannot build the project.

generated validator file imports:

import (
	"bytes"
	"errors"
	"fmt"
	"net"
	"net/mail"
	"net/url"
	"regexp"
	"strings"
	"time"
	"unicode/utf8"

	"github.com/golang/protobuf/ptypes"
)

what I expected to be:

import (
	"bytes"
	"errors"
	"fmt"
	"net"
	"net/mail"
	"net/url"
	"regexp"
	"strings"
	"time"
	"unicode/utf8"

	"github.com/golang/protobuf/ptypes"
	v1 "github.com/myrepo/example-grpc-go/pkg/api/message/v1"
)

hamed-yousefi-tb avatar Aug 30 '19 07:08 hamed-yousefi-tb

Hey, thanks for the report! Would be helpful to have a (reduced) example of these protos. Otherwise, it's hard to distinguish what's happening here.

rodaine avatar Sep 16 '19 20:09 rodaine

@rodaine I wrote this example in this morning: pgv-example If you take a look at this line, you can see that FilterOperation_name belongs to common/v1 package and this package didn't import there. let me know if you need any more details. thx

hamed-yousefi-tb avatar Sep 17 '19 06:09 hamed-yousefi-tb

Hey group! I came across this exact thing about five minutes ago. Whilst the validation code generates just fine, the generated import() list in my *.validate.go file is missing code from a common proto type I use throughout my other protos.

// Expected `package proto_tf_x

import ( "bytes" "errors" "fmt" "net" "net/mail" "net/url" "regexp" "strings" "time" "unicode/utf8"

"github.com/golang/protobuf/ptypes"
"github.com/davedotdev/proj/proto_common" // I added this manually...

)`

// Actual `package proto_tf_x

import ( "bytes" "errors" "fmt" "net" "net/mail" "net/url" "regexp" "strings" "time" "unicode/utf8"

"github.com/golang/protobuf/ptypes"

)`

The generated code that I require validation on imports the davedotdev/proj/proto_common package.

Hope this helps!

davedotdev avatar Apr 21 '20 13:04 davedotdev