Add the range and sign check for the google.protobuf.Duration
- there was a discussion here 3c2557c2c075d657f716489ac2e7fe1d4c5675cf
that for 1 second or more durations, a non-zero value for the
'nanos' field must be of the same sign as the 'seconds' field.
Also, it was mentioned, that the value should count as invalid,
if it's out of range.
The statement based on the documentation here:
https://protobuf.dev/reference/php/api-docs/Google/Protobuf/Duration.html
But also it was not clear how to implement such restrictions properly
in setters of Duration class, because it is obvious that such check
would slow down the setter a lot. Plus, the logic would make the setter
behaviour annoying for users. Setting one value would reset
the another if the sign is changed, and vice versa.
I spent some time looking at common practices and found out,
that usually protobuf generators do not automatically enforce the sign
rule in setters.
They delegate it to another utility classes or to helper methods.
From design point of view it's better, because the generated classes
stay simple data containers with simple setters and getters,
as they should be.
For example, see how ranges are taken into account here:
https://github.com/protocolbuffers/protobuf-go/blob/v1.36.11/types/known/durationpb/duration.pb.go#L226
Or here:
https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/util/time_util.h#L55
So, for complying with the requirements we can add an additional
methods into the generated Duration class. For example, the
'isValid()/isValid(seconds, nanos)'. Both should return true,
if nanos and seconds were set correctly, and returns false in all
other cases.
Task-number: QTBUG-132876
Pick-to: 6.11
Change-Id: I0327efaa8fdf5b60ff40ba0a2a1dfea2db2ce9d0
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>