Skip to content

Commit

Permalink
[ggj][engx] fix: consolidate findRepeatedField into Message (#388)
Browse files Browse the repository at this point in the history
* fix: support LRO package.name annotations

* feat: add microgenerator rules for cloud/asset

* fix: update CircleCI

* fix: sort message fields in asc. index order

* feat: add cloud/redis microgenerator rules

* fix: linter

* fix: update parser test method sig order

* fix: update circleci config

* fix: linter

* fix: consoliate findRepeatedField into Message
  • Loading branch information
miraleung authored Oct 10, 2020
1 parent 0c0fb17 commit 1ff08ee
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
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

0 comments on commit 1ff08ee

Please sign in to comment.