Skip to content

Commit

Permalink
[ggj][engx] fix: retrieve LRO typs withnested message tnames (#629)
Browse files Browse the repository at this point in the history
* fix: fix dep ordering in Bazel dedupe rules

* fix:  retrieve LRO typs withnested message tnames
  • Loading branch information
miraleung authored Dec 30, 2020
1 parent 767874f commit c785470
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Parser {
private static final String COMMA = ",";
Expand Down Expand Up @@ -547,17 +548,8 @@ static LongrunningOperation parseLro(
OperationInfo lroInfo =
methodDescriptor.getOptions().getExtension(OperationsProto.operationInfo);

String responseTypeName = lroInfo.getResponseType();
String responseTypePackage = "";
if (responseTypeName.contains(DOT)) {
responseTypeName = responseTypeName.substring(responseTypeName.lastIndexOf(DOT) + 1);
}

String metadataTypeName = lroInfo.getMetadataType();
if (metadataTypeName.contains(DOT)) {
metadataTypeName = metadataTypeName.substring(metadataTypeName.lastIndexOf(DOT) + 1);
}

String responseTypeName = parseNestedProtoTypeName(lroInfo.getResponseType());
String metadataTypeName = parseNestedProtoTypeName(lroInfo.getMetadataType());
Message responseMessage = messageTypes.get(responseTypeName);
Message metadataMessage = messageTypes.get(metadataTypeName);
Preconditions.checkNotNull(
Expand Down Expand Up @@ -722,4 +714,24 @@ private static String parseServiceJavaPackage(CodeGeneratorRequest request) {
!Strings.isNullOrEmpty(finalJavaPackage), "No service Java package found");
return finalJavaPackage;
}

/**
* Retrieves the nested type name from a fully-qualified protobuf type name. Example:
* google.ads.googleads.v3.resources.MutateJob.MutateJobMetadata > MutateJob.MutateJobMetadata.
*/
@VisibleForTesting
static String parseNestedProtoTypeName(String fullyQualifiedName) {
if (!fullyQualifiedName.contains(DOT)) {
return fullyQualifiedName;
}
// Find the first component in CapitalCamelCase. Assumes that proto package
// components must be in all lowercase and type names are in CapitalCamelCase.
String[] components = fullyQualifiedName.split("\\.");
List<String> nestedTypeComponents =
IntStream.range(0, components.length)
.filter(i -> Character.isUpperCase(components[i].charAt(0)))
.mapToObj(i -> components[i])
.collect(Collectors.toList());
return String.join(".", nestedTypeComponents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ public void sanitizeDefaultHost_basic() {
assertEquals(String.format("%s:443", defaultHost), Parser.sanitizeDefaultHost(defaultHost));
}

@Test
public void parseNestedProtoTypeName() {
assertEquals("MutateJobMetadata", Parser.parseNestedProtoTypeName("MutateJobMetadata"));
assertEquals(
"MutateJob.MutateJobMetadata",
Parser.parseNestedProtoTypeName("MutateJob.MutateJobMetadata"));
assertEquals(
"MutateJob.MutateJobMetadata",
Parser.parseNestedProtoTypeName(
"google.ads.googleads.v3.resources.MutateJob.MutateJobMetadata"));
}

private void assertMethodArgumentEquals(
String name, TypeNode type, List<TypeNode> nestedFields, MethodArgument argument) {
assertEquals(name, argument.name());
Expand Down

0 comments on commit c785470

Please sign in to comment.