From 7d6ef259b3f578ef4d17c3f554c00aad65e21408 Mon Sep 17 00:00:00 2001 From: Sebastien Houver Date: Thu, 8 Oct 2020 12:09:17 +0200 Subject: [PATCH] adding support for experimental_allow_proto3_optional cf. https://github.com/xolstice/protobuf-maven-plugin/issues/77 --- .../plugin/protobuf/AbstractProtocMojo.java | 14 +++++++++++ .../maven/plugin/protobuf/Protoc.java | 24 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/xolstice/maven/plugin/protobuf/AbstractProtocMojo.java b/src/main/java/org/xolstice/maven/plugin/protobuf/AbstractProtocMojo.java index 9b993dc6..55e7b474 100644 --- a/src/main/java/org/xolstice/maven/plugin/protobuf/AbstractProtocMojo.java +++ b/src/main/java/org/xolstice/maven/plugin/protobuf/AbstractProtocMojo.java @@ -312,6 +312,19 @@ abstract class AbstractProtocMojo extends AbstractMojo { ) protected boolean useArgumentFile; + /** + * If set to {@code true}, experimental optional feature will be enabled. + * + *

NOTE: This is only supported for protoc 3.12.0 and higher.

+ * + * @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 @@ -623,6 +636,7 @@ protected void addProtocBuilderParameters(final Protoc.Builder protocBuilder) { } protocBuilder.setTempDirectory(tempDirectory); protocBuilder.useArgumentFile(useArgumentFile); + protocBuilder.useExperimentalOptional(useExperimentalOptional); } /** diff --git a/src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java b/src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java index d8097636..6800b3ab 100644 --- a/src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java +++ b/src/main/java/org/xolstice/maven/plugin/protobuf/Protoc.java @@ -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}. * @@ -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, @@ -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"); @@ -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(); } @@ -255,6 +263,10 @@ public List 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) { @@ -506,6 +518,8 @@ static final class Builder { private boolean useArgumentFile; + private boolean useExperimentalOptional; + /** * Constructs a new builder. * @@ -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()); @@ -862,7 +881,8 @@ public Protoc build() { nativePluginExecutable, nativePluginParameter, tempDirectory, - useArgumentFile); + useArgumentFile, + useExperimentalOptional); } } }