python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Allow mapping google.protobuf.duration to Optional datetime.timedelta

Open malteneuss opened this issue 4 years ago • 0 comments

We had some issues loosing a zero-duration. Currently, if a serialized Protobuf message contains a 0-duration like myDuration in this example:

syntax = "proto3";

message Some {
  string someField = 1;
  google.protobuf.duration myDuration = 2;
}

and data like

{
  "someField": "42",
  "myDuration": "0s"         
}

this myDuration field gets correctly deserialized into a timedelta(0). However, when forwarding/serializing that Python object again, the duration gets lost:

{
  "someField": "42",
  // myDuration missing
}

Because timedelta(0) is the default value, it appearently represents no-value in betterProto (see Well known Google Types).

Is it possible to configure the mapping from Protobuf's duration to an Optional timedelta value so that no-value can be distinguished from a 0-value in Python?

malteneuss avatar Jun 03 '21 14:06 malteneuss