Protobuf验证器命令在错误的路径中生成文件

发布于 2025-02-11 01:19:03 字数 653 浏览 2 评论 0原文

我正在尝试包括GRPC的请求验证。我像这样修改了Protobuf命令。 pkg/test/test.proto包含我的架构。

如果我运行以下命令:

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=. 

validator.go未在pkg/test中生成文件,而不是在新文件夹中生成的新文件夹中生成的文件{源相对pkg}/pkg/test /test.proto/validator.go

如何在没有文件夹结构的情况下生成验证器 pkg/test中的文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2025-02-18 01:19:03

分析

看起来像*。验证器.pb.go文件是在错误的目录中生成的。

使用pkg/test/test.proto文件>以下内容:

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;
}

生成文件系统内容:

$ 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

解决方案

添加- govalidators_opt = paths = paths = source_relative命令行参数。

请注意参数名称:

- govalidators_opt

完整的命令行:

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

产生的文件系统内容:

$ 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

其他引用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文