1

Hi I have a data stream pipeline that works over "events". Those events are simple protocol buffer messages, say:

message OrderCoffee {
    int32 id = 1;
}

message CancelOrder {
    int32 id = 1;
}

A client then serialize/encode those messages and push them into a message broker (say Google Pub/Sub). A subscriber consumes one message and tries to decode/deserialize (pseudocode):

decoded_message = OrderCoffe.decode(encoded_message)
decoded_message = CancelOrder.decode(encoded_message)

Which of those lines work? Both, at least in my Ruby code. I don't know if I have a conceptual misunderstanding about how to use protocol buffers or that is a ruby bug.

If that is the expected bahaviour, how can I know at runtime which message should I decode the received message?

EDIT:

Ok, the solution seems to be https://developers.google.com/protocol-buffers/docs/techniques?csw=1#self-description .

I couldn't understand though. Could someone provide an example of how to implement that in ruby?

Kamal Aboul-Hosn
  • 15,111
  • 1
  • 34
  • 46
Montenegrodr
  • 1,597
  • 1
  • 16
  • 30

1 Answers1

2

Basically, you can't from there. Protobuf messages are not self-describing. If it was me, I'd add a wrapper:

message SomeType {
    oneof the_thing {
        OrderCoffee order = 1;
        CancelOrder cancel = 2;
    }
}

When you deserialize a the_thing, you can test which inner object is assigned.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900