Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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