3 ms·
Proto 3 has the "oneof" type. For instance: message MyThing { oneof sum_type { TypeOne type_one = 1; TypeTwo type_two = 2; } } The lack of inherit
by Xorlev 9y ago
Proto 3 has the "oneof" type. For instance:
message MyThing {
oneof sum_type {
TypeOne type_one = 1;
TypeTwo type_two = 2;
}
}
The lack of inheritance seems awkward at first, but with oneof it isn't much of a blocker. The APIs for this aren't always great -- Go's in particular feel kind of awkward (IMO). Java's are nice -- it's a separate enum you can switch over.
An example from a Go project of mine:
switch req.StartAt.(type) {
case *pb.GetLogsRequest_Timestamp:
t, err := types.TimestampFromProto(req.GetTimestamp())
if err != nil {
return nil, errors.Errorf("Bad timestamp: %v", req.GetTimestamp())
}
filter.Timestamp = t
case *pb.GetLogsRequest_Offset:
filter.StartOffset = req.GetOffset()
case *pb.GetLogsRequest_Position_:
switch req.GetPosition() {
case pb.GetLogsRequest_LATEST:
filter.Position = LATEST
case pb.GetLogsRequest_EARLIEST:
filter.Position = EARLIEST
}
case nil:
default:
return nil, errors.Errorf("Unknown GetLogsRequest.StartAt type.")
}
}