use of com.google.api.generator.gapic.model.ResourceReference 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.ResourceReference 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.ResourceReference in project gapic-generator-java by googleapis.
the class Parser method parseField.
private static Field parseField(FieldDescriptor fieldDescriptor, Descriptor messageDescriptor, boolean hasFieldNameConflict, Set<ResourceReference> outputResourceReferencesSeen) {
FieldOptions fieldOptions = fieldDescriptor.getOptions();
MessageOptions messageOptions = messageDescriptor.getOptions();
ResourceReference resourceReference = null;
if (fieldOptions.hasExtension(ResourceProto.resourceReference)) {
com.google.api.ResourceReference protoResourceReference = fieldOptions.getExtension(ResourceProto.resourceReference);
// Assumes only one of type or child_type is set.
String typeString = protoResourceReference.getType();
String childTypeString = protoResourceReference.getChildType();
Preconditions.checkState(!Strings.isNullOrEmpty(typeString) ^ !Strings.isNullOrEmpty(childTypeString), String.format("Exactly one of type or child_type must be set for resource_reference in field %s", fieldDescriptor.getName()));
boolean isChildType = !Strings.isNullOrEmpty(childTypeString);
resourceReference = isChildType ? ResourceReference.withChildType(childTypeString) : ResourceReference.withType(typeString);
outputResourceReferencesSeen.add(resourceReference);
} else if (messageOptions.hasExtension(ResourceProto.resource)) {
ResourceDescriptor protoResource = messageOptions.getExtension(ResourceProto.resource);
// aip.dev/4231.
String resourceFieldNameValue = ResourceNameConstants.NAME_FIELD_NAME;
if (!Strings.isNullOrEmpty(protoResource.getNameField())) {
resourceFieldNameValue = protoResource.getNameField();
}
if (fieldDescriptor.getName().equals(resourceFieldNameValue)) {
resourceReference = ResourceReference.withType(protoResource.getType());
}
}
Field.Builder fieldBuilder = Field.builder();
if (fieldDescriptor.getFile().toProto().hasSourceCodeInfo()) {
SourceCodeInfoLocation protoFieldLocation = SOURCE_CODE_INFO_PARSER.getLocation(fieldDescriptor);
if (!Objects.isNull(protoFieldLocation) && !Strings.isNullOrEmpty(protoFieldLocation.getLeadingComments())) {
fieldBuilder.setDescription(protoFieldLocation.getLeadingComments());
}
}
// Mirror protoc's name conflict resolution behavior for fields.
// For more context, trace hasFieldNameConflict back to where it gets passed in above.
String actualFieldName = hasFieldNameConflict ? fieldDescriptor.getName() + fieldDescriptor.getNumber() : fieldDescriptor.getName();
return fieldBuilder.setName(actualFieldName).setOriginalName(fieldDescriptor.getName()).setType(TypeParser.parseType(fieldDescriptor)).setIsMessage(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE).setIsEnum(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.ENUM).setIsContainedInOneof(fieldDescriptor.getContainingOneof() != null && !fieldDescriptor.getContainingOneof().isSynthetic()).setIsProto3Optional(fieldDescriptor.getContainingOneof() != null && fieldDescriptor.getContainingOneof().isSynthetic()).setIsRepeated(fieldDescriptor.isRepeated()).setIsMap(fieldDescriptor.isMapField()).setResourceReference(resourceReference).build();
}
use of com.google.api.generator.gapic.model.ResourceReference in project gapic-generator-java by googleapis.
the class Parser method parse.
public static GapicContext parse(CodeGeneratorRequest request) {
Optional<String> gapicYamlConfigPathOpt = PluginArgumentParser.parseGapicYamlConfigPath(request);
Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(gapicYamlConfigPathOpt);
Optional<List<GapicLroRetrySettings>> lroRetrySettingsOpt = GapicLroRetrySettingsParser.parse(gapicYamlConfigPathOpt);
Optional<GapicLanguageSettings> languageSettingsOpt = GapicLanguageSettingsParser.parse(gapicYamlConfigPathOpt);
Optional<String> transportOpt = PluginArgumentParser.parseTransport(request);
boolean willGenerateMetadata = PluginArgumentParser.hasMetadataFlag(request);
Optional<String> serviceConfigPathOpt = PluginArgumentParser.parseJsonConfigPath(request);
Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(serviceConfigPathOpt.orElse(null));
if (serviceConfigOpt.isPresent()) {
GapicServiceConfig serviceConfig = serviceConfigOpt.get();
serviceConfig.setLroRetrySettings(lroRetrySettingsOpt);
serviceConfig.setBatchingSettings(batchingSettingsOpt);
serviceConfig.setLanguageSettings(languageSettingsOpt);
serviceConfigOpt = Optional.of(serviceConfig);
}
Optional<String> serviceYamlConfigPathOpt = PluginArgumentParser.parseServiceYamlConfigPath(request);
Optional<com.google.api.Service> serviceYamlProtoOpt = serviceYamlConfigPathOpt.flatMap(ServiceYamlParser::parse);
// Collect the resource references seen in messages.
Set<ResourceReference> outputResourceReferencesSeen = new HashSet<>();
// Keep message and resource name parsing separate for cleaner logic.
// While this takes an extra pass through the protobufs, the extra time is relatively trivial
// and is worth the larger reduced maintenance cost.
Map<String, Message> messages = parseMessages(request, outputResourceReferencesSeen);
Map<String, ResourceName> resourceNames = parseResourceNames(request);
messages = updateResourceNamesInMessages(messages, resourceNames.values());
// Contains only resource names that are actually used. Usage refers to the presence of a
// request message's field in an RPC's method_signature annotation. That is, resource name
// definitions
// or references that are simply defined, but not used in such a manner, will not have
// corresponding Java helper
// classes generated.
Set<ResourceName> outputArgResourceNames = new HashSet<>();
List<Service> mixinServices = new ArrayList<>();
Transport transport = Transport.parse(transportOpt.orElse(Transport.GRPC.toString()));
List<Service> services = parseServices(request, messages, resourceNames, outputArgResourceNames, serviceYamlProtoOpt, serviceConfigOpt, mixinServices, transport);
Preconditions.checkState(!services.isEmpty(), "No services found to generate");
// Temporary workaround for Ads, who still need these resource names.
if (services.get(0).protoPakkage().startsWith("google.ads.googleads.v")) {
Function<ResourceName, String> typeNameFn = r -> r.resourceTypeString().substring(r.resourceTypeString().indexOf("/") + 1);
Function<Set<ResourceName>, Set<String>> typeStringSetFn = sr -> sr.stream().map(typeNameFn).collect(Collectors.toSet());
// Include all resource names present in message types for backwards-compatibility with the
// monolith. In the future, this should be removed on a client library major semver update.
// Resolve type name collisions with the ones present in the method arguments.
final Set<String> typeStringSet = typeStringSetFn.apply(outputArgResourceNames);
outputArgResourceNames.addAll(resourceNames.values().stream().filter(r -> r.hasParentMessageName() && !typeStringSet.contains(typeNameFn.apply(r))).collect(Collectors.toSet()));
String servicePackage = services.get(0).pakkage();
Map<String, ResourceName> patternsToResourceNames = ResourceParserHelpers.createPatternResourceNameMap(resourceNames);
for (ResourceReference resourceReference : outputResourceReferencesSeen) {
final Set<String> interimTypeStringSet = typeStringSetFn.apply(outputArgResourceNames);
outputArgResourceNames.addAll(ResourceReferenceParser.parseResourceNames(resourceReference, servicePackage, null, resourceNames, patternsToResourceNames).stream().filter(r -> !interimTypeStringSet.contains(typeNameFn.apply(r))).collect(Collectors.toSet()));
}
}
return GapicContext.builder().setServices(services).setMixinServices(mixinServices).setMessages(messages).setResourceNames(resourceNames).setHelperResourceNames(outputArgResourceNames).setServiceConfig(serviceConfigOpt.orElse(null)).setGapicMetadataEnabled(willGenerateMetadata).setServiceYamlProto(serviceYamlProtoOpt.orElse(null)).setTransport(transport).build();
}
use of com.google.api.generator.gapic.model.ResourceReference in project gapic-generator-java by googleapis.
the class ResourceReferenceParser method parseResourceNames.
public static List<ResourceName> parseResourceNames(ResourceReference resourceReference, String servicePackage, @Nullable String description, Map<String, ResourceName> resourceNames, Map<String, ResourceName> patternsToResourceNames) {
ResourceName resourceName = null;
if (resourceReference.isOnlyWildcard()) {
resourceName = ResourceName.createWildcard("*", "com.google.api.wildcard.placeholder");
resourceNames.put(resourceName.resourceTypeString(), resourceName);
} else {
resourceName = resourceNames.get(resourceReference.resourceTypeString());
}
// example.com/FooBar.
if (resourceReference.resourceTypeString().indexOf(SLASH) < 0) {
Optional<String> actualResourceTypeNameOpt = resourceNames.keySet().stream().filter(k -> k.substring(k.lastIndexOf(SLASH) + 1).equals(resourceReference.resourceTypeString())).findFirst();
if (actualResourceTypeNameOpt.isPresent()) {
resourceName = resourceNames.get(actualResourceTypeNameOpt.get());
}
} else {
resourceName = resourceNames.get(resourceReference.resourceTypeString());
}
Preconditions.checkNotNull(resourceName, String.format("No resource definition found for reference with type %s", resourceReference.resourceTypeString()));
if (!resourceReference.isChildType() || resourceName.isOnlyWildcard()) {
return Arrays.asList(resourceName);
}
// Create a parent ResourceName for each pattern.
List<ResourceName> parentResourceNames = new ArrayList<>();
Set<String> resourceTypeStrings = new HashSet<>();
for (String pattern : resourceName.patterns()) {
Optional<ResourceName> parentResourceNameOpt = parseParentResourceName(pattern, servicePackage, resourceName.pakkage(), resourceName.resourceTypeString(), description, patternsToResourceNames);
// Prevent duplicates.
if (parentResourceNameOpt.isPresent() && !resourceTypeStrings.contains(parentResourceNameOpt.get().resourceTypeString())) {
ResourceName parentResourceName = parentResourceNameOpt.get();
parentResourceNames.add(parentResourceName);
resourceTypeStrings.add(parentResourceName.resourceTypeString());
}
}
return parentResourceNames;
}
Aggregations