Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> parseJsonConfigPath(CodeGeneratorRequest request) {
static Optional<String> parseJsonConfigPath(CodeGeneratorRequest request) {
return parseJsonConfigPath(request.getParameter());
}

public static Optional<String> parseGapicYamlConfigPath(CodeGeneratorRequest request) {
static Optional<String> parseGapicYamlConfigPath(CodeGeneratorRequest request) {
return parseGapicYamlConfigPath(request.getParameter());
}

/** Expects a comma-separated list of file paths. */
@VisibleForTesting
static Optional<String> parseJsonConfigPath(String pluginProtocArgument) {
return parseArgument(pluginProtocArgument, JSON_FILE_ENDING);
return parseArgument(pluginProtocArgument, KEY_GRPC_SERVICE_CONFIG, JSON_FILE_ENDING);
}

@VisibleForTesting
static Optional<String> parseGapicYamlConfigPath(String pluginProtocArgument) {
return parseArgument(pluginProtocArgument, GAPIC_YAML_FILE_ENDING);
return parseArgument(pluginProtocArgument, KEY_GAPIC_CONFIG, GAPIC_YAML_FILE_ENDING);
}

private static Optional<String> parseArgument(String pluginProtocArgument, String fileEnding) {
private static Optional<String> parseArgument(
String pluginProtocArgument, String key, String fileEnding) {
if (Strings.isNullOrEmpty(pluginProtocArgument)) {
return Optional.<String>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.<String>empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

Expand All @@ -51,19 +69,20 @@ 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());
}

@Test
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());
}

Expand All @@ -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
Expand All @@ -86,23 +121,38 @@ 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());
}

@Test
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());
}

@Test
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);
}
}