一个自定义类型的Protobuf。

huangapple go评论71阅读模式
英文:

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&lt;string, oneof_above_defined_types&gt; 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&lt;string, TypeX&gt; params = n;
}

huangapple
  • 本文由 发表于 2021年11月5日 11:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/69848402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定