|
| 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 | +} |
0 commit comments