-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ServiceTalk protoc plugin extensible (#2160)
Motivation: When we want to enrich ServiceTalk generated service classes with additional functionality, this insertion point enables us to add such code. Modifications: - Adds plugin insertion point (comment) into servicetalk generated service classes; - Since JavaPoet has no way to add a comment within a class, added an empty type as a placeholder and replaced it with the comment before adding the content to `CodeGeneratorResponse`; Result: Every ServiceTalk generated service class will have the following comment towards the end of the class: // @@protoc_insertion_point(service_scope:<fully qualified name of service>)
- Loading branch information
suman-ganta
authored
Apr 13, 2022
1 parent
78d1186
commit 2ca7af2
Showing
5 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
107 changes: 107 additions & 0 deletions
107
servicetalk-grpc-protoc/src/test/java/io/servicetalk/grpc/protoc/InsertionPointTest.java
This file contains 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,107 @@ | ||
/* | ||
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* 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 io.servicetalk.grpc.protoc; | ||
|
||
import com.google.protobuf.DescriptorProtos; | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest; | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
import static io.servicetalk.grpc.protoc.FileDescriptor.insertionPoint; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class InsertionPointTest { | ||
|
||
@Test | ||
void insertionPointExistsInMultiFiles() throws IOException { | ||
List<CodeGeneratorResponse.File> files = generate("test_multi.proto"); | ||
assertEquals(2, files.size()); | ||
|
||
assertTrue(files.get(0).getContent().contains(insertionPoint("test.multi.Tester"))); | ||
assertTrue(files.get(1).getContent().contains(insertionPoint("test.multi.Tester2"))); | ||
} | ||
|
||
@Test | ||
void insertionPointExistsInSingleFile() throws IOException { | ||
List<CodeGeneratorResponse.File> files = generate("test_single.proto"); | ||
assertEquals(3, files.size()); | ||
assertTrue(files.get(1).getContent().contains(insertionPoint("test.single.Greeter"))); | ||
assertTrue(files.get(2).getContent().contains(insertionPoint("test.single.Fareweller"))); | ||
} | ||
|
||
private static List<CodeGeneratorResponse.File> generate(String file) throws IOException { | ||
final CodeGeneratorRequest.Builder reqBuilder = CodeGeneratorRequest.newBuilder(); | ||
reqBuilder.addAllProtoFile(getFileDescriptorSet().getFileList()); | ||
reqBuilder.addFileToGenerate(file); | ||
CodeGeneratorResponse codeGeneratorResponse = executePlugin(reqBuilder.build()); | ||
return codeGeneratorResponse.getFileList(); | ||
} | ||
|
||
/** | ||
* Executes protoc servicetalk plugin | ||
* @param request generator request | ||
* @return generator response | ||
* @throws IOException throws exception if it fails to get hold of descriptor set | ||
*/ | ||
private static CodeGeneratorResponse executePlugin(CodeGeneratorRequest request) throws IOException { | ||
// make stdin with request | ||
ByteArrayOutputStream tempCollector = new ByteArrayOutputStream(); | ||
request.writeTo(tempCollector); | ||
ByteArrayInputStream stdinWithRequest = new ByteArrayInputStream(tempCollector.toByteArray()); | ||
|
||
// hold stdout into this after plugin execution | ||
ByteArrayOutputStream stdoutWithResponse = new ByteArrayOutputStream(); | ||
|
||
InputStream stdin = System.in; | ||
PrintStream stdout = System.out; | ||
try { | ||
// prepare stdin and stdout for the plugin execution | ||
System.setIn(stdinWithRequest); | ||
System.setOut(new PrintStream(stdoutWithResponse)); | ||
// execute plugin | ||
Main.main(); | ||
return CodeGeneratorResponse.parseFrom(stdoutWithResponse.toByteArray()); | ||
} finally { | ||
// restore | ||
System.setIn(stdin); | ||
System.setOut(stdout); | ||
} | ||
} | ||
|
||
/** | ||
* This descriptor file is expected to be generated at build time by the protoc plugin | ||
* @return descriptor fileset | ||
* @throws IOException throws exception if it fails to locate descriptor set on file system | ||
*/ | ||
static DescriptorProtos.FileDescriptorSet getFileDescriptorSet() throws IOException { | ||
String baseDir = System.getProperty("generatedFilesBaseDir", | ||
"servicetalk-grpc-protoc/build/generated/sources/proto"); | ||
File descriptorSet = new File(baseDir + "/test/descriptor_set.desc"); | ||
assertTrue(descriptorSet.exists()); | ||
byte[] data = Files.readAllBytes(descriptorSet.toPath()); | ||
return DescriptorProtos.FileDescriptorSet.parseFrom(data); | ||
} | ||
} |