Reputation: 11
I have a basic protobuf message defined:
syntax = "proto3";
message Order {
string id = 1;
oneof placedby {
string customer_id = 2;
string store_id = 3;
}
}
I'm using kafka to create an event driven system, so when this event is picked up by the kafka consumer, I unmarshal the message, but the placed_by
field is always nil.
I'm setting it in a service like so:
newOrder := &Order{
Id: "123456789",
Placedby: &Order_CustomerId{ CustomerId: "987654321" },
}
out, err := proto.Marshal(newOrder)
if err != nil {
return err
}
At this point the event (out
) is broadcast and picked up by a consumer which unmarshals it:
order := &Order{}
err := proto.Unmarshal(event, order)
if err != nil {
return err
}
For some reason, the order.Placedby
field is always nil. The examples I've been able to find that marshal/unmarshal oneof field types are dealing with files: https://software-factotum.medium.com/protobuf-and-go-handling-oneof-field-type-172ca780ec47
Is there a known limitation for oneof fields when working with an event driven system? I'm using the "google.golang.org/protobuf/proto"
package version 1.28.0
Upvotes: 1
Views: 1048
Reputation: 11
Thank you Brits for your help. For anyone else running into this, as they pointed out, there wasn't an issue with the code itself. The gRPC service for Order wasn't shut down properly which caused it to be in a stale state (before the oneof field was added). Manually killing the gRPC service process and starting it back up again fixed the issue.
Upvotes: 0