Examples using the reflection feature of Go Protocol Buffers API v2. It was presented at Go Conference Tokyo 2021 Spring. The slide is here (Though it's written by Japanese)
It has custom rules for String fields like a length or a format.
The way to use is simple. Set the options to String fields in .proto
and use the Validation
func to validate the concreate Go struct mapped with Proto Message.
message UpdateAccountIDRequest {
string iD = 1 [ (stroptpb.opts) = {min_len : 6, max_len: 12} ];
}
// ok
msg := &testproto.UpdateAccountIDRequest{
ID: "12345678",
}
err := stropt.Validate(msg)
fmt.Println(err)
// Output: nil
// shorted than min length
msg = &testproto.UpdateAccountIDRequest{
ID: "12345",
}
err = stropt.Validate(msg)
fmt.Println(err)
// Output: Field: ID, invalid length, the value must be longer than or equal to 6, but actual: 5
// longer than max length
msg = &testproto.UpdateAccountIDRequest{
ID: "1234567890123",
}
err = stropt.Validate(msg)
fmt.Println(err)
// Output: Field: ID, invalid length, the value must be shorter than or equal to 12, but actual: 13
It embeds random values to any structs implementing the proto.Message
interface.
proto.Message
is implemented by all structs generated by the default complier for Protocol Buffers.
It's also separated as the production package in another repository here.
message TestMessage {
string some_str = 1;
int32 some_int = 2;
float some_float = 3;
bool some_bool = 4;
repeated string some_slice = 5;
ChildMessage some_msg = 6;
repeated ChildMessage some_msgs = 7;
map<int32, ChildMessage> some_map = 8;
}
message ChildMessage{
int32 some_int = 1;
}
msg := &testproto.TestMessage{}
protorand.EmbedValues(msg)
fmt.Println(msg)
// It outputs the message with the random values
Go Protocol Buffers API v2のリフレクション機能の使用例になります。 Go Conference Tokyo 2021 Springで公開されたものなります。 スライドはこちら
Protocol BuffersのStringフィールドの、長さや形式などの制約を設定して、具体的な値をバリデーションする例です。
message UpdateAccountIDRequest {
string iD = 1 [ (stroptpb.opts) = {min_len : 6, max_len: 12} ];
}
// ok
msg := &testproto.UpdateAccountIDRequest{
ID: "12345678",
}
err := stropt.Validate(msg)
fmt.Println(err)
// Output: nil
// shorted than min length
msg = &testproto.UpdateAccountIDRequest{
ID: "12345",
}
err = stropt.Validate(msg)
fmt.Println(err)
// Output: Field: ID, invalid length, the value must be longer than or equal to 6, but actual: 5
// longer than max length
msg = &testproto.UpdateAccountIDRequest{
ID: "1234567890123",
}
err = stropt.Validate(msg)
fmt.Println(err)
// Output: Field: ID, invalid length, the value must be shorter than or equal to 12, but actual: 13
proto.Message
インターフェースを実装した構造体に、その値をランダムに埋め込む関数です。
proto.Message` は、Protocol Buffersのデフォルトコンパイラーが生成するすべての構造体に実装されています。
こちらは、本番用としてこちらに切り出されています。
message TestMessage {
string some_str = 1;
int32 some_int = 2;
float some_float = 3;
bool some_bool = 4;
repeated string some_slice = 5;
ChildMessage some_msg = 6;
repeated ChildMessage some_msgs = 7;
map<int32, ChildMessage> some_map = 8;
}
message ChildMessage{
int32 some_int = 1;
}
msg := &testproto.TestMessage{}
protorand.EmbedValues(msg)
fmt.Println(msg)
// It outputs the message with the random values