diff --git a/run.sh b/run.sh index 0103b0cec4..6adb1f4771 100755 --- a/run.sh +++ b/run.sh @@ -72,12 +72,25 @@ then echo_success "Done" fi +# Key values are synced to rules_java_gapic/java_gapic.bzl. +SERVICE_CONFIG_OPT="" +if [ -n "$FLAGS_service_config" ] +then + SERVICE_CONFIG_OPT="grpc-service-config=$FLAGS_service_config" +fi +GAPIC_CONFIG_OPT="" +if [ -n "$FLAGS_gapic_config" ] +then + GAPIC_CONFIG_OPT="gapic-config=$FLAGS_gapic_config" +fi + # Run protoc. protoc -I="${PROTOC_INCLUDE_DIR}" -I="${FLAGS_googleapis}" -I="${FLAGS_protos}" \ -I="${FLAGS_googleapis}/google/longrunning" \ + --include_source_info \ --plugin=bazel-bin/protoc-gen-java_gapic ${FLAGS_protos}/*.proto \ --java_gapic_out="${FLAGS_out}" \ - --java_gapic_opt="${FLAGS_service_config},${FLAGS_gapic_config}" \ + --java_gapic_opt="${SERVICE_CONFIG_OPT},${GAPIC_CONFIG_OPT}" \ --experimental_allow_proto3_optional echo_success "Output files written to ${FLAGS_out}" diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/PluginArgumentParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/PluginArgumentParser.java index d5e325e3bb..784a9ae496 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/PluginArgumentParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/PluginArgumentParser.java @@ -22,36 +22,48 @@ // Parses the arguments from the protoc plugin. public class PluginArgumentParser { private static final String COMMA = ","; + private static final String EQUALS = "="; + + // Synced to rules_java_gapic/java_gapic.bzl. + @VisibleForTesting static final String KEY_GRPC_SERVICE_CONFIG = "grpc-service-config"; + @VisibleForTesting static final String KEY_GAPIC_CONFIG = "gapic-config"; + private static final String JSON_FILE_ENDING = "grpc_service_config.json"; private static final String GAPIC_YAML_FILE_ENDING = "gapic.yaml"; - public static Optional parseJsonConfigPath(CodeGeneratorRequest request) { + static Optional parseJsonConfigPath(CodeGeneratorRequest request) { return parseJsonConfigPath(request.getParameter()); } - public static Optional parseGapicYamlConfigPath(CodeGeneratorRequest request) { + static Optional parseGapicYamlConfigPath(CodeGeneratorRequest request) { return parseGapicYamlConfigPath(request.getParameter()); } /** Expects a comma-separated list of file paths. */ @VisibleForTesting static Optional parseJsonConfigPath(String pluginProtocArgument) { - return parseArgument(pluginProtocArgument, JSON_FILE_ENDING); + return parseArgument(pluginProtocArgument, KEY_GRPC_SERVICE_CONFIG, JSON_FILE_ENDING); } @VisibleForTesting static Optional parseGapicYamlConfigPath(String pluginProtocArgument) { - return parseArgument(pluginProtocArgument, GAPIC_YAML_FILE_ENDING); + return parseArgument(pluginProtocArgument, KEY_GAPIC_CONFIG, GAPIC_YAML_FILE_ENDING); } - private static Optional parseArgument(String pluginProtocArgument, String fileEnding) { + private static Optional parseArgument( + String pluginProtocArgument, String key, String fileEnding) { if (Strings.isNullOrEmpty(pluginProtocArgument)) { return Optional.empty(); } - for (String rawPath : pluginProtocArgument.split(COMMA)) { - String path = rawPath.trim(); - if (path.endsWith(fileEnding)) { - return Optional.of(path); + for (String argComponent : pluginProtocArgument.split(COMMA)) { + String[] args = argComponent.trim().split(EQUALS); + if (args.length < 2) { + continue; + } + String keyVal = args[0]; + String valueVal = args[1]; + if (keyVal.equals(key) && valueVal.endsWith(fileEnding)) { + return Optional.of(valueVal); } } return Optional.empty(); diff --git a/src/test/java/com/google/api/generator/gapic/protoparser/PluginArgumentParserTest.java b/src/test/java/com/google/api/generator/gapic/protoparser/PluginArgumentParserTest.java index c17c3c101d..e8bbd21de9 100644 --- a/src/test/java/com/google/api/generator/gapic/protoparser/PluginArgumentParserTest.java +++ b/src/test/java/com/google/api/generator/gapic/protoparser/PluginArgumentParserTest.java @@ -21,10 +21,28 @@ import org.junit.Test; public class PluginArgumentParserTest { + @Test public void parseJsonPath_onlyOnePresent() { String jsonPath = "/tmp/grpc_service_config.json"; - assertEquals(jsonPath, PluginArgumentParser.parseJsonConfigPath(jsonPath).get()); + assertEquals( + jsonPath, + PluginArgumentParser.parseJsonConfigPath(createGrpcServiceConfig(jsonPath)).get()); + } + + @Test + public void parseJsonPath_returnsFirstOneFound() { + String jsonPathOne = "/tmp/foobar_grpc_service_config.json"; + String jsonPathTwo = "/tmp/some_other_grpc_service_config.json"; + assertEquals( + jsonPathOne, + PluginArgumentParser.parseJsonConfigPath( + String.join( + ",", + Arrays.asList( + createGrpcServiceConfig(jsonPathOne), + createGrpcServiceConfig(jsonPathTwo)))) + .get()); } @Test @@ -35,11 +53,11 @@ public void parseJsonPath_similarFileAppearsFirst() { String.join( ",", Arrays.asList( - gapicPath, - "/tmp/something.json", - "/tmp/some_grpc_service_configjson", - jsonPath, - gapicPath)); + createGapicConfig(gapicPath), + createGrpcServiceConfig("/tmp/something.json"), + createGrpcServiceConfig("/tmp/some_grpc_service_configjson"), + createGrpcServiceConfig(jsonPath), + createGapicConfig(gapicPath))); assertEquals(jsonPath, PluginArgumentParser.parseJsonConfigPath(rawArgument).get()); } @@ -51,11 +69,11 @@ public void parseJsonPath_argumentHasSpaces() { String.join( " , ", Arrays.asList( - gapicPath, - "/tmp/something.json", - "/tmp/some_grpc_service_configjson", - jsonPath, - gapicPath)); + createGapicConfig(gapicPath), + createGrpcServiceConfig("/tmp/something.json"), + createGrpcServiceConfig("/tmp/some_grpc_service_configjson"), + createGrpcServiceConfig(jsonPath), + createGapicConfig(gapicPath))); assertEquals(jsonPath, PluginArgumentParser.parseJsonConfigPath(rawArgument).get()); } @@ -63,7 +81,8 @@ public void parseJsonPath_argumentHasSpaces() { public void parseJsonPath_restAreEmpty() { String jsonPath = "/tmp/foobar_grpc_service_config.json"; String gapicPath = ""; - String rawArgument = String.join(",", Arrays.asList(gapicPath, jsonPath, gapicPath)); + String rawArgument = + String.join(",", Arrays.asList(gapicPath, createGrpcServiceConfig(jsonPath), gapicPath)); assertEquals(jsonPath, PluginArgumentParser.parseJsonConfigPath(rawArgument).get()); } @@ -77,7 +96,23 @@ public void parseJsonPath_noneFound() { @Test public void parseGapicYamlPath_onlyOnePresent() { String gapicPath = "/tmp/something_gapic.yaml"; - assertEquals(gapicPath, PluginArgumentParser.parseGapicYamlConfigPath(gapicPath).get()); + assertEquals( + gapicPath, + PluginArgumentParser.parseGapicYamlConfigPath(createGapicConfig(gapicPath)).get()); + } + + @Test + public void parseGapicYamlPath_returnsFirstOneFound() { + String gapicPathOne = "/tmp/something_gapic.yaml"; + String gapicPathTwo = "/tmp/other_gapic.yaml"; + assertEquals( + gapicPathOne, + PluginArgumentParser.parseGapicYamlConfigPath( + String.join( + ",", + Arrays.asList( + createGapicConfig(gapicPathOne), createGapicConfig(gapicPathTwo)))) + .get()); } @Test @@ -86,7 +121,12 @@ public void parseGapicYamlPath_similarFileAppearsFirst() { String gapicPath = "/tmp/something_gapic.yaml"; String rawArgument = String.join( - ",", Arrays.asList(jsonPath, "/tmp/something.yaml", "/tmp/some_gapicyaml", gapicPath)); + ",", + Arrays.asList( + createGrpcServiceConfig(jsonPath), + createGapicConfig("/tmp/something.yaml"), + createGapicConfig("/tmp/some_gapicyaml"), + createGapicConfig(gapicPath))); assertEquals(gapicPath, PluginArgumentParser.parseGapicYamlConfigPath(rawArgument).get()); } @@ -94,7 +134,8 @@ public void parseGapicYamlPath_similarFileAppearsFirst() { public void parseGapicYamlPath_restAreEmpty() { String jsonPath = ""; String gapicPath = "/tmp/something_gapic.yaml"; - String rawArgument = String.join(",", Arrays.asList(jsonPath, gapicPath, jsonPath)); + String rawArgument = + String.join(",", Arrays.asList(jsonPath, createGapicConfig(gapicPath), jsonPath)); assertEquals(gapicPath, PluginArgumentParser.parseGapicYamlConfigPath(rawArgument).get()); } @@ -102,7 +143,16 @@ public void parseGapicYamlPath_restAreEmpty() { public void parseGapicYamlPath_noneFound() { String jsonPath = "/tmp/foo_grpc_service_config.json"; String gapicPath = ""; - String rawArgument = String.join(",", Arrays.asList(jsonPath, gapicPath)); + String rawArgument = + String.join(",", Arrays.asList(createGrpcServiceConfig(jsonPath), gapicPath)); assertFalse(PluginArgumentParser.parseGapicYamlConfigPath(rawArgument).isPresent()); } + + private static String createGrpcServiceConfig(String path) { + return String.format("%s=%s", PluginArgumentParser.KEY_GRPC_SERVICE_CONFIG, path); + } + + private static String createGapicConfig(String path) { + return String.format("%s=%s", PluginArgumentParser.KEY_GAPIC_CONFIG, path); + } }