-
Notifications
You must be signed in to change notification settings - Fork 68
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
868b41d
Add writeSampleCode method
summer-ji-eng 433014b
add license
summer-ji-eng d9a74a7
Enable single statement as input
summer-ji-eng bf3fb8d
combine unit test
summer-ji-eng a1d956d
add final in class
summer-ji-eng dbfcb2e
Merge branch 'master' into add_sample_code_writer
summer-ji-eng 0645521
refactor method name
summer-ji-eng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/google/api/generator/gapic/composer/samplecode/SampleCodeWriter.java
This file contains hidden or 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,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 write(Statement ... statement) { | ||
return write(Arrays.asList(statement)); | ||
} | ||
|
||
public static String write(List<Statement> statements) { | ||
JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
for (Statement statement : statements) { | ||
statement.accept(visitor); | ||
} | ||
return SampleCodeJavaFormatter.format(visitor.write()); | ||
} | ||
} |
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
src/test/java/com/google/api/generator/gapic/composer/samplecode/SampleCodeWriterTest.java
This file contains hidden or 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,80 @@ | ||
// 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.ExprStatement; | ||
import com.google.api.generator.engine.ast.MethodInvocationExpr; | ||
import com.google.api.generator.engine.ast.PrimitiveValue; | ||
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 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.write( | ||
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) { | ||
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(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: Since there is only one usage of this method, let's just call the internals directly and forego this wrapper.
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.
There are two usage above. Line 57, and line 58. I suggest to use the wrapper make the code clean.