Skip to content

Commit

Permalink
feat: support deeply-nested types in AST and proto message parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung committed Oct 30, 2020
1 parent 9792aab commit e337c6a
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ public String pakkage() {
public abstract boolean useFullName();

@Override
public String enclosingClassName() {
public ImmutableList<String> enclosingClassNames() {
if (!hasEnclosingClass()) {
return null;
return ImmutableList.of();
}
return clazz().getEnclosingClass().getSimpleName();
// The innermost type will be the last element in the list.
ImmutableList.Builder<String> listBuilder = new ImmutableList.Builder<>();
Class currentClz = clazz();
while (currentClz.getEnclosingClass() != null) {
listBuilder.add(currentClz.getEnclosingClass().getSimpleName());
currentClz = currentClz.getEnclosingClass();
}
return listBuilder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface Reference {
boolean useFullName();

@Nullable
String enclosingClassName();
ImmutableList<String> enclosingClassNames();

@Nullable
Reference wildcardUpperBound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package com.google.api.generator.engine.ast;

import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
Expand All @@ -43,7 +43,7 @@ public abstract class VaporReference implements Reference {

@Nullable
@Override
public abstract String enclosingClassName();
public abstract ImmutableList<String> enclosingClassNames();

@Nullable
public abstract Reference supertypeReference();
Expand All @@ -56,9 +56,9 @@ public Reference wildcardUpperBound() {

@Override
public String fullName() {
// TODO(unsupported): Nested classes with depth greater than 1.
if (hasEnclosingClass()) {
return String.format("%s.%s.%s", pakkage(), enclosingClassName(), plainName());
return String.format(
"%s.%s.%s", pakkage(), String.join(DOT, enclosingClassNames()), plainName());
}
return String.format("%s.%s", pakkage(), plainName());
}
Expand All @@ -68,7 +68,7 @@ public String fullName() {

@Override
public boolean hasEnclosingClass() {
return !Strings.isNullOrEmpty(enclosingClassName());
return !enclosingClassNames().isEmpty();
}

@Override
Expand All @@ -86,7 +86,7 @@ public boolean isSupertypeOrEquals(Reference other) {
VaporReference ref = (VaporReference) other;
return pakkage().equals(ref.pakkage())
&& plainName().equals(ref.plainName())
&& Objects.equals(enclosingClassName(), ref.enclosingClassName());
&& Objects.equals(enclosingClassNames(), ref.enclosingClassNames());
}

@Override
Expand All @@ -112,14 +112,14 @@ public boolean equals(Object o) {
return pakkage().equals(ref.pakkage())
&& name().equals(ref.name())
&& generics().equals(ref.generics())
&& Objects.equals(enclosingClassName(), ref.enclosingClassName());
&& Objects.equals(enclosingClassNames(), ref.enclosingClassNames());
}

@Override
public int hashCode() {
int hash = 17 * pakkage().hashCode() + 19 * name().hashCode() + 23 * generics().hashCode();
if (!Strings.isNullOrEmpty(enclosingClassName())) {
hash += 29 * enclosingClassName().hashCode();
if (!enclosingClassNames().isEmpty()) {
hash += 29 * enclosingClassNames().hashCode();
}
return hash;
}
Expand All @@ -133,7 +133,8 @@ public static Builder builder() {
return new AutoValue_VaporReference.Builder()
.setUseFullName(false)
.setGenerics(ImmutableList.of())
.setIsStaticImport(false);
.setIsStaticImport(false)
.setEnclosingClassNames(Collections.emptyList());
}

// Private.
Expand All @@ -153,7 +154,11 @@ public Builder setGenerics(Reference... references) {

public abstract Builder setGenerics(List<Reference> clazzes);

public abstract Builder setEnclosingClassName(String enclosingClassName);
public Builder setEnclosingClassNames(String... enclosingClassNames) {
return setEnclosingClassNames(Arrays.asList(enclosingClassNames));
}

public abstract Builder setEnclosingClassNames(List<String> enclosingClassNames);

public abstract Builder setIsStaticImport(boolean isStaticImport);

Expand All @@ -166,8 +171,7 @@ public Builder setGenerics(Reference... references) {

abstract ImmutableList<Reference> generics();

@Nullable
abstract String enclosingClassName();
abstract ImmutableList<String> enclosingClassNames();

abstract boolean isStaticImport();

Expand All @@ -180,11 +184,11 @@ public VaporReference build() {

setPlainName(name());

setIsStaticImport(enclosingClassName() != null && isStaticImport());
setIsStaticImport(!enclosingClassNames().isEmpty() && isStaticImport());

StringBuilder sb = new StringBuilder();
if (enclosingClassName() != null && !isStaticImport()) {
sb.append(enclosingClassName());
if (!enclosingClassNames().isEmpty() && !isStaticImport()) {
sb.append(String.join(DOT, enclosingClassNames()));
sb.append(DOT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import javax.annotation.Nullable;

public class ImportWriterVisitor implements AstNodeVisitor {
private static final String DOT = ".";
private static final String NEWLINE = "\n";
private static final String PKG_JAVA_LANG = "java.lang";

Expand Down Expand Up @@ -423,7 +424,8 @@ private void references(List<Reference> refs) {

if (ref.isStaticImport()
&& !Strings.isNullOrEmpty(currentClassName)
&& ref.enclosingClassName().equals(currentClassName)) {
&& !ref.enclosingClassNames().isEmpty()
&& ref.enclosingClassNames().contains(currentClassName)) {
continue;
}

Expand All @@ -432,7 +434,8 @@ private void references(List<Reference> refs) {
staticImports.add(ref.fullName());
} else {
if (ref.hasEnclosingClass()) {
imports.add(String.format("%s.%s", ref.pakkage(), ref.enclosingClassName()));
imports.add(
String.format("%s.%s", ref.pakkage(), String.join(DOT, ref.enclosingClassNames())));
} else {
imports.add(ref.fullName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static MethodDefinition createGetRequestBuilderMethod(
TypeNode builderType =
TypeNode.withReference(
VaporReference.builder()
.setEnclosingClassName(method.inputType().reference().name())
.setEnclosingClassNames(method.inputType().reference().name())
.setName("Builder")
.setPakkage(method.inputType().reference().pakkage())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(String.format("%sClient", service.name()))
.setEnclosingClassNames(String.format("%sClient", service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ private static Map<String, TypeNode> createDynamicTypes(
VaporReference.builder()
.setName("Builder")
.setPakkage(resourceName.pakkage())
.setEnclosingClassName(thisClassName)
.setEnclosingClassNames(thisClassName)
.setIsStaticImport(true)
.build()));

Expand All @@ -1591,7 +1591,7 @@ private static Map<String, TypeNode> createDynamicTypes(
VaporReference.builder()
.setName(s)
.setPakkage(resourceName.pakkage())
.setEnclosingClassName(thisClassName)
.setEnclosingClassNames(thisClassName)
.setIsStaticImport(true)
.build()))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ private static Map<String, TypeNode> createVaporTypes(Service service) {
VaporReference.builder()
.setName(
String.format(t, JavaStyle.toUpperCamelCase(method.name())))
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setPakkage(service.pakkage())
.setIsStaticImport(true) // Same class, so they won't be imported.
.build()))));
Expand All @@ -1465,7 +1465,7 @@ private static Map<String, TypeNode> createVaporTypes(Service service) {
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down Expand Up @@ -1864,7 +1864,7 @@ private static TypeNode getPagedResponseType(Method method, Service service) {
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setIsStaticImport(true)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private static MethodDefinition createCreatorMethod(
TypeNode.withReference(
VaporReference.builder()
.setName("Builder")
.setEnclosingClassName(String.format("%sSettings", service.name()))
.setEnclosingClassNames(String.format("%sSettings", service.name()))
.setPakkage(service.pakkage())
.build());
Expr returnMethodExpr =
Expand Down Expand Up @@ -712,7 +712,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
TypeNode.withReference(
VaporReference.builder()
.setName(BUILDER_CLASS_NAME)
.setEnclosingClassName(getThisClassName(service.name()))
.setEnclosingClassNames(getThisClassName(service.name()))
.setPakkage(service.pakkage())
.setIsStaticImport(true)
.build()));
Expand All @@ -729,7 +729,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down Expand Up @@ -828,7 +828,7 @@ private static TypeNode getStubSettingsBuilderType(Service service) {
VaporReference.builder()
.setPakkage(service.pakkage())
.setName(BUILDER_CLASS_NAME)
.setEnclosingClassName(getStubSettingsClassName(service.name()))
.setEnclosingClassNames(getStubSettingsClassName(service.name()))
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static Map<String, TypeNode> createTypes(
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setEnclosingClassNames(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public class ServiceStubSettingsClassComposer {
private static final String OPERATION_SETTINGS_LITERAL = "OperationSettings";
private static final String SETTINGS_LITERAL = "Settings";

private static final String DOT = ".";
private static final String LEFT_BRACE = "{";
private static final String RIGHT_BRACE = "}";
private static final String SLASH = "/";
Expand Down Expand Up @@ -1369,7 +1370,7 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
t ->
TypeNode.withReference(
VaporReference.builder()
.setName(t.reference().enclosingClassName())
.setName(String.join(DOT, t.reference().enclosingClassNames()))
.setPakkage(t.reference().pakkage())
.build());
List<Statement> ctorBodyStatements = new ArrayList<>();
Expand Down Expand Up @@ -1845,7 +1846,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
VaporReference.builder()
.setName(NESTED_BUILDER_CLASS_NAME)
.setPakkage(pakkage)
.setEnclosingClassName(thisClassName)
.setEnclosingClassNames(thisClassName)
.setIsStaticImport(true)
.build()));

Expand Down Expand Up @@ -1874,7 +1875,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
VaporReference.builder()
.setName(getPagedResponseTypeName(m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(String.format("%sClient", service.name()))
.setEnclosingClassNames(String.format("%sClient", service.name()))
.setIsStaticImport(true)
.build()))));

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/google/api/generator/gapic/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -41,6 +42,11 @@ public abstract class Message {
@Nullable
public abstract ResourceName resource();

// The nested types in left-to-right order, if any.
// Example: com.google.Foo.Bar.Car.ThisType will have the outer types listed in the order
// [Foo, Bar, Car].
public abstract ImmutableList<String> outerNestedTypes();

public abstract Builder toBuilder();

public boolean hasResource() {
Expand All @@ -60,7 +66,7 @@ public Field findAndUnwrapFirstRepeatedField() {
}

public static Builder builder() {
return new AutoValue_Message.Builder();
return new AutoValue_Message.Builder().setOuterNestedTypes(Collections.emptyList());
}

@AutoValue.Builder
Expand All @@ -73,6 +79,8 @@ public abstract static class Builder {

public abstract Builder setResource(ResourceName resource);

public abstract Builder setOuterNestedTypes(List<String> outerNestedTypes);

abstract Builder setFieldMap(Map<String, Field> fieldMap);

abstract ImmutableList<Field> fields();
Expand Down
Loading

0 comments on commit e337c6a

Please sign in to comment.