-
Notifications
You must be signed in to change notification settings - Fork 89
chore: refactor GrpcConversions to use OffsetDateTime instead of deprecated methods #1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
BenWhitehead
commented
May 25, 2022
- Add GrpcConversions#timestampCodec convert between java.time.OffsetDateTime and com.google.protobuf.Timestamp
- Add property test for timestampCodec
- Replace all usage of "Long" methods in GrpcConversions with their appropriate java.time alternatives
google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
Outdated
Show resolved
Hide resolved
…ecated methods * Add GrpcConversions#timestampCodec convert between java.time.OffsetDateTime and com.google.protobuf.Timestamp * Add property test for timestampCodec * Replace all usage of "Long" methods in GrpcConversions with their appropriate java.time alternatives
| private static final class Supp implements ArbitrarySupplier<Timestamp> { | ||
| @Override | ||
| public Arbitrary<Timestamp> get() { | ||
| return StorageArbitraries.timestamp(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you need this helper static method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than the Arbitrary<Timestamp> from StorageArbitraries being registered globally, this method provides the resolution handle to jqwik so it knows how to generate arbitraries for the test method.
Above on line 41 @ForAll(supplier = Supp.class) points to this class as the means to resolve the Arbitrary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we'd rather it be registered globally, I can do that too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, this is just to test the timestampCodec specifically; wouldn't you need a timestamp supplier for other tests such as bucket/ object metadata? I think that's what is confusing me. This is more of a question that can be answered after PR approval. Thanks Ben
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other things would also need the timestamp supplier, which so far has been accessed and composed directly via StorageArbitraries#timestamp().
This supplier class is effectively wiring so that jqwik finds that method for the test.
| Codec.of(this::crc32cEncode, this::crc32cDecode); | ||
|
|
||
| @VisibleForTesting | ||
| final Codec<OffsetDateTime, Timestamp> timestampCodec = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this in our codecs source instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the pattern from ApiaryConversions for that conversion to DataTime[1].
Give this conversion is specific to the use of protos it probably makes sense to keep it near the proto conversions. Or, is there another location you'd like to see this moved to?
[1]
java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/ApiaryConversions.java
Lines 131 to 152 in 359504b
| @VisibleForTesting | |
| final Codec<OffsetDateTime, DateTime> dateTimeCodec = | |
| Codec.of( | |
| odt -> { | |
| ZoneOffset offset = odt.getOffset(); | |
| int i = Math.toIntExact(TimeUnit.SECONDS.toMinutes(offset.getTotalSeconds())); | |
| return new DateTime(odt.toInstant().toEpochMilli(), i); | |
| }, | |
| dt -> { | |
| long milli = dt.getValue(); | |
| int timeZoneShiftMinutes = dt.getTimeZoneShift(); | |
| Duration timeZoneShift = Duration.of(timeZoneShiftMinutes, ChronoUnit.MINUTES); | |
| int hours = Math.toIntExact(timeZoneShift.toHours()); | |
| int minutes = | |
| Math.toIntExact( | |
| timeZoneShift.minusHours(timeZoneShift.toHours()).getSeconds() / 60); | |
| ZoneOffset offset = ZoneOffset.ofHoursMinutes(hours, minutes); | |
| return Instant.ofEpochMilli(milli).atOffset(offset); | |
| }); |