Skip to content

Commit 8a1834d

Browse files
authored
feat: add parsing for language_settings in gapic.yaml (#554)
1 parent 1507363 commit 8a1834d

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.gapic.model;
16+
17+
import com.google.auto.value.AutoValue;
18+
import com.google.common.collect.ImmutableMap;
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
/** Represents the LRO retry settings in a gapic.yaml file. */
23+
@AutoValue
24+
public abstract class GapicLanguageSettings {
25+
// The Java package mapping.
26+
public abstract String pakkage();
27+
28+
// Private.
29+
abstract ImmutableMap<String, String> protoServiceToJavaClass();
30+
31+
public String getJavaFullName(String protoPackage, String protoRpcName) {
32+
String protoFullName = String.format("%s.%s", protoPackage, protoRpcName);
33+
String finalProtoRpcName = protoRpcName;
34+
if (protoServiceToJavaClass().containsKey(protoFullName)) {
35+
finalProtoRpcName = protoServiceToJavaClass().get(protoFullName);
36+
}
37+
return String.format("%s.%s", pakkage(), finalProtoRpcName);
38+
}
39+
40+
public static Builder builder() {
41+
return new AutoValue_GapicLanguageSettings.Builder()
42+
.setProtoServiceToJavaClass(Collections.emptyMap());
43+
}
44+
45+
@AutoValue.Builder
46+
public abstract static class Builder {
47+
public abstract Builder setPakkage(String pakkage);
48+
49+
public abstract Builder setProtoServiceToJavaClass(Map<String, String> protoServiceToJavaClass);
50+
51+
public abstract GapicLanguageSettings build();
52+
}
53+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.gapic.protoparser;
16+
17+
import com.google.api.generator.gapic.model.GapicLanguageSettings;
18+
import com.google.common.annotations.VisibleForTesting;
19+
import com.google.common.base.Strings;
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
24+
import java.util.Map;
25+
import java.util.Optional;
26+
import org.yaml.snakeyaml.Yaml;
27+
import org.yaml.snakeyaml.constructor.SafeConstructor;
28+
29+
public class GapicLanguageSettingsParser {
30+
private static final String YAML_KEY_LANGUAGE_SETTINGS = "language_settings";
31+
private static final String YAML_KEY_JAVA = "java";
32+
private static final String YAML_KEY_PACKAGE_NAME = "package_name";
33+
private static final String YAML_KEY_INTERFACE_NAMES = "interface_names";
34+
35+
public static Optional<GapicLanguageSettings> parse(Optional<String> gapicYamlConfigFilePathOpt) {
36+
return gapicYamlConfigFilePathOpt.isPresent()
37+
? parse(gapicYamlConfigFilePathOpt.get())
38+
: Optional.empty();
39+
}
40+
41+
@VisibleForTesting
42+
static Optional<GapicLanguageSettings> parse(String gapicYamlConfigFilePath) {
43+
if (Strings.isNullOrEmpty(gapicYamlConfigFilePath)
44+
|| !(new File(gapicYamlConfigFilePath)).exists()) {
45+
return Optional.empty();
46+
}
47+
48+
String fileContents = null;
49+
50+
try {
51+
fileContents = new String(Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)));
52+
} catch (IOException e) {
53+
return Optional.empty();
54+
}
55+
56+
Yaml yaml = new Yaml(new SafeConstructor());
57+
Map<String, Object> yamlMap = yaml.load(fileContents);
58+
return parseFromMap(yamlMap);
59+
}
60+
61+
private static Optional<GapicLanguageSettings> parseFromMap(Map<String, Object> yamlMap) {
62+
if (!yamlMap.containsKey(YAML_KEY_LANGUAGE_SETTINGS)) {
63+
return Optional.empty();
64+
}
65+
66+
Map<String, Object> languageYamlConfig =
67+
(Map<String, Object>) yamlMap.get(YAML_KEY_LANGUAGE_SETTINGS);
68+
if (!languageYamlConfig.containsKey(YAML_KEY_JAVA)) {
69+
return Optional.empty();
70+
}
71+
72+
Map<String, Object> javaYamlConfig =
73+
(Map<String, Object>) languageYamlConfig.get(YAML_KEY_JAVA);
74+
if (!javaYamlConfig.containsKey(YAML_KEY_PACKAGE_NAME)) {
75+
return Optional.empty();
76+
}
77+
78+
GapicLanguageSettings.Builder gapicLanguageSettingsBuilder =
79+
GapicLanguageSettings.builder()
80+
.setPakkage((String) javaYamlConfig.get(YAML_KEY_PACKAGE_NAME));
81+
82+
if (!javaYamlConfig.containsKey(YAML_KEY_INTERFACE_NAMES)) {
83+
return Optional.of(gapicLanguageSettingsBuilder.build());
84+
}
85+
86+
return Optional.of(
87+
gapicLanguageSettingsBuilder
88+
.setProtoServiceToJavaClass(
89+
(Map<String, String>) javaYamlConfig.get(YAML_KEY_INTERFACE_NAMES))
90+
.build());
91+
}
92+
}

src/test/java/com/google/api/generator/gapic/protoparser/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package(default_visibility = ["//visibility:public"])
44

55
TESTS = [
66
"BatchingSettingsConfigParserTest",
7+
"GapicLanguageSettingsParserTest",
78
"GapicLroRetrySettingsParserTest",
89
"HttpRuleParserTest",
910
"MethodSignatureParserTest",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.gapic.protoparser;
16+
17+
import static junit.framework.Assert.assertEquals;
18+
import static junit.framework.Assert.assertTrue;
19+
20+
import com.google.api.generator.gapic.model.GapicLanguageSettings;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.util.Optional;
24+
import org.junit.Test;
25+
26+
public class GapicLanguageSettingsParserTest {
27+
private static final String YAML_DIRECTORY =
28+
"src/test/java/com/google/api/generator/gapic/testdata/";
29+
30+
@Test
31+
public void parseLanguageSettings_onlyInterfacePresent() {
32+
String filename = "datastore_gapic.yaml";
33+
Path path = Paths.get(YAML_DIRECTORY, filename);
34+
Optional<GapicLanguageSettings> settingsOpt =
35+
GapicLanguageSettingsParser.parse(path.toString());
36+
assertTrue(settingsOpt.isPresent());
37+
GapicLanguageSettings settings = settingsOpt.get();
38+
assertEquals("com.google.cloud.datastore.v1", settings.pakkage());
39+
assertEquals(
40+
"com.google.cloud.datastore.v1.FooBar",
41+
settings.getJavaFullName("google.datastore.v1", "FooBar"));
42+
}
43+
44+
@Test
45+
public void parseLanguageSettings_methodNameOverridesPresent() {
46+
String filename = "logging_gapic.yaml";
47+
Path path = Paths.get(YAML_DIRECTORY, filename);
48+
Optional<GapicLanguageSettings> settingsOpt =
49+
GapicLanguageSettingsParser.parse(path.toString());
50+
assertTrue(settingsOpt.isPresent());
51+
GapicLanguageSettings settings = settingsOpt.get();
52+
assertEquals("com.google.cloud.logging.v2", settings.pakkage());
53+
assertEquals(
54+
"com.google.cloud.logging.v2.Logging",
55+
settings.getJavaFullName("google.logging.v2", "LoggingServiceV2"));
56+
}
57+
}

0 commit comments

Comments
 (0)