Skip to content

Commit

Permalink
feat: add parsing for language_settings in gapic.yaml (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung authored Nov 24, 2020
1 parent 1507363 commit 8a1834d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.model;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.Map;

/** Represents the LRO retry settings in a gapic.yaml file. */
@AutoValue
public abstract class GapicLanguageSettings {
// The Java package mapping.
public abstract String pakkage();

// Private.
abstract ImmutableMap<String, String> protoServiceToJavaClass();

public String getJavaFullName(String protoPackage, String protoRpcName) {
String protoFullName = String.format("%s.%s", protoPackage, protoRpcName);
String finalProtoRpcName = protoRpcName;
if (protoServiceToJavaClass().containsKey(protoFullName)) {
finalProtoRpcName = protoServiceToJavaClass().get(protoFullName);
}
return String.format("%s.%s", pakkage(), finalProtoRpcName);
}

public static Builder builder() {
return new AutoValue_GapicLanguageSettings.Builder()
.setProtoServiceToJavaClass(Collections.emptyMap());
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setPakkage(String pakkage);

public abstract Builder setProtoServiceToJavaClass(Map<String, String> protoServiceToJavaClass);

public abstract GapicLanguageSettings build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.protoparser;

import com.google.api.generator.gapic.model.GapicLanguageSettings;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public class GapicLanguageSettingsParser {
private static final String YAML_KEY_LANGUAGE_SETTINGS = "language_settings";
private static final String YAML_KEY_JAVA = "java";
private static final String YAML_KEY_PACKAGE_NAME = "package_name";
private static final String YAML_KEY_INTERFACE_NAMES = "interface_names";

public static Optional<GapicLanguageSettings> parse(Optional<String> gapicYamlConfigFilePathOpt) {
return gapicYamlConfigFilePathOpt.isPresent()
? parse(gapicYamlConfigFilePathOpt.get())
: Optional.empty();
}

@VisibleForTesting
static Optional<GapicLanguageSettings> parse(String gapicYamlConfigFilePath) {
if (Strings.isNullOrEmpty(gapicYamlConfigFilePath)
|| !(new File(gapicYamlConfigFilePath)).exists()) {
return Optional.empty();
}

String fileContents = null;

try {
fileContents = new String(Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)));
} catch (IOException e) {
return Optional.empty();
}

Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> yamlMap = yaml.load(fileContents);
return parseFromMap(yamlMap);
}

private static Optional<GapicLanguageSettings> parseFromMap(Map<String, Object> yamlMap) {
if (!yamlMap.containsKey(YAML_KEY_LANGUAGE_SETTINGS)) {
return Optional.empty();
}

Map<String, Object> languageYamlConfig =
(Map<String, Object>) yamlMap.get(YAML_KEY_LANGUAGE_SETTINGS);
if (!languageYamlConfig.containsKey(YAML_KEY_JAVA)) {
return Optional.empty();
}

Map<String, Object> javaYamlConfig =
(Map<String, Object>) languageYamlConfig.get(YAML_KEY_JAVA);
if (!javaYamlConfig.containsKey(YAML_KEY_PACKAGE_NAME)) {
return Optional.empty();
}

GapicLanguageSettings.Builder gapicLanguageSettingsBuilder =
GapicLanguageSettings.builder()
.setPakkage((String) javaYamlConfig.get(YAML_KEY_PACKAGE_NAME));

if (!javaYamlConfig.containsKey(YAML_KEY_INTERFACE_NAMES)) {
return Optional.of(gapicLanguageSettingsBuilder.build());
}

return Optional.of(
gapicLanguageSettingsBuilder
.setProtoServiceToJavaClass(
(Map<String, String>) javaYamlConfig.get(YAML_KEY_INTERFACE_NAMES))
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package(default_visibility = ["//visibility:public"])

TESTS = [
"BatchingSettingsConfigParserTest",
"GapicLanguageSettingsParserTest",
"GapicLroRetrySettingsParserTest",
"HttpRuleParserTest",
"MethodSignatureParserTest",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.protoparser;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import com.google.api.generator.gapic.model.GapicLanguageSettings;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import org.junit.Test;

public class GapicLanguageSettingsParserTest {
private static final String YAML_DIRECTORY =
"src/test/java/com/google/api/generator/gapic/testdata/";

@Test
public void parseLanguageSettings_onlyInterfacePresent() {
String filename = "datastore_gapic.yaml";
Path path = Paths.get(YAML_DIRECTORY, filename);
Optional<GapicLanguageSettings> settingsOpt =
GapicLanguageSettingsParser.parse(path.toString());
assertTrue(settingsOpt.isPresent());
GapicLanguageSettings settings = settingsOpt.get();
assertEquals("com.google.cloud.datastore.v1", settings.pakkage());
assertEquals(
"com.google.cloud.datastore.v1.FooBar",
settings.getJavaFullName("google.datastore.v1", "FooBar"));
}

@Test
public void parseLanguageSettings_methodNameOverridesPresent() {
String filename = "logging_gapic.yaml";
Path path = Paths.get(YAML_DIRECTORY, filename);
Optional<GapicLanguageSettings> settingsOpt =
GapicLanguageSettingsParser.parse(path.toString());
assertTrue(settingsOpt.isPresent());
GapicLanguageSettings settings = settingsOpt.get();
assertEquals("com.google.cloud.logging.v2", settings.pakkage());
assertEquals(
"com.google.cloud.logging.v2.Logging",
settings.getJavaFullName("google.logging.v2", "LoggingServiceV2"));
}
}

0 comments on commit 8a1834d

Please sign in to comment.