-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inline disclaimer comment, use Sample/RegionTag, collect GapicC…
…lass samples (pt 2) (#970) * chore: rename files * feat: prepare for writing samples, include comment in inline samples * chore: update test naming * test: refactor grpc golden unit tests include samples * test: integration goldens * test: unit goldens * nit refactor * formatting * refactor: move isProtoEmptyType, move handleDuplicateSamples, update sample name/file name * fix - update unit goldens dir with lowercase dir * test: SampleComposerUtilTest included * fix: include header
- Loading branch information
Showing
227 changed files
with
9,064 additions
and
1,597 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 91 additions & 47 deletions
138
...va/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/com/google/api/generator/gapic/composer/samplecode/SampleComposerUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2022 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.composer.samplecode; | ||
|
||
import com.google.api.generator.engine.ast.AssignmentExpr; | ||
import com.google.api.generator.engine.ast.MethodInvocationExpr; | ||
import com.google.api.generator.engine.ast.TypeNode; | ||
import com.google.api.generator.engine.ast.VariableExpr; | ||
import com.google.api.generator.gapic.model.MethodArgument; | ||
import com.google.api.generator.gapic.model.ResourceName; | ||
import com.google.api.generator.gapic.model.Sample; | ||
import com.google.api.generator.gapic.utils.JavaStyle; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class SampleComposerUtil { | ||
// Assign client variable expr with create client. | ||
// e.g EchoClient echoClient = EchoClient.create() | ||
static AssignmentExpr assignClientVariableWithCreateMethodExpr(VariableExpr clientVarExpr) { | ||
return AssignmentExpr.builder() | ||
.setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()) | ||
.setValueExpr( | ||
MethodInvocationExpr.builder() | ||
.setStaticReferenceType(clientVarExpr.variable().type()) | ||
.setReturnType(clientVarExpr.variable().type()) | ||
.setMethodName("create") | ||
.build()) | ||
.build(); | ||
} | ||
|
||
static boolean isStringTypedResourceName( | ||
MethodArgument arg, Map<String, ResourceName> resourceNames) { | ||
return arg.type().equals(TypeNode.STRING) | ||
&& arg.field().hasResourceReference() | ||
&& resourceNames.containsKey(arg.field().resourceReference().resourceTypeString()); | ||
} | ||
|
||
static String createOverloadDisambiguation(List<VariableExpr> methodArgVarExprs) { | ||
if (methodArgVarExprs.isEmpty()) { | ||
return "Noargs"; | ||
} | ||
return methodArgVarExprs.stream() | ||
.map( | ||
arg -> | ||
JavaStyle.toUpperCamelCase( | ||
arg.variable().type().reference() == null | ||
? arg.variable().type().typeKind().name().toLowerCase() | ||
: arg.variable().type().reference().name().toLowerCase())) | ||
.collect(Collectors.joining()); | ||
} | ||
|
||
public static List<Sample> handleDuplicateSamples(List<Sample> samples) { | ||
// grab all distinct samples and group by sample name | ||
// ie: { "echo", ["echo(request"], | ||
// "echoString", ["echo(parent)", "echo(child)", "echo(context)"], | ||
// "echoDelete", ["echo.delete(request)"] } | ||
Map<String, List<Sample>> distinctSamplesGroupedByName = | ||
samples.stream().distinct().collect(Collectors.groupingBy(s -> s.name())); | ||
|
||
// collect samples that don't have duplicates | ||
// ie: ["echo", "echoDelete"] | ||
List<Sample> uniqueSamples = | ||
distinctSamplesGroupedByName.entrySet().stream() | ||
.filter(entry -> entry.getValue().size() < 2) | ||
.map(entry -> entry.getValue().get(0)) | ||
.collect(Collectors.toList()); | ||
|
||
if (uniqueSamples.size() == distinctSamplesGroupedByName.size()) { | ||
return uniqueSamples; | ||
} | ||
|
||
// collect samples that do have duplicates | ||
// ie: ["echoString"] | ||
List<Map.Entry<String, List<Sample>>> duplicateDistinctSamples = | ||
distinctSamplesGroupedByName.entrySet().stream() | ||
.filter(entry -> entry.getValue().size() > 1) | ||
.collect(Collectors.toList()); | ||
|
||
// update similar samples regionTag/name so filesname/regiontag are unique | ||
// ie: ["echo", "echoDelete", "echoString", "echoString1"] | ||
for (Map.Entry<String, List<Sample>> entry : duplicateDistinctSamples) { | ||
int sampleNum = 0; | ||
for (Sample sample : entry.getValue()) { | ||
Sample uniqueSample = sample; | ||
// first sample will be "canonical", not updating name | ||
if (sampleNum != 0) { | ||
uniqueSample = | ||
sample.withRegionTag( | ||
sample | ||
.regionTag() | ||
.withOverloadDisambiguation( | ||
sample.regionTag().overloadDisambiguation() + sampleNum)); | ||
} | ||
uniqueSamples.add(uniqueSample); | ||
sampleNum++; | ||
} | ||
} | ||
return uniqueSamples; | ||
} | ||
} |
Oops, something went wrong.