-
Notifications
You must be signed in to change notification settings - Fork 70
[composer][samplecode]Add writeSampleCode method #499
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
Changes from 5 commits
868b41d
433014b
d9a74a7
bf3fb8d
a1d956d
dbfcb2e
0645521
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright 2020 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.Statement; | ||
| import com.google.api.generator.engine.writer.JavaWriterVisitor; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public final class SampleCodeWriter { | ||
|
|
||
| public static String writeSampleCode(Statement ... statement) { | ||
| return writeSampleCode(Arrays.asList(statement)); | ||
| } | ||
|
|
||
| public static String writeSampleCode(List<Statement> statements) { | ||
| JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
| for (Statement statement : statements) { | ||
| statement.accept(visitor); | ||
| } | ||
| return SampleCodeJavaFormatter.format(visitor.write()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright 2020 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 static junit.framework.TestCase.assertEquals; | ||
|
|
||
| import com.google.api.gax.rpc.ClientSettings; | ||
| import com.google.api.generator.engine.ast.AssignmentExpr; | ||
| import com.google.api.generator.engine.ast.ConcreteReference; | ||
| import com.google.api.generator.engine.ast.Expr; | ||
| import com.google.api.generator.engine.ast.ExprStatement; | ||
| import com.google.api.generator.engine.ast.MethodInvocationExpr; | ||
| import com.google.api.generator.engine.ast.PrimitiveValue; | ||
| import com.google.api.generator.engine.ast.Reference; | ||
| import com.google.api.generator.engine.ast.TypeNode; | ||
| import com.google.api.generator.engine.ast.Statement; | ||
| import com.google.api.generator.engine.ast.TryCatchStatement; | ||
| import com.google.api.generator.engine.ast.ValueExpr; | ||
| import com.google.api.generator.engine.ast.Variable; | ||
| import com.google.api.generator.engine.ast.VariableExpr; | ||
| import com.google.api.generator.gapic.model.Method; | ||
| import java.lang.reflect.Array; | ||
| import java.util.Arrays; | ||
| import org.junit.Test; | ||
|
|
||
| public class SampleCodeWriterTest { | ||
| @Test | ||
| public void writeSampleCode_statements() { | ||
| TypeNode settingType = TypeNode.withReference(ConcreteReference.withClazz(ClientSettings.class)); | ||
| Variable aVar = Variable.builder().setName("clientSettings").setType(settingType).build(); | ||
| VariableExpr aVarExpr = VariableExpr.withVariable(aVar); | ||
| MethodInvocationExpr aValueExpr = MethodInvocationExpr.builder() | ||
| .setExprReferenceExpr(MethodInvocationExpr.builder() | ||
| .setMethodName("newBuilder") | ||
| .setStaticReferenceType(settingType) | ||
| .build()) | ||
| .setReturnType(settingType) | ||
| .setMethodName("build") | ||
| .build(); | ||
| AssignmentExpr assignmentExpr = AssignmentExpr.builder() | ||
| .setVariableExpr(aVarExpr.toBuilder().setIsDecl(true).build()) | ||
| .setValueExpr(aValueExpr) | ||
| .build(); | ||
| Statement sampleStatement = TryCatchStatement.builder() | ||
| .setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)) | ||
| .setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))) | ||
| .setIsSampleCode(true) | ||
| .build(); | ||
| String result = SampleCodeWriter.writeSampleCode( | ||
| ExprStatement.withExpr(assignmentExpr), | ||
| sampleStatement); | ||
| String expected = "ClientSettings clientSettings = ClientSettings.newBuilder().build();\n" | ||
| + "try (boolean aBool = false) {\n" | ||
| + " int x = 3;\n" | ||
| + "}"; | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| private AssignmentExpr createAssignmentExpr(String varName, String varValue, TypeNode type) { | ||
|
Contributor
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. Nit: Since there is only one usage of this method, let's just call the internals directly and forego this wrapper.
Contributor
Author
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. There are two usage above. Line 57, and line 58. I suggest to use the wrapper make the code clean. |
||
| Variable variable = Variable.builder().setName(varName).setType(type).build(); | ||
| VariableExpr variableExpr = | ||
| VariableExpr.builder() | ||
| .setVariable(variable) | ||
| .setIsDecl(true) | ||
| .build(); | ||
| return AssignmentExpr.builder() | ||
| .setVariableExpr(variableExpr) | ||
| .setValueExpr(ValueExpr.withValue(PrimitiveValue.builder().setType(type).setValue(varValue).build())) | ||
| .build(); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.