一个自定义类型的Protobuf。

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

oneof CustomType protobuf

问题

你好!要确保map只接受上面定义的自定义类型,你可以在定义map时使用特定的类型名称。在这种情况下,你可以使用"oneof_above_defined_types"作为map的值类型。这样,map就只能接受Type1、Type2和Type3这三种类型作为值了。以下是修改后的代码示例:

  1. message Request {
  2. // ...
  3. map<string, oneof_above_defined_types> params = n;
  4. }

请注意,"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.

  1. message Type1 {
  2. enum Type{
  3. type1 = 1
  4. }
  5. Type type = 0;
  6. string name = 1;
  7. }
  8. message Type2 {
  9. enum Type{
  10. type2 = 1
  11. }
  12. Type type = 0;
  13. string name = 1;
  14. repeated string value = 2;
  15. }
  16. message Type3 {
  17. enum Type{
  18. time = 1
  19. }
  20. Type type = 0;
  21. string name = 1;
  22. string format = 2;
  23. string value = 3;
  24. }
  25. message Request {
  26. something
  27. something
  28. map&lt;string, oneof_above_defined_types&gt; params = n
  29. }

How do i make sure that map takes only custom types defined above?

答案1

得分: 2

我认为你需要定义一个包含oneof类型的新类型:

  1. message TypeX {
  2. oneof type_oneof {
  3. Type1 type_1 = 1;
  4. Type2 type_2 = 2;
  5. Type3 type_3 = 3;
  6. };
  7. }
  8. message Request {
  9. ...
  10. map<string, TypeX> params = n;
  11. }
英文:

I think you'll need to define a new type that includes the oneof type:

  1. message TypeX {
  2. oneof type_oneof {
  3. Type1 type_1 = 1;
  4. Type2 type_2 = 2;
  5. Type3 type_3 = 3;
  6. };
  7. }
  8. message Request {
  9. ...
  10. map&lt;string, TypeX&gt; params = n;
  11. }

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:

确定