Skip to content

Commit

Permalink
[ggj][parsing][resnames] fix: support older type-only resrefs w/o a s…
Browse files Browse the repository at this point in the history
…ervice prefix (#565)

* fix: handle wildcard-typed resrefs when parsing methods

* fix: support older type-only resrefs w/o a service prefix
  • Loading branch information
miraleung authored Nov 25, 2020
1 parent 3565433 commit 0daff2f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class Parser {
private static final String COLON = ":";
private static final String DEFAULT_PORT = "443";
private static final String DOT = ".";
private static final String SLASH = "/";

private static final ResourceName WILDCARD_RESOURCE_NAME =
ResourceName.createWildcard("*", "com.google.api.wildcard.placeholder");
Expand Down Expand Up @@ -436,7 +437,21 @@ static List<Method> parseMethods(
for (Field field : inputMessage.fields()) {
if (field.hasResourceReference()) {
String resourceTypeString = field.resourceReference().resourceTypeString();
ResourceName resourceName = resourceNames.get(resourceTypeString);
ResourceName resourceName = null;
// Support older resource_references that specify only the final typename, e.g. FooBar
// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,30 @@ public static List<ResourceName> parseResourceNames(
} else {
resourceName = resourceNames.get(resourceReference.resourceTypeString());
}
resourceName = resourceNames.get(resourceReference.resourceTypeString());

// Support older resource_references that specify only the final typename, e.g. FooBar versus
// 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()));
"No resource definition found for reference with type %s",
resourceReference.resourceTypeString())
+ "\nDEL: "
+ resourceNames.keySet());
if (!resourceReference.isChildType() || resourceName.isOnlyWildcard()) {
return Arrays.asList(resourceName);
}
Expand Down

0 comments on commit 0daff2f

Please sign in to comment.