-2

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?

Pawan Kumar
  • 247
  • 6
  • 21

1 Answers1

2

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;
}
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • Yeah i tried doing that but looks like schema changes and it is not compatible with previous one. eg. { "id":"some_id", "random":"some_random", "params":{ "random_name":{ "type":"type1", "name":"name" }, "random_2":{ "type":"type2", "name":"type2_name", "value":"type2_value", } } – Pawan Kumar Nov 05 '21 at 05:02
  • I don't understand your comment. Using `Request` as defined in my answer, your JSON would be `{"params":{"key-1":{"type_1":{"type":0,"name":"name-1"}},"key-2":{"type_2":{"type":0,"name":"name-2","value":["foo","bar","baz"]}},"key-3":{"type_3":{"type":0,"name":"name-3","format":"format-3","value":"value-3"}}}}` – DazWilkin Nov 05 '21 at 15:22
  • **NOTE** Protobuf requires that enumerations start at 0 too because, if unset, these take the default value and this will be zero; so you need to have an equivalent. – DazWilkin Nov 05 '21 at 15:23