Skip to content
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

[ggj][engx] fix: consolidate findRepeatedField into Message #388

Merged
merged 15 commits into from
Oct 10, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -817,21 +817,15 @@ private static List<ClassDefinition> createNestedPagingClasses(
}
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().name());
TypeNode repeatedResponseType = null;
for (Field field : methodOutputMessage.fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
repeatedResponseType = TypeNode.withReference(repeatedGenericRef);
break;
}
}

Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedResponseType,
repeatedPagedResultsField,
String.format(
"No repeated field found on message %s for method %s",
methodOutputMessage.name(), method.name()));

TypeNode repeatedResponseType = repeatedPagedResultsField.type();

nestedClasses.add(
createNestedRpcPagedResponseClass(method, repeatedResponseType, messageTypes, types));
nestedClasses.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,20 +478,21 @@ private static MethodDefinition createRpcTestMethod(
method, serviceName, classMemberVarExprs, resourceNames, messageTypes);
}
// Construct the expected response.
// TODO(miraleung): Paging here.
TypeNode methodOutputType = method.hasLro() ? method.lro().responseType() : method.outputType();
List<Expr> methodExprs = new ArrayList<>();

TypeNode repeatedResponseType = null;
VariableExpr responsesElementVarExpr = null;
if (method.isPaged()) {
Message methodOutputMessage = messageTypes.get(method.outputType().reference().name());
repeatedResponseType = findRepeatedPagedType(methodOutputMessage);
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedResponseType,
repeatedPagedResultsField,
String.format(
"No repeated type found for paged method %s with output message type %s",
"No repeated field found for paged method %s with output message type %s",
method.name(), methodOutputMessage.name()));

repeatedResponseType = repeatedPagedResultsField.type();
responsesElementVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(repeatedResponseType).setName("responsesElement").build());
Expand Down Expand Up @@ -1832,16 +1833,6 @@ private static TypeNode getCallableType(Method protoMethod) {
ConcreteReference.builder().setClazz(callableClazz).setGenerics(generics).build());
}

private static TypeNode findRepeatedPagedType(Message message) {
for (Field field : message.fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
return TypeNode.withReference(repeatedGenericRef);
}
}
return null;
}

private static String getCallableMethodName(Method protoMethod) {
Preconditions.checkState(
!protoMethod.stream().equals(Method.Stream.NONE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,28 +354,16 @@ private static List<Expr> createPagingStaticAssignExprs(
String.format(
"No method found for message type %s for method %s among %s",
pagedResponseMessageKey, method.name(), messageTypes.keySet()));
TypeNode repeatedResponseType = null;
String repeatedFieldName = null;
for (Field field : pagedResponseMessage.fields()) {
Preconditions.checkState(
field != null,
String.format("Null field found for message %s", pagedResponseMessage.name()));
if (field.isRepeated() && !field.isMap()) {
// Field is currently a List-type.
Preconditions.checkState(
!field.type().reference().generics().isEmpty(),
String.format("No generics found for field reference %s", field.type().reference()));
repeatedResponseType = TypeNode.withReference(field.type().reference().generics().get(0));
repeatedFieldName = field.name();
break;
}
}
Field repeatedPagedResultsField = pagedResponseMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedResponseType,
repeatedPagedResultsField,
String.format(
"No repeated type found for paged reesponse %s for method %s",
method.outputType().reference().name(), method.name()));

TypeNode repeatedResponseType = repeatedPagedResultsField.type();
String repeatedFieldName = repeatedPagedResultsField.name();

// Create the PAGE_STR_DESC variable.
TypeNode pagedListDescType =
TypeNode.withReference(
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/google/api/generator/gapic/model/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public boolean hasResourceReference() {
return type().equals(TypeNode.STRING) && resourceReference() != null;
}

abstract Builder toBuilder();

public static Builder builder() {
return new AutoValue_Field.Builder()
.setIsMessage(false)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/google/api/generator/gapic/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.api.generator.gapic.model;

import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -46,6 +47,18 @@ public boolean hasResource() {
return resource() != null;
}

/** Returns the first list repeated field in a message, unwrapped from its list type. */
@Nullable
public Field findAndUnwrapFirstRepeatedField() {
for (Field field : fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
return field.toBuilder().setType(TypeNode.withReference(repeatedGenericRef)).build();
}
}
return null;
}

public static Builder builder() {
return new AutoValue_Message.Builder();
}
Expand Down