背景
protobuf 2中定义的enum枚举值必须为数字类型,故不支持string类型,但有些业务场景又确实需要定义string常量。
目标
在protobuf 2中定义string常量。
方案
思路:通optional + default实现string常量。
细节:
1、protobuf定义
syntax = "proto2";
package stonelu.learn.pb;
message Constant {
message TestStringConst {
optional string NO_TEST_STRING_CONST = 1 [default = "no_test_string_const"]; // val: no_test_string_const
optional string Email = 2 [default = "email"]; // val: email
optional string Sex = 3 [default = "sex"]; // val: sex
optional string Address = 4 [default = "address"]; // val address
}
}
2、生成pb.