英文:
oneof CustomType protobuf
问题
你好!要确保map只接受上面定义的自定义类型,你可以在定义map时使用特定的类型名称。在这种情况下,你可以使用"oneof_above_defined_types"作为map的值类型。这样,map就只能接受Type1、Type2和Type3这三种类型作为值了。以下是修改后的代码示例:
message Request {
  // ...
  map<string, oneof_above_defined_types> params = n;
}
请注意,"oneof_above_defined_types"应该替换为你实际定义的自定义类型的名称,比如"Type1"、"Type2"和"Type3"。这样,你就可以确保map只接受这些自定义类型了。
英文:
I am tyring to replicate python schema code to golang(protobuf). I am stuck in 1 of the condition.
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
}
How do i make sure that map takes only custom types defined above?
答案1
得分: 2
我认为你需要定义一个包含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;
}
英文:
I think you'll need to define a new type that includes the oneof type:
message TypeX {
   oneof type_oneof {
    Type1 type_1 = 1;
    Type2 type_2 = 2;
    Type3 type_3 = 3;
  };
}
message Request {
   ...
   map<string, TypeX> params = n;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论