-
Notifications
You must be signed in to change notification settings - Fork 72
[composer][SampleCode]Utils function formatter for Sample Code #471
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
24 commits
Select commit
Hold shift + click to select a range
2cb0d9f
draft version
summer-ji-eng 2bf804e
Add unit test for long line
summer-ji-eng b0c6b47
Implement formatter for code snippet
summer-ji-eng 94bd9ed
Add license and comment
summer-ji-eng 209ec9d
Merge branch 'master' into sample_code_formatter
summer-ji-eng 7176a85
Use string as input parameters
summer-ji-eng 62da37b
remove unused dep
summer-ji-eng c9213eb
Merge branch 'master' into sample_code_formatter
summer-ji-eng 07d9d21
draft version
summer-ji-eng 55c7604
Add unit test for long line
summer-ji-eng 9f1b49d
Implement formatter for code snippet
summer-ji-eng 3126e5f
Add license and comment
summer-ji-eng 308e6cf
Use string as input parameters
summer-ji-eng e2f99ab
remove unused dep
summer-ji-eng e2d2a40
move utils to composer/samplecode
summer-ji-eng 875794f
Merge branch 'sample_code_formatter' of github.com:googleapis/gapic-g…
summer-ji-eng 40977cf
remove utils files
summer-ji-eng ad842eb
revert BUILD in test
summer-ji-eng 4426dd2
Merge branch 'master' into sample_code_formatter
summer-ji-eng 2cfa86f
Merge branch 'master' into sample_code_formatter
summer-ji-eng 222c910
Add comment and rephrase static string
summer-ji-eng 7ca32d8
simplify the wrapper
summer-ji-eng 4f91c12
Merge branch 'master' into sample_code_formatter
summer-ji-eng ef1a81d
Merge branch 'master' into sample_code_formatter
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel
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,18 @@ | ||
| load("@rules_java//java:defs.bzl", "java_library") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| filegroup( | ||
| name = "samplecode_files", | ||
| srcs = glob(["*.java"]), | ||
| ) | ||
|
|
||
| java_library( | ||
| name = "samplecode", | ||
| srcs = [ | ||
| ":samplecode_files", | ||
| ], | ||
| deps = [ | ||
| "@google_java_format_all_deps//jar", | ||
| ], | ||
| ) |
67 changes: 67 additions & 0 deletions
67
...main/java/com/google/api/generator/gapic/composer/samplecode/SampleCodeJavaFormatter.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,67 @@ | ||
| // 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.common.annotations.VisibleForTesting; | ||
| import com.google.googlejavaformat.java.Formatter; | ||
| import com.google.googlejavaformat.java.FormatterException; | ||
|
|
||
| public final class SampleCodeJavaFormatter { | ||
|
|
||
| private SampleCodeJavaFormatter() {} | ||
|
|
||
| private static final Formatter FORMATTER = new Formatter(); | ||
|
|
||
| private static final String FAKE_CLASS_TITLE = "public class FakeClass { void fakeMethod() {"; | ||
| private static final String FAKE_CLASS_CLOSE = "}}"; | ||
|
|
||
| /** | ||
| * This method is used to format sample code string. | ||
| * | ||
| * @param sampleCode A string is composed by statements. | ||
| * @return String Formatted sample code string based on google java style. | ||
| */ | ||
| public static String format(String sampleCode) { | ||
| final StringBuffer buffer = new StringBuffer(); | ||
| // Wrap the sample code inside a class for composing a valid Java source code. | ||
| // Because we utilized google-java-format to reformat the codes. | ||
| buffer.append(FAKE_CLASS_TITLE); | ||
| buffer.append(sampleCode); | ||
| buffer.append(FAKE_CLASS_CLOSE); | ||
|
|
||
| String formattedString = null; | ||
| try { | ||
| formattedString = FORMATTER.formatSource(buffer.toString()); | ||
| } catch (FormatterException e) { | ||
| throw new FormatException( | ||
| String.format("The sample code should be string where is composed by statements; %s", e)); | ||
| } | ||
| // Extract formatted sample code by | ||
| // 1. Removing the first and last two lines. | ||
| // 2. Delete the first 4 space for each line. | ||
| // 3. Trim the last new empty line. | ||
| return formattedString | ||
| .replaceAll("^([^\n]*\n){2}|([^\n]*\n){2}$", "") | ||
| .replaceAll("(?m)^ {4}", "") | ||
| .trim(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| protected static class FormatException extends RuntimeException { | ||
| public FormatException(String errorMessage) { | ||
| super(errorMessage); | ||
| } | ||
| } | ||
| } | ||
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
25 changes: 25 additions & 0 deletions
25
src/test/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel
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,25 @@ | ||
| load("@rules_java//java:defs.bzl", "java_test") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| TESTS = [ | ||
| "SampleCodeJavaFormatterTest", | ||
| ] | ||
|
|
||
| filegroup( | ||
| name = "samplecode_files", | ||
| srcs = ["{0}.java".format(f) for f in TESTS], | ||
| ) | ||
|
|
||
| [java_test( | ||
| name = test_name, | ||
| srcs = ["{0}.java".format(test_name)], | ||
| test_class = "com.google.api.generator.gapic.composer.samplecode.{0}".format(test_name), | ||
| deps = [ | ||
| "//src/main/java/com/google/api/generator/engine/ast", | ||
| "//src/main/java/com/google/api/generator/engine/writer", | ||
| "//src/main/java/com/google/api/generator/gapic/composer/samplecode", | ||
| "@com_google_truth_truth//jar", | ||
| "@junit_junit//jar", | ||
| ], | ||
| ) for test_name in TESTS] |
86 changes: 86 additions & 0 deletions
86
.../java/com/google/api/generator/gapic/composer/samplecode/SampleCodeJavaFormatterTest.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,86 @@ | ||
| // 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 static org.junit.Assert.assertThrows; | ||
|
|
||
| import com.google.api.generator.gapic.composer.samplecode.SampleCodeJavaFormatter; | ||
| import com.google.api.generator.gapic.composer.samplecode.SampleCodeJavaFormatter.FormatException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.junit.Test; | ||
|
|
||
| public class SampleCodeJavaFormatterTest { | ||
|
|
||
| @Test | ||
| public void validFormatSampleCode_tryCatchStatement() { | ||
| String samplecode = String.format(createLines(3), | ||
| "try(boolean condition = false){", | ||
| "int x = 3;", | ||
| "}"); | ||
| String result = SampleCodeJavaFormatter.format(samplecode); | ||
| String expected = | ||
| String.format(createLines(3), "try (boolean condition = false) {\n", " int x = 3;\n", "}"); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void validFormatSampleCode_longLineStatement() { | ||
| String sampleCode = | ||
| "SubscriptionAdminSettings subscriptionAdminSettings = " | ||
| + "SubscriptionAdminSettings.newBuilder().setEndpoint(myEndpoint).build();"; | ||
| String result = SampleCodeJavaFormatter.format(sampleCode); | ||
| String expected = | ||
| String.format( | ||
| createLines(2), | ||
| "SubscriptionAdminSettings subscriptionAdminSettings =\n", | ||
| " SubscriptionAdminSettings.newBuilder().setEndpoint(myEndpoint).build();"); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void validFormatSampleCode_longChainMethod() { | ||
| String sampleCode = "echoSettingsBuilder.echoSettings().setRetrySettings(echoSettingsBuilder.echoSettings().getRetrySettings().toBuilder().setTotalTimeout(Duration.ofSeconds(30)).build());"; | ||
| String result = SampleCodeJavaFormatter.format(sampleCode); | ||
| String expected = | ||
| String.format( | ||
| createLines(9), | ||
| "echoSettingsBuilder\n", | ||
| " .echoSettings()\n", | ||
| " .setRetrySettings(\n", | ||
| " echoSettingsBuilder\n", | ||
| " .echoSettings()\n", | ||
| " .getRetrySettings()\n", | ||
| " .toBuilder()\n", | ||
| " .setTotalTimeout(Duration.ofSeconds(30))\n", | ||
| " .build());"); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void invalidFormatSampleCode_nonStatement() { | ||
| assertThrows( | ||
| FormatException.class, | ||
| () -> { | ||
| SampleCodeJavaFormatter.format("abc"); | ||
| }); | ||
| } | ||
|
|
||
| /** =============================== HELPERS =============================== */ | ||
| private static String createLines(int numLines) { | ||
| return new String(new char[numLines]).replace("\0", "%s"); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.