Skip to content

Commit

Permalink
fix: Embed builder in path classes and make components directly acces…
Browse files Browse the repository at this point in the history
…sible (#231)

* fix: Embed builder in path classes and make components directly accessible.

* fix: Remove semicolon
  • Loading branch information
dpcollins-google committed Sep 3, 2020
1 parent 89f6582 commit e5e7d7d
Show file tree
Hide file tree
Showing 36 changed files with 197 additions and 329 deletions.
5 changes: 5 additions & 0 deletions google-cloud-pubsublite/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<className>com/google/cloud/pubsublite/*Path*</className>
<method>*</method>
</difference>
<difference>
<differenceType>8001</differenceType>
<className>com/google/cloud/pubsublite/*Paths*</className>
<method>*</method>
</difference>
<!-- Blanket ignored files -->
<difference>
<differenceType>6000</differenceType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,31 @@
/** A string wrapper representing a project and location. */
@AutoValue
public abstract class LocationPath implements Serializable {
public abstract ProjectPath project();
public abstract ProjectIdOrNumber project();

public abstract CloudZone location();

public ProjectPath projectPath() {
return ProjectPath.newBuilder().setProject(project()).build();
}

@Override
public String toString() {
return project() + "/locations/" + location();
return projectPath() + "/locations/" + location();
}

/** Create a new LocationPath builder. */
public static Builder newBuilder() {
return new AutoValue_LocationPath.Builder();
}

public static LocationPath of(ProjectPath project, CloudZone zone) {
return new AutoValue_LocationPath(project, zone);
public abstract Builder toBuilder();

@AutoValue.Builder
public abstract static class Builder extends ProjectBuilderHelper<Builder> {
public abstract Builder setLocation(CloudZone zone);

public abstract LocationPath build();
}

/**
Expand All @@ -49,6 +63,9 @@ public static LocationPath parse(String path) throws StatusException {
checkArgument(splits.length == 4);
checkArgument(splits[2].equals("locations"));
ProjectPath project = ProjectPath.parse(String.join("/", Arrays.copyOf(splits, 2)));
return LocationPath.of(project, CloudZone.parse(splits[3]));
return LocationPath.newBuilder()
.setProject(project.project())
.setLocation(CloudZone.parse(splits[3]))
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ private PartitionLookupUtils() {}
public static int numPartitions(TopicPath topic) throws StatusException {
try (AdminClient client =
AdminClient.create(
AdminClientSettings.newBuilder()
.setRegion(topic.location().location().region())
.build())) {;
AdminClientSettings.newBuilder().setRegion(topic.location().region()).build())) {
return numPartitions(topic, client);
} catch (Exception e) {
throw ExtractStatus.toCanonical(e);
Expand Down Expand Up @@ -66,9 +64,7 @@ public static int numPartitions(TopicPath topic, AdminClient client) throws Stat
public static int numPartitions(SubscriptionPath subscription) throws StatusException {
try (AdminClient client =
AdminClient.create(
AdminClientSettings.newBuilder()
.setRegion(subscription.location().location().region())
.build())) {
AdminClientSettings.newBuilder().setRegion(subscription.location().region()).build())) {
return numPartitions(subscription, client);
} catch (Exception e) {
throw ExtractStatus.toCanonical(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ private static ProjectNumber getProjectNumber(ProjectId id) throws StatusExcepti
}
}

public static ProjectPath toCannonical(ProjectPath project) throws StatusException {
if (project.project().getKind() == Kind.NUMBER) return project;
return ProjectPath.of(ProjectIdOrNumber.of(getProjectNumber(project.project().name())));
static ProjectNumber toCannonical(ProjectIdOrNumber project) throws StatusException {
if (project.getKind() == Kind.NUMBER) return project.number();
return getProjectNumber(project.name());
}

public static LocationPath toCannonical(LocationPath location) throws StatusException {
ProjectPath canonicalProject = toCannonical(location.project());
return LocationPath.of(canonicalProject, location.location());
public static ProjectPath toCannonical(ProjectPath path) throws StatusException {
return path.toBuilder().setProject(toCannonical(path.project())).build();
}

public static SubscriptionPath toCannonical(SubscriptionPath subscription)
throws StatusException {
LocationPath canonicalLocation = toCannonical(subscription.location());
return SubscriptionPath.of(canonicalLocation, subscription.name());
public static LocationPath toCannonical(LocationPath path) throws StatusException {
return path.toBuilder().setProject(toCannonical(path.project())).build();
}

public static TopicPath toCannonical(TopicPath topic) throws StatusException {
LocationPath canonicalLocation = toCannonical(topic.location());
return TopicPath.of(canonicalLocation, topic.name());
public static SubscriptionPath toCannonical(SubscriptionPath path) throws StatusException {
return path.toBuilder().setProject(toCannonical(path.project())).build();
}

public static TopicPath toCannonical(TopicPath path) throws StatusException {
return path.toBuilder().setProject(toCannonical(path.project())).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public String toString() {
return "projects/" + project();
}

public static ProjectPath of(ProjectIdOrNumber value) {
return new AutoValue_ProjectPath(value);
/** Create a new ProjectPath builder. */
public static Builder newBuilder() {
return new AutoValue_ProjectPath.Builder();
}

public abstract Builder toBuilder();

@AutoValue.Builder
public abstract static class Builder extends ProjectBuilderHelper<Builder> {
public abstract ProjectPath build();
}

/**
Expand All @@ -47,11 +55,12 @@ public static ProjectPath parse(String path) throws StatusException {
checkArgument(splits[0].equals("projects"));
checkArgument(!splits[1].isEmpty());
try {
return ProjectPath.of(ProjectIdOrNumber.of(ProjectNumber.of(Long.parseLong(splits[1]))));
long val = Long.parseLong(splits[1]);
return ProjectPath.newBuilder().setProject(ProjectNumber.of(val)).build();
} catch (NumberFormatException e) {
// Pass, treat as a name. Project ids must start with a letter.
// https://cloud.google.com/resource-manager/docs/creating-managing-projects#before_you_begin
}
return ProjectPath.of(ProjectIdOrNumber.of(ProjectId.of(splits[1])));
return ProjectPath.newBuilder().setProject(ProjectId.of(splits[1])).build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,47 @@
*/
@AutoValue
public abstract class SubscriptionPath implements Serializable {
public abstract LocationPath location();
public abstract ProjectIdOrNumber project();

public abstract CloudZone location();

public abstract SubscriptionName name();

public LocationPath locationPath() {
return LocationPath.newBuilder().setProject(project()).setLocation(location()).build();
}

@Override
public String toString() {
return location() + "/subscriptions/" + name();
return locationPath() + "/subscriptions/" + name();
}

/** Create a new SubscriptionPath builder. */
public static Builder newBuilder() {
return new AutoValue_SubscriptionPath.Builder();
}

public static SubscriptionPath of(LocationPath location, SubscriptionName name) {
return new AutoValue_SubscriptionPath(location, name);
public abstract Builder toBuilder();

@AutoValue.Builder
public abstract static class Builder extends ProjectBuilderHelper<Builder> {
public abstract Builder setLocation(CloudZone zone);

public abstract Builder setName(SubscriptionName name);

/** Build a new SubscriptionPath. */
public abstract SubscriptionPath build();
}

public static SubscriptionPath parse(String path) throws StatusException {
String[] splits = path.split("/");
checkArgument(splits.length == 6);
checkArgument(splits[4].equals("subscriptions"));
LocationPath location = LocationPath.parse(String.join("/", Arrays.copyOf(splits, 4)));
return SubscriptionPath.of(location, SubscriptionName.of(splits[5]));
return SubscriptionPath.newBuilder()
.setProject(location.project())
.setLocation(location.location())
.setName(SubscriptionName.of(splits[5]))
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,47 @@
*/
@AutoValue
public abstract class TopicPath implements Serializable {
public abstract LocationPath location();
public abstract ProjectIdOrNumber project();

public abstract CloudZone location();

public abstract TopicName name();

public LocationPath locationPath() {
return LocationPath.newBuilder().setProject(project()).setLocation(location()).build();
}

@Override
public String toString() {
return location() + "/topics/" + name();
return locationPath() + "/topics/" + name();
}

/** Create a new TopicPath builder. */
public static Builder newBuilder() {
return new AutoValue_TopicPath.Builder();
}

public static TopicPath of(LocationPath location, TopicName name) {
return new AutoValue_TopicPath(location, name);
public abstract Builder toBuilder();

@AutoValue.Builder
public abstract static class Builder extends ProjectBuilderHelper<Builder> {
public abstract Builder setLocation(CloudZone zone);

public abstract Builder setName(TopicName name);

/** Build a new TopicPath. */
public abstract TopicPath build();
}

public static TopicPath parse(String path) throws StatusException {
String[] splits = path.split("/");
checkArgument(splits.length == 6);
checkArgument(splits[4].equals("topics"));
LocationPath location = LocationPath.parse(String.join("/", Arrays.copyOf(splits, 4)));
return TopicPath.of(location, TopicName.of(splits[5]));
return TopicPath.newBuilder()
.setProject(location.project())
.setLocation(location.location())
.setName(TopicName.of(splits[5]))
.build();
}
}
Loading

0 comments on commit e5e7d7d

Please sign in to comment.