Skip to content

sryoya/protoreflect-go-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protoreflect-go-examples

About

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)

Contents

1. String with custom regulations

proto defenition

implementation

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.

Example

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

2. Random Message generation for Protol Buffers Message

implementation

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.

Example

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

protoreflect-go-examples (日本語版, Description by Japanese)

About

Go Protocol Buffers API v2のリフレクション機能の使用例になります。 Go Conference Tokyo 2021 Springで公開されたものなります。 スライドはこちら

Contents

1. String with custom regulations

proto defenition

implementation

Protocol BuffersのStringフィールドの、長さや形式などの制約を設定して、具体的な値をバリデーションする例です。

Example

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

2. Random Message generation for Protol Buffers Message

implementation

proto.Message インターフェースを実装した構造体に、その値をランダムに埋め込む関数です。 proto.Message` は、Protocol Buffersのデフォルトコンパイラーが生成するすべての構造体に実装されています。

こちらは、本番用としてこちらに切り出されています。

Example

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

About

The example usage of protoreflect

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages