|
| 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.ClassDefinition; |
| 19 | +import com.google.api.generator.engine.ast.CommentStatement; |
| 20 | +import com.google.api.generator.engine.ast.Expr; |
| 21 | +import com.google.api.generator.engine.ast.ExprStatement; |
| 22 | +import com.google.api.generator.engine.ast.MethodDefinition; |
| 23 | +import com.google.api.generator.engine.ast.MethodInvocationExpr; |
| 24 | +import com.google.api.generator.engine.ast.ScopeNode; |
| 25 | +import com.google.api.generator.engine.ast.Statement; |
| 26 | +import com.google.api.generator.engine.ast.TypeNode; |
| 27 | +import com.google.api.generator.engine.ast.Variable; |
| 28 | +import com.google.api.generator.engine.ast.VariableExpr; |
| 29 | +import com.google.api.generator.gapic.composer.comment.CommentComposer; |
| 30 | +import com.google.api.generator.gapic.model.RegionTag; |
| 31 | +import com.google.api.generator.gapic.model.Sample; |
| 32 | +import com.google.api.generator.gapic.utils.JavaStyle; |
| 33 | +import com.google.common.collect.ImmutableList; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.List; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +public class SampleComposer { |
| 40 | + static List<Statement> composeInlineSample(List<Statement> sampleBody) { |
| 41 | + return bodyWithComment(CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT, sampleBody); |
| 42 | + } |
| 43 | + |
| 44 | + // "Executable" meaning it includes the necessary code to execute java code, |
| 45 | + // still may require additional configuration to actually execute generated sample code |
| 46 | + static ClassDefinition composeExecutableSample(Sample sample, String pakkage) { |
| 47 | + return createExecutableSample( |
| 48 | + sample.fileHeader(), |
| 49 | + pakkage, |
| 50 | + sample.name(), |
| 51 | + sample.variableAssignments(), |
| 52 | + bodyWithComment(CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT, sample.body()), |
| 53 | + sample.regionTag()); |
| 54 | + } |
| 55 | + |
| 56 | + private static List<Statement> bodyWithComment( |
| 57 | + List<Statement> autoGeneratedComment, List<Statement> sampleBody) { |
| 58 | + List<Statement> bodyWithComment = new ArrayList<>(autoGeneratedComment); |
| 59 | + bodyWithComment.addAll(sampleBody); |
| 60 | + return bodyWithComment; |
| 61 | + } |
| 62 | + |
| 63 | + private static ClassDefinition createExecutableSample( |
| 64 | + List<CommentStatement> fileHeader, |
| 65 | + String packageName, |
| 66 | + String sampleClassName, |
| 67 | + List<AssignmentExpr> sampleVariableAssignments, |
| 68 | + List<Statement> sampleBody, |
| 69 | + RegionTag regionTag) { |
| 70 | + |
| 71 | + String sampleMethodName = JavaStyle.toLowerCamelCase(sampleClassName); |
| 72 | + List<VariableExpr> sampleMethodArgs = composeSampleMethodArgs(sampleVariableAssignments); |
| 73 | + MethodDefinition mainMethod = |
| 74 | + composeMainMethod( |
| 75 | + composeMainBody( |
| 76 | + sampleVariableAssignments, |
| 77 | + composeInvokeMethodStatement(sampleMethodName, sampleMethodArgs))); |
| 78 | + MethodDefinition sampleMethod = |
| 79 | + composeSampleMethod(sampleMethodName, sampleMethodArgs, sampleBody); |
| 80 | + return composeSampleClass( |
| 81 | + fileHeader, packageName, sampleClassName, mainMethod, sampleMethod, regionTag); |
| 82 | + } |
| 83 | + |
| 84 | + private static List<VariableExpr> composeSampleMethodArgs( |
| 85 | + List<AssignmentExpr> sampleVariableAssignments) { |
| 86 | + return sampleVariableAssignments.stream() |
| 87 | + .map(v -> v.variableExpr().toBuilder().setIsDecl(true).build()) |
| 88 | + .collect(Collectors.toList()); |
| 89 | + } |
| 90 | + |
| 91 | + private static Statement composeInvokeMethodStatement( |
| 92 | + String sampleMethodName, List<VariableExpr> sampleMethodArgs) { |
| 93 | + List<Expr> invokeArgs = |
| 94 | + sampleMethodArgs.stream() |
| 95 | + .map(arg -> arg.toBuilder().setIsDecl(false).build()) |
| 96 | + .collect(Collectors.toList()); |
| 97 | + return ExprStatement.withExpr( |
| 98 | + MethodInvocationExpr.builder() |
| 99 | + .setMethodName(sampleMethodName) |
| 100 | + .setArguments(invokeArgs) |
| 101 | + .build()); |
| 102 | + } |
| 103 | + |
| 104 | + private static List<Statement> composeMainBody( |
| 105 | + List<AssignmentExpr> sampleVariableAssignments, Statement invokeMethod) { |
| 106 | + List<ExprStatement> setVariables = |
| 107 | + sampleVariableAssignments.stream() |
| 108 | + .map(var -> ExprStatement.withExpr(var)) |
| 109 | + .collect(Collectors.toList()); |
| 110 | + List<Statement> body = new ArrayList<>(setVariables); |
| 111 | + body.add(invokeMethod); |
| 112 | + return body; |
| 113 | + } |
| 114 | + |
| 115 | + private static ClassDefinition composeSampleClass( |
| 116 | + List<CommentStatement> fileHeader, |
| 117 | + String packageName, |
| 118 | + String sampleClassName, |
| 119 | + MethodDefinition mainMethod, |
| 120 | + MethodDefinition sampleMethod, |
| 121 | + RegionTag regionTag) { |
| 122 | + return ClassDefinition.builder() |
| 123 | + .setFileHeader(fileHeader) |
| 124 | + .setRegionTag(regionTag) |
| 125 | + .setScope(ScopeNode.PUBLIC) |
| 126 | + .setPackageString(packageName) |
| 127 | + .setName(sampleClassName) |
| 128 | + .setMethods(ImmutableList.of(mainMethod, sampleMethod)) |
| 129 | + .build(); |
| 130 | + } |
| 131 | + |
| 132 | + private static MethodDefinition composeMainMethod(List<Statement> mainBody) { |
| 133 | + return MethodDefinition.builder() |
| 134 | + .setScope(ScopeNode.PUBLIC) |
| 135 | + .setIsStatic(true) |
| 136 | + .setReturnType(TypeNode.VOID) |
| 137 | + .setName("main") |
| 138 | + .setArguments( |
| 139 | + VariableExpr.builder() |
| 140 | + .setVariable( |
| 141 | + Variable.builder().setType(TypeNode.STRING_ARRAY).setName("args").build()) |
| 142 | + .setIsDecl(true) |
| 143 | + .build()) |
| 144 | + .setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))) |
| 145 | + .setBody(mainBody) |
| 146 | + .build(); |
| 147 | + } |
| 148 | + |
| 149 | + private static MethodDefinition composeSampleMethod( |
| 150 | + String sampleMethodName, |
| 151 | + List<VariableExpr> sampleMethodArgs, |
| 152 | + List<Statement> sampleMethodBody) { |
| 153 | + return MethodDefinition.builder() |
| 154 | + .setScope(ScopeNode.PUBLIC) |
| 155 | + .setIsStatic(true) |
| 156 | + .setReturnType(TypeNode.VOID) |
| 157 | + .setName(sampleMethodName) |
| 158 | + .setArguments(sampleMethodArgs) |
| 159 | + .setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))) |
| 160 | + .setBody(sampleMethodBody) |
| 161 | + .build(); |
| 162 | + } |
| 163 | +} |
0 commit comments