Skip to content

Commit

Permalink
fix(types): Use fully-qualified message type names [ggj] (#723)
Browse files Browse the repository at this point in the history
* fix(types): Use fully-qualified message type names

* fix: cleanup
  • Loading branch information
miraleung authored May 13, 2021
1 parent 2318136 commit 8a5c36c
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static Expr createBatchingDescriptorFieldDeclExpr(

private static MethodDefinition createGetBatchPartitionKeyMethod(
Method method, GapicBatchingSettings batchingSettings, Map<String, Message> messageTypes) {
String methodInputTypeName = method.inputType().reference().name();
String methodInputTypeName = method.inputType().reference().fullName();
Message inputMessage = messageTypes.get(methodInputTypeName);
Preconditions.checkNotNull(
inputMessage,
Expand Down Expand Up @@ -283,7 +283,7 @@ private static MethodDefinition createSplitResponseMethod(

List<Statement> outerForBody = new ArrayList<>();
if (hasSubresponseField) {
Message outputMessage = messageTypes.get(method.outputType().reference().name());
Message outputMessage = messageTypes.get(method.outputType().reference().fullName());
Preconditions.checkNotNull(
outputMessage, String.format("Output message not found for RPC %s", method.name()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ private static List<ClassDefinition> createNestedPagingClasses(
continue;
}
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down Expand Up @@ -1657,7 +1657,6 @@ private static TypeStore createTypes(Service service, Map<String, Message> messa
TimeUnit.class,
UnaryCallable.class);
TypeStore typeStore = new TypeStore(concreteClazzes);
typeStore.putMessageTypes(service.pakkage(), messageTypes);
createVaporTypes(service, typeStore);
return typeStore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private static MethodDefinition createRpcTestMethod(
VariableExpr responsesElementVarExpr = null;
String mockServiceVarName = getMockServiceVarName(rpcService);
if (method.isPaged()) {
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down Expand Up @@ -577,7 +577,7 @@ private static MethodDefinition createRpcTestMethod(
Variable.builder().setType(methodOutputType).setName("expectedResponse").build());
Expr expectedResponseValExpr = null;
if (method.isPaged()) {
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field firstRepeatedField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
firstRepeatedField,
Expand All @@ -589,10 +589,10 @@ private static MethodDefinition createRpcTestMethod(
DefaultValueComposer.createSimplePagedResponse(
method.outputType(), firstRepeatedField.name(), responsesElementVarExpr);
} else {
if (messageTypes.containsKey(methodOutputType.reference().name())) {
if (messageTypes.containsKey(methodOutputType.reference().fullName())) {
expectedResponseValExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
messageTypes.get(methodOutputType.reference().simpleName()),
messageTypes.get(methodOutputType.reference().fullName()),
resourceNames,
messageTypes);
} else {
Expand Down Expand Up @@ -658,7 +658,7 @@ private static MethodDefinition createRpcTestMethod(
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());
argExprs.add(requestVarExpr);
requestMessage = messageTypes.get(method.inputType().reference().simpleName());
requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
Expand Down Expand Up @@ -784,7 +784,7 @@ private static MethodDefinition createRpcTestMethod(
.build());

// Assert the responses are equivalent.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down Expand Up @@ -1022,10 +1022,10 @@ private static MethodDefinition createStreamingRpcTestMethod(
VariableExpr.withVariable(
Variable.builder().setType(methodOutputType).setName("expectedResponse").build());
Expr expectedResponseValExpr = null;
if (messageTypes.containsKey(methodOutputType.reference().name())) {
if (messageTypes.containsKey(methodOutputType.reference().fullName())) {
expectedResponseValExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
messageTypes.get(methodOutputType.reference().simpleName()),
messageTypes.get(methodOutputType.reference().fullName()),
resourceNames,
messageTypes);
} else {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ private static MethodDefinition createStreamingRpcTestMethod(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
Expand Down Expand Up @@ -1363,7 +1363,7 @@ private static List<Statement> createStreamingRpcExceptionTestStatements(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
Expand Down Expand Up @@ -1552,7 +1552,7 @@ private static List<Statement> createRpcExceptionTestStatements(
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());
argVarExprs.add(varExpr);
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ private static TypeStore createTypes(Service service, Map<String, Message> messa
UnaryCallable.class,
UnsupportedOperationException.class);
TypeStore typeStore = new TypeStore(concreteClazzes);
typeStore.putMessageTypes(service.pakkage(), messageTypes);

typeStore.put("com.google.longrunning.stub", "OperationsStub");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,9 @@ private static List<Expr> createPagingStaticAssignExprs(
}

// Find the repeated type.
String pagedResponseMessageKey =
JavaStyle.toUpperCamelCase(method.outputType().reference().simpleName());
String pagedResponseMessageKey = method.outputType().reference().fullName();
if (method.hasLro()) {
pagedResponseMessageKey =
JavaStyle.toUpperCamelCase(method.lro().responseType().reference().simpleName());
pagedResponseMessageKey = method.lro().responseType().reference().fullName();
}
Message pagedResponseMessage = messageTypes.get(pagedResponseMessageKey);
Preconditions.checkNotNull(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static Expr createSimpleMessageBuilderExpr(
if (field.isContainedInOneof() // Avoid colliding fields.
|| ((field.isMessage() || field.isEnum()) // Avoid importing unparsed messages.
&& !field.isRepeated()
&& !messageTypes.containsKey(field.type().reference().name()))) {
&& !messageTypes.containsKey(field.type().reference().fullName()))) {
continue;
}
String setterMethodNamePattern = "set%s";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ public static String composeRpcDefaultMethodHeaderSampleCode(
VariableExpr.withVariable(
Variable.builder().setName("request").setType(method.inputType()).build());
List<VariableExpr> rpcMethodArgVarExprs = Arrays.asList(requestVarExpr);
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(
requestMessage,
String.format(
"Could not find the message type %s.", method.inputType().reference().simpleName()));
"Could not find the message type %s.", method.inputType().reference().fullName()));
Expr requestBuilderExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
Expand Down Expand Up @@ -346,11 +346,11 @@ public static String composeLroCallableMethodHeaderSampleCode(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("request").setType(method.inputType()).build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(
requestMessage,
String.format(
"Could not find the message type %s.", method.inputType().reference().simpleName()));
"Could not find the message type %s.", method.inputType().reference().fullName()));
Expr requestBuilderExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
Expand Down Expand Up @@ -458,11 +458,11 @@ public static String composePagedCallableMethodHeaderSampleCode(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("request").setType(method.inputType()).build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(
requestMessage,
String.format(
"Could not find the message type %s.", method.inputType().reference().simpleName()));
"Could not find the message type %s.", method.inputType().reference().fullName()));
Expr requestBuilderExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
Expand All @@ -476,7 +476,7 @@ public static String composePagedCallableMethodHeaderSampleCode(
bodyExprs.add(requestAssignmentExpr);

// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down Expand Up @@ -578,11 +578,11 @@ public static String composeRegularCallableMethodHeaderSampleCode(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("request").setType(method.inputType()).build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(
requestMessage,
String.format(
"Could not find the message type %s.", method.inputType().reference().simpleName()));
"Could not find the message type %s.", method.inputType().reference().fullName()));
Expr requestBuilderExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
Expand Down Expand Up @@ -626,11 +626,11 @@ public static String composeStreamCallableMethodHeaderSampleCode(
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("request").setType(method.inputType()).build());
Message requestMessage = messageTypes.get(method.inputType().reference().simpleName());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(
requestMessage,
String.format(
"Could not find the message type %s.", method.inputType().reference().simpleName()));
"Could not find the message type %s.", method.inputType().reference().fullName()));
Expr requestBuilderExpr =
DefaultValueComposer.createSimpleMessageBuilderExpr(
requestMessage, resourceNames, messageTypes);
Expand Down Expand Up @@ -702,7 +702,12 @@ private static List<Statement> composeUnaryPagedRpcMethodBodyStatements(
Map<String, Message> messageTypes) {

// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Preconditions.checkNotNull(
methodOutputMessage,
"Output message %s not found, keys: ",
method.outputType().reference().fullName(),
messageTypes.keySet().toString());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down Expand Up @@ -1128,7 +1133,7 @@ private static List<Statement> composePagedCallableBodyStatements(
VariableExpr requestVarExpr,
Map<String, Message> messageTypes) {
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName());
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
repeatedPagedResultsField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
public abstract class Message {
public abstract String name();

// The fully-qualified proto name, which differs from the Java fully-qualified name.
// For example, this would be google.showcase.v1beta1.EchoRequest for echo.proto (see testdata),
// whereas that message's Java fully-qualified name is com.google.showcase.v1beta1.EchoRequest.
public abstract String fullProtoName();

// TODO(unsupported): oneof fields are parsed as separate ones because field flattening refers to
// a specific field.
public abstract ImmutableList<Field> fields();
Expand Down Expand Up @@ -88,6 +93,8 @@ public static Builder builder() {
public abstract static class Builder {
public abstract Builder setName(String name);

public abstract Builder setFullProtoName(String fullProtoName);

public abstract Builder setFields(List<Field> fields);

public Builder setEnumValues(List<String> names, List<Integer> numbers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static Optional<List<String>> parseHttpBindings(
String subField = descendantBindings[i];
if (i < descendantBindings.length - 1) {
Field field = containingMessage.fieldMap().get(subField);
containingMessage = messageTypes.get(field.type().reference().simpleName());
containingMessage = messageTypes.get(field.type().reference().fullName());
Preconditions.checkNotNull(
containingMessage,
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static List<List<MethodArgument>> parseMethodSignatures(

Map<String, ResourceName> patternsToResourceNames =
ResourceParserHelpers.createPatternResourceNameMap(resourceNames);
Message inputMessage = messageTypes.get(methodInputType.reference().simpleName());
Message inputMessage = messageTypes.get(methodInputType.reference().fullName());

// Example from Expand in echo.proto:
// stringSigs: ["content,error", "content,error,info"].
Expand Down Expand Up @@ -266,7 +266,7 @@ private static Map<TypeNode, Field> parseTypeFromArgumentName(
TypeNode.isReferenceType(firstFieldType) && !firstFieldType.equals(TypeNode.STRING),
String.format("Field reference on %s cannot be a primitive type", firstFieldName));

String firstFieldTypeName = firstFieldType.reference().name();
String firstFieldTypeName = firstFieldType.reference().fullName();
Message firstFieldMessage = messageTypes.get(firstFieldTypeName);
Preconditions.checkNotNull(
firstFieldMessage,
Expand Down
Loading

0 comments on commit 8a5c36c

Please sign in to comment.