com.google.api.generator.gapic.protoparser.BatchingSettingsConfigParser Maven / Gradle / Ivy
// 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.GapicBatchingSettings;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
public class BatchingSettingsConfigParser {
private static String LIMIT_EXCEEDED_BEHAVIOR_THROW_EXCEPTION_YAML_VALUE = "THROW_EXCEPTION";
private static String LIMIT_EXCEEDED_BEHAVIOR_BLOCK_YAML_VALUE = "BLOCK";
private static String YAML_KEY_INTERFACES = "interfaces";
private static String YAML_KEY_NAME = "name";
private static String YAML_KEY_METHODS = "methods";
private static String YAML_KEY_BATCHING = "batching";
private static String YAML_KEY_THRESHOLDS = "thresholds";
private static String YAML_KEY_DESCRIPTOR = "batch_descriptor";
private static String YAML_KEY_BATCHING_ELEMENT_COUNT_THRESHOLD = "element_count_threshold";
private static String YAML_KEY_BATCHING_DELAY_THRESHOLD_MILLIS = "delay_threshold_millis";
private static String YAML_KEY_BATCHING_REQUEST_BYTE_THRESHOLD = "request_byte_threshold";
private static String YAML_KEY_BATCHING_FLOW_CONTROL_ELEMENT_LIMIT = "flow_control_element_limit";
private static String YAML_KEY_BATCHING_FLOW_CONTROL_BYTE_LIMIT = "flow_control_byte_limit";
private static String YAML_KEY_BATCHING_FLOW_CONTROL_LIMIT_EXCEEDED_BEHAVIOR =
"flow_control_limit_exceeded_behavior";
private static String YAML_KEY_DESCRIPTOR_BATCHED_FIELD = "batched_field";
private static String YAML_KEY_DESCRIPTOR_DISCRIMINATOR_FIELD = "discriminator_fields";
private static String YAML_KEY_DESCRIPTOR_SUBRESPONSE_FIELD = "subresponse_field";
public static Optional> parse(
Optional gapicYamlConfigFilePathOpt) {
return gapicYamlConfigFilePathOpt.isPresent()
? parse(gapicYamlConfigFilePathOpt.get())
: Optional.empty();
}
@VisibleForTesting
static Optional> 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)), StandardCharsets.UTF_8);
} catch (IOException e) {
return Optional.empty();
}
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
Map yamlMap = yaml.load(fileContents);
return parseFromMap(yamlMap);
}
private static Optional> parseFromMap(Map yamlMap) {
if (!yamlMap.containsKey(YAML_KEY_INTERFACES)) {
return Optional.empty();
}
List settings = new ArrayList<>();
for (Map serviceYamlConfig :
(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy