use of com.google.api.generator.gapic.model.Field in project gapic-generator-java by googleapis.
the class MethodSignatureParser method parseTypeFromArgumentName.
private static Map<TypeNode, Field> parseTypeFromArgumentName(String argumentName, String servicePackage, Message inputMessage, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, Map<String, ResourceName> patternsToResourceNames, List<Field> argumentFieldPathAcc, Set<ResourceName> outputArgResourceNames) {
Map<TypeNode, Field> typeToField = new HashMap<>();
int dotIndex = argumentName.indexOf(DOT);
if (dotIndex < 1) {
Field field = inputMessage.fieldMap().get(argumentName);
Preconditions.checkNotNull(field, String.format("Field %s not found from input message %s values %s", argumentName, inputMessage.name(), inputMessage.fieldMap().keySet()));
if (!field.hasResourceReference()) {
typeToField.put(field.type(), field);
return typeToField;
}
// Parse the resource name tyeps.
List<ResourceName> resourceNameArgs = ResourceReferenceParser.parseResourceNames(field.resourceReference(), servicePackage, field.description(), resourceNames, patternsToResourceNames);
outputArgResourceNames.addAll(resourceNameArgs);
typeToField.put(TypeNode.STRING, field);
typeToField.putAll(resourceNameArgs.stream().collect(Collectors.toMap(r -> r.type(), r -> field.toBuilder().setResourceReference(ResourceReference.withType(r.resourceTypeString())).build())));
// Only resource name helpers should have more than one entry.
if (typeToField.size() > 1) {
typeToField.entrySet().stream().forEach(e -> {
// Skip string-only variants or ResourceName generics.
if (e.getKey().equals(TypeNode.STRING) || e.getKey().reference().name().equals("ResourceName")) {
return;
}
String resourceJavaTypeName = e.getKey().reference().name();
String resourceTypeName = e.getValue().resourceReference().resourceTypeString();
int indexOfSlash = resourceTypeName.indexOf("/");
// We assume that the corresponding Java resource name helper type (i.e. the key)
// ends in *Name. Check that it matches the expeced resource name type.
Preconditions.checkState(resourceJavaTypeName.substring(0, resourceJavaTypeName.length() - 4).equals(resourceTypeName.substring(indexOfSlash + 1)), String.format("Resource Java type %s does not correspond to proto type %s", resourceJavaTypeName, resourceTypeName));
});
}
return typeToField;
}
Preconditions.checkState(dotIndex < argumentName.length() - 1, String.format("Invalid argument name found: dot cannot be at the end of name %s", argumentName));
String firstFieldName = argumentName.substring(0, dotIndex);
String remainingArgumentName = argumentName.substring(dotIndex + 1);
// Must be a sub-message for a type's subfield to be valid.
Field firstField = inputMessage.fieldMap().get(firstFieldName);
// Validate the field into which we're descending.
Preconditions.checkState(!firstField.isRepeated(), String.format("Cannot descend into repeated field %s", firstField.name()));
TypeNode firstFieldType = firstField.type();
Preconditions.checkState(TypeNode.isReferenceType(firstFieldType) && !firstFieldType.equals(TypeNode.STRING), String.format("Field reference on %s cannot be a primitive type", firstFieldName));
String firstFieldTypeName = firstFieldType.reference().fullName();
Message firstFieldMessage = messageTypes.get(firstFieldTypeName);
Preconditions.checkNotNull(firstFieldMessage, String.format("Message type %s for field reference %s invalid", firstFieldTypeName, firstFieldName));
argumentFieldPathAcc.add(firstField);
return parseTypeFromArgumentName(remainingArgumentName, servicePackage, firstFieldMessage, messageTypes, resourceNames, patternsToResourceNames, argumentFieldPathAcc, outputArgResourceNames);
}
use of com.google.api.generator.gapic.model.Field in project gapic-generator-java by googleapis.
the class Parser method parseFields.
private static List<Field> parseFields(Descriptor messageDescriptor, Set<ResourceReference> outputResourceReferencesSeen) {
List<FieldDescriptor> fields = new ArrayList<>(messageDescriptor.getFields());
// Sort by ascending field index order. This is important for paged responses, where the first
// repeated type is taken.
fields.sort((f1, f2) -> f1.getIndex() - f2.getIndex());
// Mirror protoc's name conflict resolution behavior for fields.
// If a singular field's name equals that of a repeated field with "Count" or "List" suffixed,
// append the protobuf's field number to both fields' names.
// See:
// https://github.com/protocolbuffers/protobuf/blob/9df42757f97da9f748a464deeda96427a8f7ade0/src/google/protobuf/compiler/java/java_context.cc#L60
Map<String, Integer> repeatedFieldNamesToNumber = fields.stream().filter(f -> f.isRepeated()).collect(Collectors.toMap(f -> f.getName(), f -> f.getNumber()));
Set<Integer> fieldNumbersWithConflicts = new HashSet<>();
for (FieldDescriptor field : fields) {
Set<String> conflictingRepeatedFieldNames = repeatedFieldNamesToNumber.keySet().stream().filter(n -> field.getName().equals(n + "_count") || field.getName().equals(n + "_list")).collect(Collectors.toSet());
if (!conflictingRepeatedFieldNames.isEmpty()) {
fieldNumbersWithConflicts.addAll(conflictingRepeatedFieldNames.stream().map(n -> repeatedFieldNamesToNumber.get(n)).collect(Collectors.toSet()));
fieldNumbersWithConflicts.add(field.getNumber());
}
}
return fields.stream().map(f -> parseField(f, messageDescriptor, fieldNumbersWithConflicts.contains(f.getNumber()), outputResourceReferencesSeen)).collect(Collectors.toList());
}
use of com.google.api.generator.gapic.model.Field 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.Field in project gapic-generator-java by googleapis.
the class ParserTest method parseMessages_fieldsHaveResourceReferences.
@Test
public void parseMessages_fieldsHaveResourceReferences() {
FileDescriptor lockerServiceFileDescriptor = LockerProto.getDescriptor();
Map<String, Message> messageTypes = Parser.parseMessages(lockerServiceFileDescriptor);
// Child type.
Message message = messageTypes.get("com.google.testgapic.v1beta1.CreateFolderRequest");
Field field = message.fieldMap().get("parent");
assertTrue(field.hasResourceReference());
ResourceReference resourceReference = field.resourceReference();
assertEquals("cloudresourcemanager.googleapis.com/Folder", resourceReference.resourceTypeString());
assertTrue(resourceReference.isChildType());
// Type.
message = messageTypes.get("com.google.testgapic.v1beta1.GetFolderRequest");
field = message.fieldMap().get("name");
assertTrue(field.hasResourceReference());
resourceReference = field.resourceReference();
assertEquals("cloudresourcemanager.googleapis.com/Folder", resourceReference.resourceTypeString());
assertFalse(resourceReference.isChildType());
// Non-RPC-specific message.
message = messageTypes.get("com.google.testgapic.v1beta1.Folder");
field = message.fieldMap().get("name");
assertTrue(field.hasResourceReference());
resourceReference = field.resourceReference();
assertEquals("cloudresourcemanager.googleapis.com/Folder", resourceReference.resourceTypeString());
assertFalse(resourceReference.isChildType());
// No explicit resource_reference annotation on the field, and the resource annotation is in the
// message.
message = messageTypes.get("com.google.testgapic.v1beta1.Document");
field = message.fieldMap().get("name");
assertTrue(field.hasResourceReference());
resourceReference = field.resourceReference();
assertEquals("testgapic.googleapis.com/Document", resourceReference.resourceTypeString());
assertFalse(resourceReference.isChildType());
}
use of com.google.api.generator.gapic.model.Field in project gapic-generator-java by googleapis.
the class ParserTest method parseFields_mapType.
@Test
public void parseFields_mapType() {
FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
ServiceDescriptor testingService = testingFileDescriptor.getServices().get(0);
assertEquals(testingService.getName(), "Testing");
Map<String, Message> messageTypes = Parser.parseMessages(testingFileDescriptor);
Message message = messageTypes.get("com.google.showcase.v1beta1.Session");
Field field = message.fieldMap().get("session_ids_to_descriptor");
assertEquals(TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(TypeNode.INT_OBJECT.reference(), TypeNode.STRING.reference())).build()), field.type());
}
Aggregations