protoc-gen_validate

1. 安装和配置

linux

  1. # fetches this repo into $GOPATH
  2. go get -d github.com/envoyproxy/protoc-gen-validate
  3. # installs PGV into $GOPATH/bin
  4. make build

macos

  1. tips:go版本1.21.1
  2. 1、手动安装,下载源代码
  3. git clone https://github.com/envoyproxy/protoc-gen-validate.git
  4. 2、进入克隆的目录
  5. cd protoc-gen-validate
  6. 3、手动构建并安装:使用以下命令手动构建并安装 protoc-gen-validate
  7. go build
  8. go install
  9. 4、将 可执行文件拷贝到 go的根目录的bin目录下

windows

exe 下载地址
exe下载地址
可以点此下载:protoc-gen-validate.zip

将 zip文件中的exe文件拷贝到 go的根目录的bin目录下

生成pb.go源码

  1. protoc hello.proto --go_out=. --go-grpc_out=. --validate_out="lang=go:."

2. proto

  1. 新建validate.proto文件内容从 https://github.com/envoyproxy/protoc-gen-validate/blob/master/validate/validate.proto 拷贝
  2. 新建helloworl.proto文件
  1. syntax = "proto3";
  2. import "validate.proto";
  3. option go_package=".;proto";
  4. service Greeter {
  5. rpc SayHello (Person) returns (Person);
  6. }
  7. message Person {
  8. uint64 id = 1 [(validate.rules).uint64.gt = 999];
  9. string email = 2 [(validate.rules).string.email = true];
  10. string name = 3 [(validate.rules).string = {
  11. pattern: "^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$",max_bytes: 256,}];
  12. }

3. 服务端

  1. package main
  2. import (
  3. "context"
  4. "google.golang.org/grpc/codes"
  5. "google.golang.org/grpc/status"
  6. "net"
  7. "google.golang.org/grpc"
  8. "start/pgv_test/proto"
  9. )
  10. type Server struct{}
  11. func (s *Server) SayHello(ctx context.Context, request *proto.Person) (*proto.Person,
  12. error){
  13. return &proto.Person{
  14. Id: 32,
  15. }, nil
  16. }
  17. type Validator interface {
  18. Validate() error
  19. }
  20. func main(){
  21. var interceptor grpc.UnaryServerInterceptor
  22. interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  23. // 继续处理请求
  24. if r, ok := req.(Validator); ok {
  25. if err := r.Validate(); err != nil {
  26. return nil, status.Error(codes.InvalidArgument, err.Error())
  27. }
  28. }
  29. return handler(ctx, req)
  30. }
  31. var opts []grpc.ServerOption
  32. opts = append(opts, grpc.UnaryInterceptor(interceptor))
  33. g := grpc.NewServer(opts...)
  34. proto.RegisterGreeterServer(g, &Server{})
  35. lis, err := net.Listen("tcp", "0.0.0.0:50051")
  36. if err != nil{
  37. panic("failed to listen:"+err.Error())
  38. }
  39. err = g.Serve(lis)
  40. if err != nil{
  41. panic("failed to start grpc:"+err.Error())
  42. }
  43. }

4. 客户端

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc"
  6. "start/pgv_test/proto"
  7. )
  8. type customCredential struct{}
  9. func main() {
  10. var opts []grpc.DialOption
  11. //opts = append(opts, grpc.WithUnaryInterceptor(interceptor))
  12. opts = append(opts, grpc.WithInsecure())
  13. conn, err := grpc.Dial("localhost:50051", opts...)
  14. if err != nil {
  15. panic(err)
  16. }
  17. defer conn.Close()
  18. c := proto.NewGreeterClient(conn)
  19. //rsp, _ := c.Search(context.Background(), &empty.Empty{})
  20. rsp, err := c.SayHello(context.Background(), &proto.Person{
  21. Email: "bobby",
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26. fmt.Println(rsp.Id)
  27. }