diff --git a/src/main/java/com/google/api/codegen/config/ConfigGeneratorApi.java b/src/main/java/com/google/api/codegen/config/ConfigGeneratorApi.java index a0e436606b..fd19260198 100644 --- a/src/main/java/com/google/api/codegen/config/ConfigGeneratorApi.java +++ b/src/main/java/com/google/api/codegen/config/ConfigGeneratorApi.java @@ -20,6 +20,7 @@ import com.google.api.tools.framework.aspects.http.HttpConfigAspect; import com.google.api.tools.framework.model.Interface; import com.google.api.tools.framework.model.Method; +import com.google.api.tools.framework.model.ProtoFile; import com.google.api.tools.framework.model.stages.Merged; import com.google.api.tools.framework.processors.merger.Merger; import com.google.api.tools.framework.processors.resolver.Resolver; @@ -30,9 +31,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.io.Files; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; - import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -45,6 +43,9 @@ import java.util.Set; import java.util.TreeMap; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + /** Main class for the config generator. */ public class ConfigGeneratorApi extends ToolDriverBase { @@ -68,7 +69,7 @@ public class ConfigGeneratorApi extends ToolDriverBase { private static final String CONFIG_PROTO_TYPE = ConfigProto.getDescriptor().getFullName(); private static final String CONFIG_KEY_TIMEOUT = "timeout_millis"; - private static final int CONFIG_VALUE_DEFAULT_TIMEOUT = 30000; + private static final int CONFIG_VALUE_DEFAULT_TIMEOUT = 60000; /** Constructs a config generator api based on given options. */ public ConfigGeneratorApi(ToolOptions options) { @@ -156,9 +157,13 @@ private List generateInterfacesConfig() { } private Map generateLanguageSettings() { - int index = - Preconditions.checkPositionIndex(model.getFiles().size() - 1, model.getFiles().size()); - String packageName = model.getFiles().get(index).getFullName(); + String packageName = null; + for (Interface interfaze : model.getSymbolTable().getInterfaces()) { + // use the package name of the first interface + packageName = interfaze.getFile().getFullName(); + break; + } + Preconditions.checkNotNull(packageName, "No interface found."); return LanguageGenerator.generate(packageName); } diff --git a/src/main/java/com/google/api/codegen/config/FieldConfigGenerator.java b/src/main/java/com/google/api/codegen/config/FieldConfigGenerator.java index 1bf7028503..0b31ed2574 100644 --- a/src/main/java/com/google/api/codegen/config/FieldConfigGenerator.java +++ b/src/main/java/com/google/api/codegen/config/FieldConfigGenerator.java @@ -64,7 +64,10 @@ public Map generate(Method method) { } result.put(CONFIG_KEY_REQUIRED_FIELDS, new LinkedList(parameterList)); } - if (parameterList.size() > REQUEST_OBJECT_METHOD_THRESHOLD) { + // use all fields for the following check; if there are ignored fields for flattening + // purposes, the caller still needs a way to set them (by using the request object method). + if (message.getFields().size() > REQUEST_OBJECT_METHOD_THRESHOLD + || message.getFields().size() != parameterList.size()) { result.put(CONFIG_KEY_REQUEST_OBJECT_METHOD, true); } else { result.put(CONFIG_KEY_REQUEST_OBJECT_METHOD, false); diff --git a/src/main/java/com/google/api/codegen/config/LanguageGenerator.java b/src/main/java/com/google/api/codegen/config/LanguageGenerator.java index 71e0857d81..e452888573 100644 --- a/src/main/java/com/google/api/codegen/config/LanguageGenerator.java +++ b/src/main/java/com/google/api/codegen/config/LanguageGenerator.java @@ -17,6 +17,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -33,24 +34,31 @@ public class LanguageGenerator { private static final String CONFIG_KEY_PACKAGE_NAME = "package_name"; - private static final Map LANGUAGE_FORMATTERS = - ImmutableMap.builder() - .put("java", new SimpleLanguageFormatter(".", "com", false)) - .put("python", new SimpleLanguageFormatter(".", null, false)) - .put("go", new GoLanguageFormatter()) - .put("csharp", new SimpleLanguageFormatter(".", null, true)) - .put("ruby", new SimpleLanguageFormatter("::", null, true)) - .put("php", new SimpleLanguageFormatter("\\", null, true)) - .build(); + private static final Map LANGUAGE_FORMATTERS; + + static { + List javaRewriteRules = + Arrays.asList( + new RewriteRule("^google", "com.google.cloud"), + new RewriteRule("(.v[^.]+)$", ".spi$1")); + List phpRewriteRules = Arrays.asList(new RewriteRule("^google", "google.cloud")); + LANGUAGE_FORMATTERS = + ImmutableMap.builder() + .put("java", new SimpleLanguageFormatter(".", javaRewriteRules, false)) + .put("python", new SimpleLanguageFormatter(".", null, false)) + .put("go", new GoLanguageFormatter()) + .put("csharp", new SimpleLanguageFormatter(".", null, true)) + .put("ruby", new SimpleLanguageFormatter("::", null, true)) + .put("php", new SimpleLanguageFormatter("\\", phpRewriteRules, true)) + .build(); + } public static Map generate(String packageName) { - List packageNameComponents = - Arrays.asList(packageName.split(DEFAULT_PACKAGE_SEPARATOR)); Map languages = new LinkedHashMap<>(); for (String language : LANGUAGE_FORMATTERS.keySet()) { LanguageFormatter formatter = LANGUAGE_FORMATTERS.get(language); - String formattedPackageName = formatter.getFormattedPackageName(packageNameComponents); + String formattedPackageName = formatter.getFormattedPackageName(packageName); Map packageNameMap = new LinkedHashMap<>(); packageNameMap.put(CONFIG_KEY_PACKAGE_NAME, formattedPackageName); @@ -64,27 +72,32 @@ private static String firstCharToUpperCase(String string) { } private interface LanguageFormatter { - String getFormattedPackageName(List nameComponents); + String getFormattedPackageName(String packageName); } private static class SimpleLanguageFormatter implements LanguageFormatter { private final String separator; - private final String prefix; + private final List rewriteRules; private final boolean capitalize; - public SimpleLanguageFormatter(String separator, String prefix, boolean capitalize) { + public SimpleLanguageFormatter( + String separator, List rewriteRules, boolean capitalize) { this.separator = separator; - this.prefix = prefix; + if (rewriteRules != null) { + this.rewriteRules = rewriteRules; + } else { + this.rewriteRules = new ArrayList<>(); + } this.capitalize = capitalize; } - public String getFormattedPackageName(List nameComponents) { - List elements = new LinkedList<>(); - if (prefix != null) { - elements.add(prefix); + public String getFormattedPackageName(String packageName) { + for (RewriteRule rewriteRule : rewriteRules) { + packageName = rewriteRule.rewrite(packageName); } - for (String component : nameComponents) { + List elements = new LinkedList<>(); + for (String component : packageName.split(DEFAULT_PACKAGE_SEPARATOR)) { if (capitalize) { elements.add(firstCharToUpperCase(component)); } else { @@ -96,11 +109,10 @@ public String getFormattedPackageName(List nameComponents) { } private static class GoLanguageFormatter implements LanguageFormatter { + public String getFormattedPackageName(String packageName) { + List nameComponents = new ArrayList<>(); + nameComponents.addAll(Arrays.asList(packageName.split(DEFAULT_PACKAGE_SEPARATOR))); - private static LanguageFormatter backup = - new SimpleLanguageFormatter("/", "google.golang.org", false); - - public String getFormattedPackageName(List nameComponents) { // If the name follows the pattern google.foo.bar.v1234, // we reformat it into cloud.google.com. // google.logging.v2 => cloud.google.com/go/logging/apiv2 @@ -109,7 +121,8 @@ public String getFormattedPackageName(List nameComponents) { if (size < 3 || !nameComponents.get(0).equals("google") || !nameComponents.get(size - 1).startsWith("v")) { - return backup.getFormattedPackageName(nameComponents); + nameComponents.add(0, "google.golang.org"); + return Joiner.on("/").join(nameComponents); } return "cloud.google.com/go/" + Joiner.on("/").join(nameComponents.subList(1, size - 1)) @@ -117,4 +130,21 @@ public String getFormattedPackageName(List nameComponents) { + nameComponents.get(size - 1); } } + + private static class RewriteRule { + private final String pattern; + private final String replacement; + + public RewriteRule(String pattern, String replacement) { + this.pattern = pattern; + this.replacement = replacement; + } + + public String rewrite(String input) { + if (pattern == null) { + return input; + } + return input.replaceAll(pattern, replacement); + } + } } diff --git a/src/main/java/com/google/api/codegen/config/RetryGenerator.java b/src/main/java/com/google/api/codegen/config/RetryGenerator.java index 9b8681e03b..b0955fbc1c 100644 --- a/src/main/java/com/google/api/codegen/config/RetryGenerator.java +++ b/src/main/java/com/google/api/codegen/config/RetryGenerator.java @@ -68,9 +68,9 @@ private static List generateRetryParams() { retryParamsDefault.put("initial_retry_delay_millis", 100); retryParamsDefault.put("retry_delay_multiplier", 1.3); retryParamsDefault.put("max_retry_delay_millis", 60000); - retryParamsDefault.put("initial_rpc_timeout_millis", 60000); + retryParamsDefault.put("initial_rpc_timeout_millis", 20000); retryParamsDefault.put("rpc_timeout_multiplier", 1); - retryParamsDefault.put("max_rpc_timeout_millis", 60000); + retryParamsDefault.put("max_rpc_timeout_millis", 20000); retryParamsDefault.put("total_timeout_millis", 600000); List output = new LinkedList(); diff --git a/src/test/java/com/google/api/codegen/ConfigGenerationTest.java b/src/test/java/com/google/api/codegen/ConfigGenerationTest.java index 61d65ac888..a285e4236d 100644 --- a/src/test/java/com/google/api/codegen/ConfigGenerationTest.java +++ b/src/test/java/com/google/api/codegen/ConfigGenerationTest.java @@ -15,63 +15,45 @@ package com.google.api.codegen; import com.google.api.codegen.config.ConfigGeneratorApi; -import com.google.api.tools.framework.model.Model; -import com.google.api.tools.framework.model.testing.TestConfig; -import com.google.api.tools.framework.model.testing.TestDataLocator; +import com.google.api.tools.framework.model.testing.ConfigBaselineTestCase; import com.google.api.tools.framework.tools.ToolOptions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - import java.io.File; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.Collections; -public class ConfigGenerationTest { - @Rule public TemporaryFolder tempDir = new TemporaryFolder(); +import org.junit.Test; - @Test - public void config() throws Exception { - String testTarget = "library"; - String baselineName = testTarget + "_config.baseline"; +public class ConfigGenerationTest extends ConfigBaselineTestCase { + @Override + protected String baselineFileName() { + return "library_config.baseline"; + } - TestDataLocator locator = TestDataLocator.create(this.getClass()); - TestConfig testConfig = - new TestConfig( - locator, tempDir.getRoot().getPath(), Collections.singletonList(testTarget + ".proto")); - Model model = Model.create(testConfig.getDescriptor()); + @Override + protected boolean suppressDiagnosis() { + // Suppress linter warnings + return true; + } - String outFile = tempDir.getRoot().getPath() + File.separator + baselineName; + @Override + public Object run() throws Exception { + String outFile = tempDir.getRoot().getPath() + File.separator + baselineFileName(); ToolOptions options = ToolOptions.create(); options.set(ConfigGeneratorApi.OUTPUT_FILE, outFile); options.set(ToolOptions.DESCRIPTOR_SET, testConfig.getDescriptorFile().toString()); new ConfigGeneratorApi(options).run(); - URL baselineUrl = locator.findTestData(baselineName); - if (baselineUrl == null) { - throw new IllegalStateException(String.format("baseline not found: %s", baselineName)); - } - String baselineContent = locator.readTestData(baselineUrl); String outputContent = new String(Files.readAllBytes(Paths.get(outFile)), StandardCharsets.UTF_8); - if (!outputContent.equals(baselineContent)) { - Path saveFile = - Paths.get( - System.getProperty("java.io.tmpdir"), - String.format("%s_testdata", this.getClass().getPackage().getName()), - baselineName); - Files.createDirectories(saveFile.getParent()); - Files.copy(Paths.get(outFile), saveFile, StandardCopyOption.REPLACE_EXISTING); - throw new IllegalStateException( - String.format("baseline failed, output saved at %s", saveFile)); - } + return outputContent; + } + + @Test + public void library() throws Exception { + test("library"); } } diff --git a/src/test/java/com/google/api/codegen/testdata/library_config.baseline b/src/test/java/com/google/api/codegen/testdata/library_config.baseline index c5b894c92b..3b69b2e365 100644 --- a/src/test/java/com/google/api/codegen/testdata/library_config.baseline +++ b/src/test/java/com/google/api/codegen/testdata/library_config.baseline @@ -2,7 +2,7 @@ type: com.google.api.codegen.ConfigProto generate_samples: true language_settings: java: - package_name: com.google.example.library.v1 + package_name: com.google.cloud.example.library.spi.v1 python: package_name: google.example.library.v1 go: @@ -12,7 +12,7 @@ language_settings: ruby: package_name: Google::Example::Library::V1 php: - package_name: Google\Example\Library\V1 + package_name: Google\Cloud\Example\Library\V1 interfaces: - name: google.example.library.v1.LibraryService collections: @@ -34,9 +34,9 @@ interfaces: initial_retry_delay_millis: 100 retry_delay_multiplier: 1.3 max_retry_delay_millis: 60000 - initial_rpc_timeout_millis: 60000 + initial_rpc_timeout_millis: 20000 rpc_timeout_multiplier: 1 - max_rpc_timeout_millis: 60000 + max_rpc_timeout_millis: 20000 total_timeout_millis: 600000 methods: - name: CreateShelf @@ -49,7 +49,7 @@ interfaces: request_object_method: false retry_codes_name: non_idempotent retry_params_name: default - timeout_millis: 30000 + timeout_millis: 60000 - name: GetShelf flattening: groups: @@ -68,9 +68,9 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: ListShelves - request_object_method: false + request_object_method: true page_streaming: request: token_field: page_token @@ -79,7 +79,7 @@ interfaces: resources_field: shelves retry_codes_name: idempotent retry_params_name: default - timeout_millis: 30000 + timeout_millis: 60000 - name: DeleteShelf flattening: groups: @@ -92,7 +92,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: MergeShelves flattening: groups: @@ -107,7 +107,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: CreateBook flattening: groups: @@ -122,7 +122,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: PublishSeries flattening: groups: @@ -137,7 +137,7 @@ interfaces: request_object_method: true retry_codes_name: non_idempotent retry_params_name: default - timeout_millis: 30000 + timeout_millis: 60000 - name: GetBook flattening: groups: @@ -150,7 +150,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_2 - timeout_millis: 30000 + timeout_millis: 60000 - name: ListBooks flattening: groups: @@ -172,7 +172,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: DeleteBook flattening: groups: @@ -185,7 +185,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_2 - timeout_millis: 30000 + timeout_millis: 60000 - name: UpdateBook flattening: groups: @@ -204,7 +204,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_2 - timeout_millis: 30000 + timeout_millis: 60000 - name: MoveBook flattening: groups: @@ -219,7 +219,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_2 - timeout_millis: 30000 + timeout_millis: 60000 - name: ListStrings flattening: groups: @@ -227,7 +227,7 @@ interfaces: - name required_fields: - name - request_object_method: false + request_object_method: true page_streaming: request: page_size_field: page_size @@ -237,7 +237,7 @@ interfaces: resources_field: strings retry_codes_name: idempotent retry_params_name: default - timeout_millis: 30000 + timeout_millis: 60000 - name: AddComments flattening: groups: @@ -252,7 +252,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book_shelf - timeout_millis: 30000 + timeout_millis: 60000 - name: GetBookFromArchive flattening: groups: @@ -265,7 +265,7 @@ interfaces: retry_params_name: default field_name_patterns: name: book - timeout_millis: 30000 + timeout_millis: 60000 - name: UpdateBookIndex flattening: groups: @@ -282,4 +282,5 @@ interfaces: retry_params_name: default field_name_patterns: name: book_2 - timeout_millis: 30000 + timeout_millis: 60000 +