首页 > 解决方案 > 一个 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
}

我如何确保地图只采用上面定义的自定义类型?

标签: goprotocol-buffersproto

解决方案


我认为您需要定义一个包含 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;
}

推荐阅读