go - 一个 CustomType protobuf
问题描述
我很想将 python 模式代码复制到 golang(protobuf)。我被困在其中一种情况。
message Type1 {
enum Type{
type1 = 1
}
Type type = 0;
string name = 1;
}
message Type2 {
enum Type{
type2 = 1
}
Type type = 0;
string name = 1;
repeated string value = 2;
}
message Type3 {
enum Type{
time = 1
}
Type type = 0;
string name = 1;
string format = 2;
string value = 3;
}
message Request {
something
something
map<string, oneof_above_defined_types> params = n
}
我如何确保地图只采用上面定义的自定义类型?
解决方案
我认为您需要定义一个包含 oneof 类型的新类型:
message TypeX {
oneof type_oneof {
Type1 type_1 = 1;
Type2 type_2 = 2;
Type3 type_3 = 3;
};
}
message Request {
...
map<string, TypeX> params = n;
}
推荐阅读
- django - 在 Django REST Framework 中向用户配置文件视图添加额外的字段
- java - java Scanner 类中的 nextLine()、hasNextLine() 和 NoSuchElementException
- tensorflow - 无法加载原生 TensorFlow
- websphere - Websphere 9 启动和发布速度很差
- javascript - 用于创建 JSON 对象而不复制键的 JavaScript 循环
- c# - .NET Core/Razor 应用程序上的文件附件
- java - grails函数生成的空Csv
- amazon-web-services - 如何从 Amazon s3 中删除包含许多子目录的目录
- c++ - 保证在英特尔自然对齐的 POD 类型上的原子操作
- javascript - 将元素三倍的Javascript矩阵