-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grpc-protoc typeNameSuffix option (#1184)
Motivation: If a project uses multiple protoc plugins it is possible there are naming collisions for the resulting generated code. The protoc plugin interface doesn't give visibility into types generated by other plugins, and the plugins may not control the package name (specified in the `.proto` file, maybe used by builtins and plugins if generating into a single file). Modifications: - servicetalk-grpc-protoc now supports a typeNameSuffix option that appends a postfix to service type names to avoid collisions in the generated code. Result: Users can configure grpc-protoc to generate unique type names to avoid collisions with other gRPC protoc plugins.
- Loading branch information
1 parent
a0bf19d
commit 1e6ac5f
Showing
5 changed files
with
257 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
== ServiceTalk protoc plugin for gRPC | ||
|
||
This module implements the | ||
link:https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/plugin.proto[Protoc Plugin Interface] | ||
and generates ServiceTalk code for service definitions contained in `.proto` files. | ||
|
||
It is recommended to automate the usage of this plugin in your build via | ||
link:https://github.com/google/protobuf-gradle-plugin[protobuf-gradle-plugin] or | ||
link:https://www.xolstice.org/protobuf-maven-plugin[protobuf-maven-plugin]. Both options | ||
are demonstrated in the | ||
link:{source-root}/servicetalk-examples/grpc/helloworld[gRPC HelloWorld Example]. | ||
|
||
=== Options | ||
This plugin supports the following | ||
link:https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.command_line_interface[options]: | ||
|
||
==== `typeNameSuffix=<value>` | ||
Appends the <value> onto service type names generated by this plugin. This helps to avoid service type name | ||
collisions if other protoc plugins are generating code in the same gradle project, or in the same package name. | ||
|
||
If you are using the | ||
link:https://github.com/google/protobuf-gradle-plugin#configure-what-to-generate[protobuf-gradle-plugin] this is how you | ||
can specify an option: | ||
|
||
[source,gradle] | ||
---- | ||
task.plugins { | ||
servicetalk_grpc { | ||
option 'typeNameSuffix=Foo' | ||
} | ||
} | ||
---- |
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
155 changes: 155 additions & 0 deletions
155
servicetalk-grpc-protoc/src/test/java/io/servicetalk/grpc/protoc/StringUtilsTest.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,155 @@ | ||
/* | ||
* 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 org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class StringUtilsTest { | ||
@Test | ||
public void emptyOptions() { | ||
Map<String, String> options = StringUtils.parseOptions(""); | ||
assertThat(options.isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void singleEntryNoValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo"); | ||
assertThat(options.size(), is(1)); | ||
assertContainsNullValue(options, "foo"); | ||
} | ||
|
||
@Test | ||
public void singleEntryValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo=bar"); | ||
assertThat(options.size(), is(1)); | ||
assertThat(options.get("foo"), is("bar")); | ||
} | ||
|
||
@Test | ||
public void twoEntriesNoValues() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2"); | ||
assertThat(options.size(), is(2)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertContainsNullValue(options, "foo2"); | ||
} | ||
|
||
@Test | ||
public void twoEntriesFirstValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2"); | ||
assertThat(options.size(), is(2)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertContainsNullValue(options, "foo2"); | ||
} | ||
|
||
@Test | ||
public void twoEntriesSecondValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2=bar2"); | ||
assertThat(options.size(), is(2)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
} | ||
|
||
@Test | ||
public void twoEntriesBothValues() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2=bar2"); | ||
assertThat(options.size(), is(2)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
} | ||
|
||
@Test | ||
public void threeEntriesNoValues() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2,foo3"); | ||
assertThat(options.size(), is(3)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertContainsNullValue(options, "foo2"); | ||
assertContainsNullValue(options, "foo3"); | ||
} | ||
|
||
@Test | ||
public void threeEntriesFirstValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2,foo3"); | ||
assertThat(options.size(), is(3)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertContainsNullValue(options, "foo2"); | ||
assertContainsNullValue(options, "foo3"); | ||
} | ||
|
||
@Test | ||
public void threeEntriesSecondValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2=bar2,foo3"); | ||
assertThat(options.size(), is(3)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
assertContainsNullValue(options, "foo3"); | ||
} | ||
|
||
@Test | ||
public void threeEntriesThirdValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2,foo3=bar3"); | ||
assertThat(options.size(), is(3)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertContainsNullValue(options, "foo2"); | ||
assertThat(options.get("foo3"), is("bar3")); | ||
} | ||
|
||
@Test | ||
public void threeEntriesFirstSecondValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2=bar2,foo3"); | ||
assertThat(options.size(), is(3)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
assertContainsNullValue(options, "foo3"); | ||
} | ||
|
||
@Test | ||
public void threeEntriesFirstThirdValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2,foo3=bar3"); | ||
assertThat(options.size(), is(3)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertContainsNullValue(options, "foo2"); | ||
assertThat(options.get("foo3"), is("bar3")); | ||
} | ||
|
||
@Test | ||
public void threeEntriesSecondThirdValue() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1,foo2=bar2,foo3=bar3"); | ||
assertThat(options.size(), is(3)); | ||
assertContainsNullValue(options, "foo1"); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
assertThat(options.get("foo3"), is("bar3")); | ||
} | ||
|
||
@Test | ||
public void threeEntriesValues() { | ||
Map<String, String> options = StringUtils.parseOptions("foo1=bar1,foo2=bar2,foo3=bar3"); | ||
assertThat(options.size(), is(3)); | ||
assertThat(options.get("foo1"), is("bar1")); | ||
assertThat(options.get("foo2"), is("bar2")); | ||
assertThat(options.get("foo3"), is("bar3")); | ||
} | ||
|
||
private static void assertContainsNullValue(Map<String, String> options, String key) { | ||
assertThat(options.containsKey(key), is(true)); | ||
assertThat(options.get(key), is(nullValue())); | ||
} | ||
} |