Skip to content

Commit

Permalink
feat: BucketInfo conversion for gRPC (#1431)
Browse files Browse the repository at this point in the history
* feat: BucketInfo conversion for gRPC

* address feedback

* fix retentionPolicy encode

* address feedback

* lint

* use correct setter

* include proto-google-common-protos
  • Loading branch information
frankyn committed Jun 6, 2022
1 parent 1f14acd commit 500b73f
Show file tree
Hide file tree
Showing 9 changed files with 541 additions and 106 deletions.
4 changes: 4 additions & 0 deletions google-cloud-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.storage;

import static com.google.cloud.storage.Utils.dateTimeCodec;
import static com.google.cloud.storage.Utils.durationMillisCodec;
import static com.google.cloud.storage.Utils.ifNonNull;
import static com.google.cloud.storage.Utils.lift;
Expand Down Expand Up @@ -68,22 +69,16 @@
import com.google.cloud.storage.HmacKey.HmacKeyState;
import com.google.cloud.storage.NotificationInfo.EventType;
import com.google.cloud.storage.NotificationInfo.PayloadFormat;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import java.math.BigInteger;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.LogRecord;

Expand Down Expand Up @@ -128,29 +123,6 @@ final class ApiaryConversions {
private final Codec<NotificationInfo, com.google.api.services.storage.model.Notification>
notificationInfoCodec = Codec.of(this::notificationEncode, this::notificationDecode);

@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);
});

private ApiaryConversions() {}

