use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ResourceNameParser method parseResourceNamesFromMessages.
@VisibleForTesting
static Map<String, ResourceName> parseResourceNamesFromMessages(List<Descriptor> messageTypeDescriptors, String pakkage) {
Map<String, ResourceName> resourceNames = new HashMap<>();
for (Descriptor messageTypeDescriptor : messageTypeDescriptors) {
Optional<ResourceName> resourceNameModelOpt = parseResourceNameFromMessageType(messageTypeDescriptor, pakkage);
if (resourceNameModelOpt.isPresent()) {
ResourceName resourceName = resourceNameModelOpt.get();
resourceNames.put(resourceName.resourceTypeString(), resourceName);
}
}
return resourceNames;
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ResourceNameParser method parseResourceNamesFromFile.
@VisibleForTesting
static Map<String, ResourceName> parseResourceNamesFromFile(FileDescriptor fileDescriptor, String javaPackage) {
Map<String, ResourceName> typeStringToResourceNames = new HashMap<>();
FileOptions fileOptions = fileDescriptor.getOptions();
if (fileOptions.getExtensionCount(ResourceProto.resourceDefinition) <= 0) {
return typeStringToResourceNames;
}
List<ResourceDescriptor> protoResources = fileOptions.getExtension(ResourceProto.resourceDefinition);
for (ResourceDescriptor protoResource : protoResources) {
Optional<ResourceName> resourceNameModelOpt = createResourceName(protoResource, javaPackage);
if (!resourceNameModelOpt.isPresent()) {
continue;
}
ResourceName resourceNameModel = resourceNameModelOpt.get();
// Clobber anything if we're creating a new ResourceName from a proto.
typeStringToResourceNames.put(resourceNameModel.resourceTypeString(), resourceNameModel);
}
return typeStringToResourceNames;
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ResourceReferenceParser method parseParentResourceName.
@VisibleForTesting
static Optional<ResourceName> parseParentResourceName(String pattern, String servicePackage, String resourcePackage, String resourceTypeString, @Nullable String description, Map<String, ResourceName> patternsToResourceNames) {
Optional<String> parentPatternOpt = ResourceReferenceUtils.parseParentPattern(pattern);
if (!parentPatternOpt.isPresent()) {
return Optional.empty();
}
String parentPattern = parentPatternOpt.get();
if (patternsToResourceNames.get(parentPattern) != null) {
return Optional.of(patternsToResourceNames.get(parentPattern));
}
String[] tokens = parentPattern.split(SLASH);
int numTokens = tokens.length;
String lastToken = tokens[numTokens - 1];
Set<String> variableNames = PathTemplate.create(parentPattern).vars();
String parentVariableName = null;
// E.g. Profile is the parent of users/{user}/profiles/{profile}/blurbs/{blurb}.
for (String variableName : variableNames) {
if (lastToken.contains(variableName)) {
parentVariableName = variableName;
}
}
// That is, they will not appear in the parent components under consideration.
if (Strings.isNullOrEmpty(parentVariableName)) {
String lowerTypeName = resourceTypeString.substring(resourceTypeString.indexOf(SLASH) + 1).toLowerCase();
// We're curerntly at users/{user}/profile/blurbs.
if ((lastToken.endsWith("s") || lastToken.contains(lowerTypeName)) && numTokens > 2) {
// Not the singleton we're looking for, back up.
parentVariableName = tokens[numTokens - 2];
} else {
// Check for the parent of users/{user}/profile/blurbs/{blurb}.
// We're curerntly at users/{user}/profile.
parentVariableName = lastToken;
}
parentVariableName = parentVariableName.replace(LEFT_BRACE, EMPTY_STRING).replace(RIGHT_BRACE, EMPTY_STRING);
}
Preconditions.checkNotNull(parentVariableName, String.format("Could not parse variable name from pattern %s", parentPattern));
// Use the package where the resource was defined, only if that is a sub-package of the
// current service (which is assumed to be the project's package).
String pakkage = resolvePackages(resourcePackage, servicePackage);
String parentResourceTypeString = String.format("%s/%s", resourceTypeString.substring(0, resourceTypeString.indexOf(SLASH)), JavaStyle.toUpperCamelCase(parentVariableName));
ResourceName parentResourceName = ResourceName.builder().setVariableName(parentVariableName).setPakkage(pakkage).setResourceTypeString(parentResourceTypeString).setPatterns(Arrays.asList(parentPattern)).setDescription(description).build();
patternsToResourceNames.put(parentPattern, parentResourceName);
return Optional.of(parentResourceName);
}
use of com.google.api.generator.gapic.model.ResourceName 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;
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method invalid_composeStreamCallableMethod_bidiStreamNotExistRequest.
@Test
public void invalid_composeStreamCallableMethod_bidiStreamNotExistRequest() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("NotExistRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("chat").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.BIDI).build();
Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, clientType, resourceNames, messageTypes));
}
Aggregations