Skip to content

Commit

Permalink
adding support for experimental_allow_proto3_optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhvrsg committed Oct 8, 2020
1 parent fe8e644 commit ef0cb8b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,19 @@ abstract class AbstractProtocMojo extends AbstractMojo {
)
protected boolean useArgumentFile;

/**
* If set to {@code true}, experimental optional feature will be enabled.
*
* <p><b>NOTE:</b> This is only supported for protoc 3.12.0 and higher.</p>
*
* @since 0.7.0
*/
@Parameter(
required = false,
defaultValue = "false"
)
protected boolean useExperimentalOptional;

/**
* Specifies one of more custom protoc plugins, written in Java
* and available as Maven artifacts. An executable plugin will be created
Expand Down Expand Up @@ -623,6 +636,7 @@ protected void addProtocBuilderParameters(final Protoc.Builder protocBuilder) {
}
protocBuilder.setTempDirectory(tempDirectory);
protocBuilder.useArgumentFile(useArgumentFile);
protocBuilder.useExperimentalOptional(useExperimentalOptional);
}

/**
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ final class Protoc {
*/
private final boolean useArgumentFile;

/**
* A boolean indicating if experimental optional feature should be enabled.
*/
private boolean useExperimentalOptional;

/**
* Constructs a new instance. This should only be used by the {@link Builder}.
*
Expand All @@ -148,6 +153,7 @@ final class Protoc {
* @param nativePluginParameter an optional parameter for a native plugin.
* @param tempDirectory a directory where temporary files will be generated.
* @param useArgumentFile If {@code true}, parameters to protoc will be put in an argument file
* @param useExperimentalOptional If {@code true}, experimental optional feature will be enabled
*/
private Protoc(
final String executable,
Expand All @@ -168,7 +174,8 @@ private Protoc(
final String nativePluginExecutable,
final String nativePluginParameter,
final File tempDirectory,
final boolean useArgumentFile
final boolean useArgumentFile,
final boolean useExperimentalOptional
) {
if (executable == null) {
throw new MojoConfigurationException("'executable' is null");
Expand Down Expand Up @@ -198,6 +205,7 @@ private Protoc(
this.nativePluginParameter = nativePluginParameter;
this.tempDirectory = tempDirectory;
this.useArgumentFile = useArgumentFile;
this.useExperimentalOptional = useExperimentalOptional;
this.error = new StringStreamConsumer();
this.output = new StringStreamConsumer();
}
Expand Down Expand Up @@ -255,6 +263,10 @@ public List<String> buildProtocCommand() {
for (final File protoPathElement : protoPathElements) {
command.add("--proto_path=" + protoPathElement);
}
if (useExperimentalOptional) {
command.add("--experimental_allow_proto3_optional");

}
if (javaOutputDirectory != null) {
String outputOption = "--java_out=";
if (nativePluginParameter != null) {
Expand Down Expand Up @@ -506,6 +518,8 @@ static final class Builder {

private boolean useArgumentFile;

private boolean useExperimentalOptional;

/**
* Constructs a new builder.
*
Expand Down Expand Up @@ -749,6 +763,11 @@ public Builder useArgumentFile(final boolean useArgumentFile) {
return this;
}

public Builder useExperimentalOptional(final boolean useExperimentalOptional) {
this.useExperimentalOptional = useExperimentalOptional;
return this;
}

private void checkProtoFileIsInProtopath(final File protoFile) {
if (!protoFile.isFile()) {
throw new MojoConfigurationException("Not a regular file: " + protoFile.getAbsolutePath());
Expand Down Expand Up @@ -862,7 +881,8 @@ public Protoc build() {
nativePluginExecutable,
nativePluginParameter,
tempDirectory,
useArgumentFile);
useArgumentFile,
useExperimentalOptional);
}
}
}

0 comments on commit ef0cb8b

Please sign in to comment.