Home > Software engineering >  How to pack a repeated protobuf message into an Any
How to pack a repeated protobuf message into an Any

Time:08-24

I'm working with a third party service that consumes a com.google.protobuf.Any. I need to pass in a repeated Message myObject object, but how does one pack a repeated protobuf field into an Any?

I'm writing a Kotlin extension function along the lines of fun List<Message>.toProtobufAny(): com.google.protobuf.Any {...} to (hopefully) make this generic for any Message because I'll be sending in a variety of repeated message types. My first attempts have been to implement an Any.pack(RepeatedFieldBuilder<>), but haven't been successful yet with the syntax. Not finding very many code example out there on this specific use case.

Thanks.

CodePudding user response:

An Any contains a single proto, not a repeated field.

You can create a List<Any>, your own AnyRepeated proto, or an Any containing a proto that has a repeated proto field for your chosen message type, but you cannot pack more than one proto into an Any.

  • Related