gRPC集成protoc-gen-validate

protoc-gen-validate简介

安装和配置 Linux
执行make build之前需要先切换到protoc-gen-validate路径下;因为make build执行的就是这个路径下的Makefile;一定要确保在对应的路径下,这样make build才不会出错

fetches this repo into $GOPATH

go get -d github.com/envoyproxy/protoc-gen-validate

installs PGV into $GOPATH/bin

make build
Windows
0.6.7版本exe下载地址
最新版本exe(页面查找)
下载完成后需要将exe文件拷贝到 go的根补录的bin目录下

代码 proto hello.proto
syntax = “proto3”;
option go_package = “.;proto”;
import “validate.proto”;

service Greeter{
rpc SayHello(Person) returns (Person);
}

message Person {
uint64 id = 1 [(validate.rules).uint64.gt = 999];

string email = 2 [(validate.rules).string.email = true];

string name = 3 [(validate.rules).string = {
pattern: “[u4e00-u9fa5]”,
max_bytes: 30,
}];

Location home = 4 [(validate.rules).message.required = true];

message Location {
double lat = 1 [(validate.rules).double = {gte: -90, lte: 90}];
double lng = 2 [(validate.rules).double = {gte: -180, lte: 180}];
}
}
validate.proto
validate.proto再hello.proto有引用;可以直接再validate.protocopy出来一份放到与hello.proto的同级目录下,也可以直接复制下面的代码

syntax = “proto2”;
package validate;

option go_package = “github.com/envoyproxy/protoc-gen-validate/validate”;
option java_package = “io.envoyproxy.pgv.validate”;

import “google/protobuf/descriptor.proto”;
import “google/protobuf/duration.proto”;
import “google/protobuf/timestamp.proto”;

// Validation rules applied at the message level
extend google.protobuf.MessageOptions {
// Disabled nullifies any validation rules for this message, including any
// message fields associated with it that do support validation.
optional bool disabled = 1071;
// Ignore skips generation of validation methods for this message.
optional bool ignored = 1072;
}

// Validation rules applied at the oneof level
extend google.protobuf.OneofOptions {
// Required ensures that exactly one the field options in a oneof is set;
// validation fails if no fields in the oneof are set.
optional bool required = 1071;
}

// Validation rules applied at the field level
extend google.protobuf.FieldOptions {
// Rules specify the validations to be performed on this field. By default,
// no validation is performed against a field.
optional FieldRules rules = 1071;
}

// FieldRules encapsulates the rules for each type of field. Depending on the
// field, the correct set should be used to ensure proper validations.
message FieldRules {
optional MessageRules message = 17;
oneof type {
// Scalar Field Types
FloatRules float = 1;
DoubleRules double = 2;
Int32Rules int32 = 3;
Int64Rules int64 = 4;
UInt32Rules uint32 = 5;
UInt64Rules uint64 = 6;
SInt32Rules sint32 = 7;
SInt64Rules sint64 = 8;
Fixed32Rules fixed32 = 9;
Fixed64Rules fixed64 = 10;
SFixed32Rules sfixed32 = 11;
SFixed64Rules sfixed64 = 12;
BoolRules bool = 13;
StringRules string = 14;
BytesRules bytes = 15;

// Complex Field Types
EnumRules     enum     = 16;
RepeatedRules repeated = 18;
MapRules      map      = 19;

// Well-Known Field Types
AnyRules       any       = 20;
DurationRules  duration  = 21;
TimestampRules timestamp = 22;

}
}

// FloatRules describes the constraints applied to float values
message FloatRules {
// Const specifies that this field must be exactly the specified value
optional float const = 1;

// Lt specifies that this field must be less than the specified value,
// exclusive
optional float lt = 2;

// Lte specifies that this field must be less than or equal to the
// specified value, inclusive
optional float lte = 3;

// Gt specifies that this field must be greater than the specified value,
// exclusive. If the value of Gt is larger than a specified Lt or Lte, the
// range is reversed.
optional float gt = 4;

// Gte specifies that this field must be greater than or equal to the
// specified value, inclusive. If the value of Gte is larger than a
// specified Lt or Lte, the range is reversed.
optional float gte = 5;

// In specifies that this field must be equal to one of the specified
// values
repeated float in = 6;

// NotIn specifies that this field cannot be equal to one of the specified
// values
repeated float not_in = 7;

// IgnoreEmpty specifies that the validation rules of this field should be
// evaluated only if the field is not empty
optional bool ignore_empty = 8;
}

// DoubleRules describes the constraints applied to double values
message DoubleRules {
// Const specifies that this field must be exactly the specified value
optional double const = 1;

// Lt specifies that this field must be less than the specified value,
// exclusive
optional double lt = 2;

// Lte specifies that this field must be less than or equal to the
// specified value, inclusive
optional double lte = 3;

// Gt specifies that this field must be greater than the specified value,
// exclusive. If the value of Gt is larger than a specified Lt or Lte, the
// range is reversed.
optional double gt = 4;

// Gte specifies that this field must be greater than or equal to the
// specified value, inclusive. If the value of Gte is larger than a
// specified Lt or Lte, the range is reversed.
optional double gte = 5;

// In specifies that this field must be equal to one of the specified
// values
repeated double in = 6;

// NotIn specifies that this field cannot be equal to one of the specified
// values
repeated double not_in = 7;

// IgnoreEmpty specifies that the validation rules of this field should be
// evaluated only if the field is not empty
optional bool ignore_empty = 8;
}

// Int32Rules describes the constraints applied to int32 values
message Int32Rules {
// Const specifies that this field must be exactly the specified value
optional int32 const = 1;

// Lt specifies that this field must be less than the specified value,
// exclusive
optional int32 lt = 2;

// Lte specifies that this field must be less than or equal to the
// specified value, inclusive
optional int32 lte = 3;

// Gt specifies that this field must be greater than the specified value,
// exclusive. If the value of Gt is larger than a specified Lt or Lte, the
// range is reversed.
optional int32 gt = 4;

// Gte specifies that this field must be greater than or equal to the
// specified value, inclusive. If the value of Gte is lar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}` 是一个 Maven 坐标,用于使用 gRPCProtocol Buffers 编译器插件来生成 gRPC 相关的 Java 代码。 这个坐标指定了以下部分: - `io.grpc` 是 Maven 组织 ID,表示该插件是由 gRPC 提供的。 - `protoc-gen-grpc-java` 是插件的名称,用于生成 gRPC 相关的 Java 代码。 - `1.0.0` 是插件的版本号,表示要使用的插件版本。 - `exe:${os.detected.classifier}` 指定了插件的文件类型和操作系统相关的后缀。 `${os.detected.classifier}` 是一个 Maven 变量,用于根据操作系统自动选择相应的插件文件。它会根据当前操作系统选择适当的文件后缀,例如在 Windows 上是 `.exe`,在 Linux 上是 `.linux-x86_64`。 通过在 Maven 项目的 `pom.xml` 文件中添加该依赖项,您可以在构建过程中自动下载并使用该插件来生成 gRPCJava 代码。例如: ```xml <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.17.3:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> ``` 这样配置后,您可以使用 `mvn compile` 命令来自动生成 gRPCJava 代码。生成的代码将位于 `target/generated-sources/protobuf` 目录下。 请确保您的 Maven 项目中已经包含了正确的依赖项,并且配置文件中的版本号与您所需的版本一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值