|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.api.generator.gapic.composer.samplecode; |
| 16 | + |
| 17 | +import com.google.api.generator.engine.ast.AssignmentExpr; |
| 18 | +import com.google.api.generator.engine.ast.MethodInvocationExpr; |
| 19 | +import com.google.api.generator.engine.ast.TypeNode; |
| 20 | +import com.google.api.generator.engine.ast.VariableExpr; |
| 21 | +import com.google.api.generator.gapic.model.MethodArgument; |
| 22 | +import com.google.api.generator.gapic.model.ResourceName; |
| 23 | +import com.google.api.generator.gapic.model.Sample; |
| 24 | +import com.google.api.generator.gapic.utils.JavaStyle; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +public class SampleComposerUtil { |
| 30 | + // Assign client variable expr with create client. |
| 31 | + // e.g EchoClient echoClient = EchoClient.create() |
| 32 | + static AssignmentExpr assignClientVariableWithCreateMethodExpr(VariableExpr clientVarExpr) { |
| 33 | + return AssignmentExpr.builder() |
| 34 | + .setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()) |
| 35 | + .setValueExpr( |
| 36 | + MethodInvocationExpr.builder() |
| 37 | + .setStaticReferenceType(clientVarExpr.variable().type()) |
| 38 | + .setReturnType(clientVarExpr.variable().type()) |
| 39 | + .setMethodName("create") |
| 40 | + .build()) |
| 41 | + .build(); |
| 42 | + } |
| 43 | + |
| 44 | + static boolean isStringTypedResourceName( |
| 45 | + MethodArgument arg, Map<String, ResourceName> resourceNames) { |
| 46 | + return arg.type().equals(TypeNode.STRING) |
| 47 | + && arg.field().hasResourceReference() |
| 48 | + && resourceNames.containsKey(arg.field().resourceReference().resourceTypeString()); |
| 49 | + } |
| 50 | + |
| 51 | + static String createOverloadDisambiguation(List<VariableExpr> methodArgVarExprs) { |
| 52 | + if (methodArgVarExprs.isEmpty()) { |
| 53 | + return "Noargs"; |
| 54 | + } |
| 55 | + return methodArgVarExprs.stream() |
| 56 | + .map( |
| 57 | + arg -> |
| 58 | + JavaStyle.toUpperCamelCase( |
| 59 | + arg.variable().type().reference() == null |
| 60 | + ? arg.variable().type().typeKind().name().toLowerCase() |
| 61 | + : arg.variable().type().reference().name().toLowerCase())) |
| 62 | + .collect(Collectors.joining()); |
| 63 | + } |
| 64 | + |
| 65 | + public static List<Sample> handleDuplicateSamples(List<Sample> samples) { |
| 66 | + // grab all distinct samples and group by sample name |
| 67 | + // ie: { "echo", ["echo(request"], |
| 68 | + // "echoString", ["echo(parent)", "echo(child)", "echo(context)"], |
| 69 | + // "echoDelete", ["echo.delete(request)"] } |
| 70 | + Map<String, List<Sample>> distinctSamplesGroupedByName = |
| 71 | + samples.stream().distinct().collect(Collectors.groupingBy(s -> s.name())); |
| 72 | + |
| 73 | + // collect samples that don't have duplicates |
| 74 | + // ie: ["echo", "echoDelete"] |
| 75 | + List<Sample> uniqueSamples = |
| 76 | + distinctSamplesGroupedByName.entrySet().stream() |
| 77 | + .filter(entry -> entry.getValue().size() < 2) |
| 78 | + .map(entry -> entry.getValue().get(0)) |
| 79 | + .collect(Collectors.toList()); |
| 80 | + |
| 81 | + if (uniqueSamples.size() == distinctSamplesGroupedByName.size()) { |
| 82 | + return uniqueSamples; |
| 83 | + } |
| 84 | + |
| 85 | + // collect samples that do have duplicates |
| 86 | + // ie: ["echoString"] |
| 87 | + List<Map.Entry<String, List<Sample>>> duplicateDistinctSamples = |
| 88 | + distinctSamplesGroupedByName.entrySet().stream() |
| 89 | + .filter(entry -> entry.getValue().size() > 1) |
| 90 | + .collect(Collectors.toList()); |
| 91 | + |
| 92 | + // update similar samples regionTag/name so filesname/regiontag are unique |
| 93 | + // ie: ["echo", "echoDelete", "echoString", "echoString1"] |
| 94 | + for (Map.Entry<String, List<Sample>> entry : duplicateDistinctSamples) { |
| 95 | + int sampleNum = 0; |
| 96 | + for (Sample sample : entry.getValue()) { |
| 97 | + Sample uniqueSample = sample; |
| 98 | + // first sample will be "canonical", not updating name |
| 99 | + if (sampleNum != 0) { |
| 100 | + uniqueSample = |
| 101 | + sample.withRegionTag( |
| 102 | + sample |
| 103 | + .regionTag() |
| 104 | + .withOverloadDisambiguation( |
| 105 | + sample.regionTag().overloadDisambiguation() + sampleNum)); |
| 106 | + } |
| 107 | + uniqueSamples.add(uniqueSample); |
| 108 | + sampleNum++; |
| 109 | + } |
| 110 | + } |
| 111 | + return uniqueSamples; |
| 112 | + } |
| 113 | +} |
0 commit comments