-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Use parent type instead of child_type in method doc sample #862
Changes from all commits
49f0b39
8c59769
7578d45
c9f0b07
9350266
a4543a7
bde778e
8c8ab88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import com.google.api.generator.gapic.model.ResourceName; | ||
import com.google.api.generator.gapic.utils.JavaStyle; | ||
import com.google.api.generator.gapic.utils.ResourceNameConstants; | ||
import com.google.api.generator.gapic.utils.ResourceReferenceUtils; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import com.google.longrunning.Operation; | ||
|
@@ -48,6 +49,7 @@ | |
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class DefaultValueComposer { | ||
|
@@ -76,6 +78,7 @@ public static Expr createDefaultValue( | |
Expr defValue = | ||
createDefaultValue( | ||
resourceName, | ||
methodArg.field().resourceReference().isChildType(), | ||
resourceNames.values().stream().collect(Collectors.toList()), | ||
methodArg.field().name()); | ||
|
||
|
@@ -175,17 +178,46 @@ static Expr createDefaultValue(Field f, boolean useExplicitInitTypeInGenerics) { | |
} | ||
|
||
public static Expr createDefaultValue( | ||
ResourceName resourceName, List<ResourceName> resnames, String fieldOrMessageName) { | ||
return createDefaultValueResourceHelper(resourceName, resnames, fieldOrMessageName, true); | ||
ResourceName resourceName, | ||
boolean isChildType, | ||
List<ResourceName> resnames, | ||
String fieldOrMessageName) { | ||
return createDefaultValueResourceHelper( | ||
resourceName, isChildType, resnames, fieldOrMessageName, true); | ||
} | ||
|
||
private static Optional<ResourceName> findParentResource( | ||
ResourceName childResource, List<ResourceName> resourceNames) { | ||
Map<String, ResourceName> patternToResourceName = new HashMap<>(); | ||
|
||
for (ResourceName resourceName : resourceNames) { | ||
for (String parentPattern : resourceName.patterns()) { | ||
patternToResourceName.put(parentPattern, resourceName); | ||
} | ||
} | ||
|
||
for (String childPattern : childResource.patterns()) { | ||
Optional<String> parentPattern = ResourceReferenceUtils.parseParentPattern(childPattern); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *Please see the other comment in I could be missing some more complicated interactions, but at first glance: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell, |
||
if (parentPattern.isPresent() && patternToResourceName.containsKey(parentPattern.get())) { | ||
return Optional.of(patternToResourceName.get(parentPattern.get())); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
@VisibleForTesting | ||
static Expr createDefaultValueResourceHelper( | ||
ResourceName resourceName, | ||
boolean isChildType, | ||
List<ResourceName> resnames, | ||
String fieldOrMessageName, | ||
boolean allowAnonResourceNameClass) { | ||
|
||
if (isChildType) { | ||
resourceName = findParentResource(resourceName, resnames).orElse(resourceName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Do you think we should fail instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, just wanted to make sure I understood it right. |
||
} | ||
|
||
boolean hasOnePattern = resourceName.patterns().size() == 1; | ||
if (resourceName.isOnlyWildcard()) { | ||
List<ResourceName> unexaminedResnames = new ArrayList<>(resnames); | ||
|
@@ -195,7 +227,7 @@ static Expr createDefaultValueResourceHelper( | |
continue; | ||
} | ||
unexaminedResnames.remove(resname); | ||
return createDefaultValue(resname, unexaminedResnames, fieldOrMessageName); | ||
return createDefaultValue(resname, false, unexaminedResnames, fieldOrMessageName); | ||
} | ||
|
||
if (unexaminedResnames.isEmpty()) { | ||
|
@@ -283,6 +315,7 @@ public static Expr createSimpleMessageBuilderExpr( | |
defaultExpr = | ||
createDefaultValueResourceHelper( | ||
resourceNames.get(field.resourceReference().resourceTypeString()), | ||
field.resourceReference().isChildType(), | ||
resourceNames.values().stream().collect(Collectors.toList()), | ||
message.name(), | ||
/* allowAnonResourceNameClass = */ false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.gapic.utils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
public final class ResourceReferenceUtils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tl;dr; can we keep this in parser class? It seems we can avoid creating a new util class for this: WDYT about keeping the parser method in parser class, but make the parent-child resolution logic persisted in ResourceName class (or another/new data structure returned by the parser class) and make the parser class precompute everything only once. If parent-child resolutin is not a property of a resource name but a property of an rpc method using that resource name, then it is not clear why that logic is still called in resource name parser class. |
||
|
||
private static final String SLASH = "/"; | ||
|
||
private ResourceReferenceUtils() {} | ||
|
||
public static Optional<String> parseParentPattern(String pattern) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic was there before, but it does not look accurate, as it makes many different assumption, which may be false. for examle, if there is a pattern
Instead this logic always returns only one candidate. Not sure how many (if any) practical cases we have which the current implementation would not satisfy. I guess it is ok to keep the implementaiton as is for now, but maybe put a comment/request for refactoring it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's true, but according to https://google.aip.dev/122, this example is not a recommended pattern.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, but if it was guaranteed they would phrase it as "must" in the AIP. I think it is ok to keep the implementation as is, since it is beyond the scope of this PR, just maybe add a comment about it. |
||
String[] tokens = pattern.split(SLASH); | ||
String lastToken = tokens[tokens.length - 1]; | ||
if (lastToken.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL) | ||
|| lastToken.equals(ResourceNameConstants.WILDCARD_PATTERN)) { | ||
return Optional.empty(); | ||
} | ||
|
||
int lastTokenIndex = tokens.length - 2; | ||
int minLengthWithParent = 4; | ||
// Singleton patterns, e.g. projects/{project}/agent. | ||
if (!lastToken.contains("{")) { | ||
minLengthWithParent = 3; | ||
lastTokenIndex = tokens.length - 1; | ||
} | ||
|
||
// No fully-formed parent. Expected: ancestors/{ancestor}/childNodes/{child_node}. | ||
if (tokens.length < minLengthWithParent) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(String.join(SLASH, Arrays.asList(tokens).subList(0, lastTokenIndex))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this whole thing be included in ResourceReferenceParser class? This seems like a general resource names resolution logic, not necessarily specific to sample generation.