nestjsprotocol-buffersgrpcgrpc-nodeprotobuf.js

ProtoBuf - Can a field support two possible datatypes?


is it possible for one field in protobuf to support/accept a string or an array of string. This is my message:

message MessageA {
  string fieldId =1;
  string method = 2;
  google.protobuf.Any value =3;
}

The reason for it being dynamic is because an array of string or a simple string can be the input. There are separate methods on what will happen if the payload is string[] or string.

Right now, I'm using any but I'm not sure what to do in the any file. I've read that oneof does not support array data types thats why I'm trying to make it work with any.

Here is the error message when I try to put a repeated inside a oneof: enter image description here


Solution

  • For anyone that might have the same problem as me, you can use google.protobuf.Value. There would be an additional object but it can be transformed using UsePipe so it shouldn't be a problem.

    Here is the documentation: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value