1

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

astra.xvi
  • 43
  • 7

2 Answers2

0

No, basically. The main way of doing this would be to have two inner-types - one with a single-valued member, one with a repeated member, and have a oneof that covers the two inner-types. Or perhaps simpler: just have a repeated and separately have an enum, boolean, or other indicator to choose between the two possible meanings (so you can tell between "an array, that happened to have exactly one item" vs "the single value").

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Hello, I tried your first proposal: create a `oneof` that has a repeated member and a single valued member. I'm getting an error because `repeated` is not supported in `oneof`. – astra.xvi Nov 16 '22 at 16:03
  • @astra-xvi you need to wrap the `repeated` field in a `message` and use the `message` as a `oneof` alternative. – DazWilkin Nov 16 '22 at 17:37
  • @astra.xvi as DazWilkin says: I was advocating creating two message types, each with the required setup, and oneof *those* – Marc Gravell Nov 16 '22 at 19:31
0

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

astra.xvi
  • 43
  • 7