use of com.google.api.generator.gapic.model.Message in project gapic-generator-java by googleapis.
the class Parser method parsePageSizeFieldName.
@VisibleForTesting
static String parsePageSizeFieldName(MethodDescriptor methodDescriptor, Map<String, Message> messageTypes, Transport transport) {
TypeNode inputMessageType = TypeParser.parseType(methodDescriptor.getInputType());
TypeNode outputMessageType = TypeParser.parseType(methodDescriptor.getOutputType());
Message inputMessage = messageTypes.get(inputMessageType.reference().fullName());
Message outputMessage = messageTypes.get(outputMessageType.reference().fullName());
// This should technically handle the absence of either of these fields (aip.dev/158), but we
// gate on their collective presence to ensure the generated surface is backawrds-compatible
// with monolith-gnerated libraries.
String pagedFieldName = null;
if (inputMessage != null && inputMessage.fieldMap().containsKey("page_token") && outputMessage != null && outputMessage.fieldMap().containsKey("next_page_token")) {
// List of potential field names representing page size.
// page_size gets priority over max_results if both are present
List<String> fieldNames = new ArrayList<>();
fieldNames.add("page_size");
if (transport == Transport.REST) {
fieldNames.add("max_results");
}
for (String fieldName : fieldNames) {
if (pagedFieldName == null && inputMessage.fieldMap().containsKey(fieldName)) {
pagedFieldName = fieldName;
}
}
}
return pagedFieldName;
}
use of com.google.api.generator.gapic.model.Message in project gapic-generator-java by googleapis.
the class Parser method parseMessages.
private static Map<String, Message> parseMessages(Descriptor messageDescriptor, Set<ResourceReference> outputResourceReferencesSeen, List<String> outerNestedTypes) {
Map<String, Message> messages = new HashMap<>();
String messageName = messageDescriptor.getName();
if (!messageDescriptor.getNestedTypes().isEmpty()) {
for (Descriptor nestedMessage : messageDescriptor.getNestedTypes()) {
if (isMapType(nestedMessage)) {
continue;
}
List<String> currentNestedTypes = new ArrayList<>(outerNestedTypes);
currentNestedTypes.add(messageName);
messages.putAll(parseMessages(nestedMessage, outputResourceReferencesSeen, currentNestedTypes));
}
}
TypeNode messageType = TypeParser.parseType(messageDescriptor);
List<FieldDescriptor> fields = messageDescriptor.getFields();
HashMap<String, String> operationRequestFields = new HashMap<String, String>();
BiMap<String, String> operationResponseFields = HashBiMap.create();
OperationResponse.Builder operationResponse = null;
for (FieldDescriptor fd : fields) {
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationRequestField)) {
String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationRequestField);
operationRequestFields.put(orf, fd.getName());
}
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationResponseField)) {
String orf = fd.getOptions().getExtension(ExtendedOperationsProto.operationResponseField);
operationResponseFields.put(orf, fd.getName());
}
if (fd.getOptions().hasExtension(ExtendedOperationsProto.operationField)) {
OperationResponseMapping orm = fd.getOptions().getExtension(ExtendedOperationsProto.operationField);
if (operationResponse == null) {
operationResponse = OperationResponse.builder();
}
if (orm.equals(OperationResponseMapping.NAME)) {
operationResponse.setNameFieldName(fd.getName());
} else if (orm.equals(OperationResponseMapping.STATUS)) {
operationResponse.setStatusFieldName(fd.getName());
operationResponse.setStatusFieldTypeName(fd.toProto().getTypeName());
} else if (orm.equals(OperationResponseMapping.ERROR_CODE)) {
operationResponse.setErrorCodeFieldName(fd.getName());
} else if (orm.equals(OperationResponseMapping.ERROR_MESSAGE)) {
operationResponse.setErrorMessageFieldName(fd.getName());
}
}
}
messages.put(messageType.reference().fullName(), Message.builder().setType(messageType).setName(messageName).setFullProtoName(messageDescriptor.getFullName()).setFields(parseFields(messageDescriptor, outputResourceReferencesSeen)).setOuterNestedTypes(outerNestedTypes).setOperationRequestFields(operationRequestFields).setOperationResponseFields(operationResponseFields).setOperationResponse(operationResponse != null ? operationResponse.build() : null).build());
return messages;
}
use of com.google.api.generator.gapic.model.Message in project gapic-generator-java by googleapis.
the class Parser method parseMessages.
public static Map<String, Message> parseMessages(FileDescriptor fileDescriptor, Set<ResourceReference> outputResourceReferencesSeen) {
// TODO(miraleung): Preserve nested type and package data in the type key.
Map<String, Message> messages = new HashMap<>();
for (Descriptor messageDescriptor : fileDescriptor.getMessageTypes()) {
messages.putAll(parseMessages(messageDescriptor, outputResourceReferencesSeen));
}
// We treat enums as messages since we primarily care only about the type representation.
for (EnumDescriptor enumDescriptor : fileDescriptor.getEnumTypes()) {
String name = enumDescriptor.getName();
List<EnumValueDescriptor> valueDescriptors = enumDescriptor.getValues();
TypeNode enumType = TypeParser.parseType(enumDescriptor);
messages.put(enumType.reference().fullName(), Message.builder().setType(enumType).setName(name).setFullProtoName(enumDescriptor.getFullName()).setEnumValues(valueDescriptors.stream().map(v -> v.getName()).collect(Collectors.toList()), valueDescriptors.stream().map(v -> v.getNumber()).collect(Collectors.toList())).build());
}
return messages;
}
use of com.google.api.generator.gapic.model.Message in project gapic-generator-java by googleapis.
the class Parser method parseMethods.
@VisibleForTesting
static List<Method> parseMethods(ServiceDescriptor serviceDescriptor, String servicePackage, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, Optional<GapicServiceConfig> serviceConfigOpt, Set<ResourceName> outputArgResourceNames, Transport transport) {
List<Method> methods = new ArrayList<>();
for (MethodDescriptor protoMethod : serviceDescriptor.getMethods()) {
// Parse the method.
TypeNode inputType = TypeParser.parseType(protoMethod.getInputType());
Method.Builder methodBuilder = Method.builder();
if (protoMethod.getFile().toProto().hasSourceCodeInfo()) {
SourceCodeInfoLocation protoMethodLocation = SOURCE_CODE_INFO_PARSER.getLocation(protoMethod);
if (!Objects.isNull(protoMethodLocation) && !Strings.isNullOrEmpty(protoMethodLocation.getLeadingComments())) {
methodBuilder.setDescription(protoMethodLocation.getLeadingComments());
}
}
boolean isDeprecated = false;
if (protoMethod.getOptions().hasDeprecated()) {
isDeprecated = protoMethod.getOptions().getDeprecated();
}
Message inputMessage = messageTypes.get(inputType.reference().fullName());
Preconditions.checkNotNull(inputMessage, String.format("No message found for %s", inputType.reference().fullName()));
HttpBindings httpBindings = HttpRuleParser.parse(protoMethod, inputMessage, messageTypes);
boolean isBatching = !serviceConfigOpt.isPresent() ? false : serviceConfigOpt.get().hasBatchingSetting(/* protoPakkage */
protoMethod.getFile().getPackage(), serviceDescriptor.getName(), protoMethod.getName());
boolean operationPollingMethod = protoMethod.getOptions().hasExtension(ExtendedOperationsProto.operationPollingMethod) ? protoMethod.getOptions().getExtension(ExtendedOperationsProto.operationPollingMethod) : false;
RoutingHeaderRule routingHeaderRule = RoutingRuleParser.parse(protoMethod, inputMessage, messageTypes);
methods.add(methodBuilder.setName(protoMethod.getName()).setInputType(inputType).setOutputType(TypeParser.parseType(protoMethod.getOutputType())).setStream(Method.toStream(protoMethod.isClientStreaming(), protoMethod.isServerStreaming())).setLro(parseLro(servicePackage, protoMethod, messageTypes)).setMethodSignatures(MethodSignatureParser.parseMethodSignatures(protoMethod, servicePackage, inputType, messageTypes, resourceNames, outputArgResourceNames)).setHttpBindings(httpBindings).setRoutingHeaderRule(routingHeaderRule).setIsBatching(isBatching).setPageSizeFieldName(parsePageSizeFieldName(protoMethod, messageTypes, transport)).setIsDeprecated(isDeprecated).setOperationPollingMethod(operationPollingMethod).build());
// Any input type that has a resource reference will need a resource name helper class.
for (Field field : inputMessage.fields()) {
if (field.hasResourceReference()) {
String resourceTypeString = field.resourceReference().resourceTypeString();
ResourceName resourceName = null;
// versus example.com/FooBar.
if (resourceTypeString.indexOf(SLASH) < 0) {
Optional<String> actualResourceTypeNameOpt = resourceNames.keySet().stream().filter(k -> k.substring(k.lastIndexOf(SLASH) + 1).equals(resourceTypeString)).findFirst();
if (actualResourceTypeNameOpt.isPresent()) {
resourceName = resourceNames.get(actualResourceTypeNameOpt.get());
}
} else {
resourceName = resourceNames.get(resourceTypeString);
}
if (ResourceNameConstants.WILDCARD_PATTERN.equals(resourceTypeString)) {
resourceName = WILDCARD_RESOURCE_NAME;
} else {
Preconditions.checkNotNull(resourceName, String.format("Resource name %s not found; parsing field %s in message %s in method %s", resourceTypeString, field.name(), inputMessage.name(), protoMethod.getName()));
}
outputArgResourceNames.add(resourceName);
}
}
}
return methods;
}
use of com.google.api.generator.gapic.model.Message in project gapic-generator-java by googleapis.
the class ParserTest method parseMethodSignatures_validArgstAndEmptyString.
@Test
public void parseMethodSignatures_validArgstAndEmptyString() {
// TODO(miraleung): Move this to MethodSignatureParserTest.
MethodDescriptor methodDescriptor = echoService.getMethods().get(0);
assertEquals("Echo", methodDescriptor.getName());
TypeNode inputType = TypeParser.parseType(methodDescriptor.getInputType());
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
List<List<MethodArgument>> methodArgs = MethodSignatureParser.parseMethodSignatures(methodDescriptor, ECHO_PACKAGE, inputType, messageTypes, resourceNames, outputResourceNames);
assertEquals(Collections.emptyList(), methodArgs.get(0));
assertEquals(1, methodArgs.get(1).size());
assertEquals("parent", methodArgs.get(1).get(0).name());
}
Aggregations