Skip to content

Commit

Permalink
# This is a combination of 6 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

# This is a combination of 2 commits.
# This is the 1st commit message:

# This is a combination of 2 commits.
# This is the 1st commit message:

wip: first stab at trying to corral all {to,from}Pb methods from model classes

# This is the commit message #2:

wip: refactor all access of ApiaryConversions to use their intermediary `codec` method

# This is the commit message #2:

wip: add clirr rule to allow removing accidental public hmacKeyMetadata#toPb

# This is the commit message #2:

wip: add enum ApiaryConversions#Codecs

# This is the commit message #3:

wip: fix lifecycle rule tests

# This is the commit message #4:

wip: fix blob metadata encoding

# This is the commit message #5:

wip: replace Blob#fromPb and Bucket#fromPb with BlobInfo#asBlob and BucketInfo#asBucket

The old fromPb method were conflating creating an instance of an "Info" model with create an instance of an "Info" model and binding it together with a Storage instance.

Now, an instance of Info model can be decoded (where/whenever) and then turned into a Blob/Bucket as necessary.

# This is the commit message #6:

tersify conversions
  • Loading branch information
BenWhitehead committed May 2, 2022
1 parent 21d7c06 commit 2018f6b
Show file tree
Hide file tree
Showing 30 changed files with 1,818 additions and 1,380 deletions.
5 changes: 5 additions & 0 deletions google-cloud-storage/clirr-ignored-differences.xml
Expand Up @@ -11,4 +11,9 @@
<differenceType>7012</differenceType>
<method>* downloadTo(com.google.cloud.storage.BlobId, java.nio.file.Path, com.google.cloud.storage.Storage$BlobSourceOption[])</method>
</difference>
<difference>
<differenceType>7002</differenceType>
<className>com/google/cloud/storage/HmacKey$HmacKeyMetadata</className>
<method>* toPb()</method>
</difference>
</differences>
126 changes: 7 additions & 119 deletions google-cloud-storage/src/main/java/com/google/cloud/storage/Acl.java
Expand Up @@ -19,11 +19,8 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.ApiFunction;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
Expand All @@ -37,20 +34,6 @@
public final class Acl implements Serializable {

private static final long serialVersionUID = 7516713233557576082L;
static final Function<ObjectAccessControl, Acl> FROM_OBJECT_PB_FUNCTION =
new Function<ObjectAccessControl, Acl>() {
@Override
public Acl apply(ObjectAccessControl aclPb) {
return Acl.fromPb(aclPb);
}
};
static final Function<BucketAccessControl, Acl> FROM_BUCKET_PB_FUNCTION =
new Function<BucketAccessControl, Acl>() {
@Override
public Acl apply(BucketAccessControl aclPb) {
return Acl.fromPb(aclPb);
}
};

private final Entity entity;
private final Role role;
Expand All @@ -64,15 +47,9 @@ private Role(String constant) {
super(constant);
}

private static final ApiFunction<String, Role> CONSTRUCTOR =
new ApiFunction<String, Role>() {
@Override
public Role apply(String constant) {
return new Role(constant);
}
};
private static final ApiFunction<String, Role> CONSTRUCTOR = Role::new;

private static final StringEnumType<Role> type = new StringEnumType(Role.class, CONSTRUCTOR);
private static final StringEnumType<Role> type = new StringEnumType<>(Role.class, CONSTRUCTOR);

public static final Role OWNER = type.createAndRegister("OWNER");
public static final Role READER = type.createAndRegister("READER");
Expand Down Expand Up @@ -195,36 +172,7 @@ public int hashCode() {

@Override
public String toString() {
return toPb();
}

String toPb() {
return type.name().toLowerCase() + "-" + getValue();
}

static Entity fromPb(String entity) {
if (entity.startsWith("user-")) {
return new User(entity.substring(5));
}
if (entity.equals(User.ALL_USERS)) {
return User.ofAllUsers();
}
if (entity.equals(User.ALL_AUTHENTICATED_USERS)) {
return User.ofAllAuthenticatedUsers();
}
if (entity.startsWith("group-")) {
return new Group(entity.substring(6));
}
if (entity.startsWith("domain-")) {
return new Domain(entity.substring(7));
}
if (entity.startsWith("project-")) {
int idx = entity.indexOf('-', 8);
String team = entity.substring(8, idx);
String projectId = entity.substring(idx + 1);
return new Project(Project.ProjectRole.valueOf(team.toUpperCase()), projectId);
}
return new RawEntity(entity);
return Conversions.apiary().entity().encode(this);
}
}

Expand Down Expand Up @@ -272,8 +220,8 @@ public String getEmail() {
public static final class User extends Entity {

private static final long serialVersionUID = 3076518036392737008L;
private static final String ALL_USERS = "allUsers";
private static final String ALL_AUTHENTICATED_USERS = "allAuthenticatedUsers";
static final String ALL_USERS = "allUsers";
static final String ALL_AUTHENTICATED_USERS = "allAuthenticatedUsers";

/**
* Creates a user entity.
Expand All @@ -289,19 +237,6 @@ public String getEmail() {
return getValue();
}

@Override
String toPb() {
switch (getValue()) {
case ALL_AUTHENTICATED_USERS:
return ALL_AUTHENTICATED_USERS;
case ALL_USERS:
return ALL_USERS;
default:
break;
}
return super.toPb();
}

public static User ofAllUsers() {
return new User(ALL_USERS);
}
Expand All @@ -326,16 +261,10 @@ private ProjectRole(String constant) {
super(constant);
}

private static final ApiFunction<String, ProjectRole> CONSTRUCTOR =
new ApiFunction<String, ProjectRole>() {
@Override
public ProjectRole apply(String constant) {
return new ProjectRole(constant);
}
};
private static final ApiFunction<String, ProjectRole> CONSTRUCTOR = ProjectRole::new;

private static final StringEnumType<ProjectRole> type =
new StringEnumType(ProjectRole.class, CONSTRUCTOR);
new StringEnumType<>(ProjectRole.class, CONSTRUCTOR);

public static final ProjectRole OWNERS = type.createAndRegister("OWNERS");
public static final ProjectRole EDITORS = type.createAndRegister("EDITORS");
Expand Down Expand Up @@ -390,11 +319,6 @@ public static final class RawEntity extends Entity {
RawEntity(String entity) {
super(Type.UNKNOWN, entity);
}

@Override
String toPb() {
return getValue();
}
}

private Acl(Builder builder) {
Expand Down Expand Up @@ -482,40 +406,4 @@ public boolean equals(Object obj) {
&& Objects.equals(this.etag, other.etag)
&& Objects.equals(this.id, other.id);
}

BucketAccessControl toBucketPb() {
BucketAccessControl bucketPb = new BucketAccessControl();
bucketPb.setEntity(getEntity().toString());
bucketPb.setRole(getRole().toString());
bucketPb.setId(getId());
bucketPb.setEtag(getEtag());
return bucketPb;
}

ObjectAccessControl toObjectPb() {
ObjectAccessControl objectPb = new ObjectAccessControl();
objectPb.setEntity(getEntity().toPb());
objectPb.setRole(getRole().name());
objectPb.setId(getId());
objectPb.setEtag(getEtag());
return objectPb;
}

static Acl fromPb(ObjectAccessControl objectAccessControl) {
Role role = Role.valueOf(objectAccessControl.getRole());
Entity entity = Entity.fromPb(objectAccessControl.getEntity());
return newBuilder(entity, role)
.setEtag(objectAccessControl.getEtag())
.setId(objectAccessControl.getId())
.build();
}

static Acl fromPb(BucketAccessControl bucketAccessControl) {
Role role = Role.valueOf(bucketAccessControl.getRole());
Entity entity = Entity.fromPb(bucketAccessControl.getEntity());
return newBuilder(entity, role)
.setEtag(bucketAccessControl.getEtag())
.setId(bucketAccessControl.getId())
.build();
}
}

0 comments on commit 2018f6b

Please sign in to comment.