Codec<Entity, String> entity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,13 @@ LifecycleCondition getLifecycleCondition() {
public static class LifecycleCondition implements Serializable {
private static final long serialVersionUID = -6482314338394768785L;
private final Integer age;
private final DateTime createdBefore;
private final OffsetDateTime createdBefore;
private final Integer numberOfNewerVersions;
private final Boolean isLive;
private final List<StorageClass> matchesStorageClass;
private final Integer daysSinceNoncurrentTime;
private final DateTime noncurrentTimeBefore;
private final DateTime customTimeBefore;
private final OffsetDateTime noncurrentTimeBefore;
private final OffsetDateTime customTimeBefore;
private final Integer daysSinceCustomTime;

private LifecycleCondition(Builder builder) {
Expand All @@ -493,13 +493,13 @@ private LifecycleCondition(Builder builder) {
public Builder toBuilder() {
return newBuilder()
.setAge(this.age)
.setCreatedBefore(this.createdBefore)
.setCreateBeforeOffsetDateTime(this.createdBefore)
.setNumberOfNewerVersions(this.numberOfNewerVersions)
.setIsLive(this.isLive)
.setMatchesStorageClass(this.matchesStorageClass)
.setDaysSinceNoncurrentTime(this.daysSinceNoncurrentTime)
.setNoncurrentTimeBefore(this.noncurrentTimeBefore)
.setCustomTimeBefore(this.customTimeBefore)
.setNoncurrentTimeBeforeOffsetDateTime(this.noncurrentTimeBefore)
.setCustomTimeBeforeOffsetDateTime(this.customTimeBefore)
.setDaysSinceCustomTime(this.daysSinceCustomTime);
}

Expand All @@ -526,7 +526,17 @@ public Integer getAge() {
return age;
}

/** @deprecated Use {@link #getCreatedBeforeOffsetDateTime()} */
@Deprecated
public DateTime getCreatedBefore() {
return Utils.dateTimeCodec.nullable().encode(createdBefore);
}

/**
* Returns the date and offset from UTC for this condition. If a time other than 00:00:00.000
* is present in the value, GCS will truncate to 00:00:00.000.
*/
public OffsetDateTime getCreatedBeforeOffsetDateTime() {
return createdBefore;
}

Expand All @@ -549,13 +559,37 @@ public Integer getDaysSinceNoncurrentTime() {

/**
* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
*
* @deprecated Use {@link #getNoncurrentTimeBeforeOffsetDateTime()}
*/
@Deprecated
public DateTime getNoncurrentTimeBefore() {
return Utils.dateTimeCodec.nullable().encode(noncurrentTimeBefore);
}

/**
* Returns the date and offset from UTC for this condition. If a time other than 00:00:00.000
* is present in the value, GCS will truncate to 00:00:00.000.
*/
public OffsetDateTime getNoncurrentTimeBeforeOffsetDateTime() {
return noncurrentTimeBefore;
}

/* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").*/
/**
* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
*
* @deprecated Use {@link #getCustomTimeBeforeOffsetDateTime()}
*/
@Deprecated
public DateTime getCustomTimeBefore() {
return Utils.dateTimeCodec.nullable().encode(customTimeBefore);
}

/**
* Returns the date and offset from UTC for this condition. If a time other than 00:00:00.000
* is present in the value, GCS will truncate to 00:00:00.000.
*/
public OffsetDateTime getCustomTimeBeforeOffsetDateTime() {
return customTimeBefore;
}

Expand All @@ -567,13 +601,13 @@ public Integer getDaysSinceCustomTime() {
/** Builder for {@code LifecycleCondition}. */
public static class Builder {
private Integer age;
private DateTime createdBefore;
private OffsetDateTime createdBefore;
private Integer numberOfNewerVersions;
private Boolean isLive;
private List<StorageClass> matchesStorageClass;
private Integer daysSinceNoncurrentTime;
private DateTime noncurrentTimeBefore;
private DateTime customTimeBefore;
private OffsetDateTime noncurrentTimeBefore;
private OffsetDateTime customTimeBefore;
private Integer daysSinceCustomTime;

private Builder() {}
Expand All @@ -594,9 +628,23 @@ public Builder setAge(Integer age) {
* Sets the date a Blob should be created before for an Action to be executed. Note that
* only the date will be considered, if the time is specified it will be truncated. This
* condition is satisfied when an object is created before midnight of the specified date in
* UTC. *
* UTC.
*
* @deprecated Use {@link #setCreateBeforeOffsetDateTime(OffsetDateTime)}
*/
@Deprecated
public Builder setCreatedBefore(DateTime createdBefore) {
return setCreateBeforeOffsetDateTime(
Utils.dateTimeCodec.nullable().decode(createdBefore));
}

/**
* Sets the date a Blob should be created before for an Action to be executed. Note that
* only the date will be considered, if the time is specified it will be truncated. This
* condition is satisfied when an object is created before midnight of the specified date in
* UTC.
*/
public Builder setCreateBeforeOffsetDateTime(OffsetDateTime createdBefore) {
this.createdBefore = createdBefore;
return this;
}
Expand Down Expand Up @@ -646,8 +694,22 @@ public Builder setDaysSinceNoncurrentTime(Integer daysSinceNoncurrentTime) {
* Note that only date part will be considered, if the time is specified it will be
* truncated. This condition is satisfied when the noncurrent time on an object is before
* this date. This condition is relevant only for versioned objects.
*
* @deprecated Use {@link #setNoncurrentTimeBeforeOffsetDateTime(OffsetDateTime)}
*/
@Deprecated
public Builder setNoncurrentTimeBefore(DateTime noncurrentTimeBefore) {
return setNoncurrentTimeBeforeOffsetDateTime(
Utils.dateTimeCodec.nullable().decode(noncurrentTimeBefore));
}

/**
* Sets the date with only the date part (for instance, "2013-01-15"). Note that only date
* part will be considered, if the time is specified it will be truncated. This condition is
* satisfied when the noncurrent time on an object is before this date. This condition is
* relevant only for versioned objects.
*/
public Builder setNoncurrentTimeBeforeOffsetDateTime(OffsetDateTime noncurrentTimeBefore) {
this.noncurrentTimeBefore = noncurrentTimeBefore;
return this;
}
Expand All @@ -657,8 +719,21 @@ public Builder setNoncurrentTimeBefore(DateTime noncurrentTimeBefore) {
* Note that only date part will be considered, if the time is specified it will be
* truncated. This condition is satisfied when the custom time on an object is before this
* date in UTC.
*
* @deprecated Use {@link #setCustomTimeBeforeOffsetDateTime(OffsetDateTime)}
*/
@Deprecated
public Builder setCustomTimeBefore(DateTime customTimeBefore) {
return setCustomTimeBeforeOffsetDateTime(
Utils.dateTimeCodec.nullable().decode(customTimeBefore));
}

/**
* Sets the date with only the date part (for instance, "2013-01-15"). Note that only date
* part will be considered, if the time is specified it will be truncated. This condition is
* satisfied when the custom time on an object is before this date in UTC.
*/
public Builder setCustomTimeBeforeOffsetDateTime(OffsetDateTime customTimeBefore) {
this.customTimeBefore = customTimeBefore;
return this;
}
Expand Down

0 comments on commit 500b73f

Please sign in to comment.