Make YamlUtils work with arbitrary classes

This makes it possible to use YamlUtils to configure the proxy.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=165639295
This commit is contained in:
jianglai 2017-08-17 15:37:36 -07:00 committed by Ben McIlwain
parent 2fe82921a7
commit dedabfb076

View file

@ -40,6 +40,24 @@ public final class YamlUtils {
private static final String YAML_CONFIG_PROD =
readResourceUtf8(RegistryConfig.class, "files/default-config.yaml");
/**
* Loads the POJO of type {@code T} from merged YAML configuration files.
*
* @param defaultYaml content of the default YAML file.
* @param customYaml content of the custom YAML file, to override default values.
* @param clazz type of the POJO loaded from the merged YAML files.
* @throws IllegalStateException if the configuration files don't exist or are invalid
*/
public static <T> T getConfigSettings(String defaultYaml, String customYaml, Class<T> clazz) {
try {
String mergedYaml = mergeYaml(defaultYaml, customYaml);
return new Yaml().loadAs(mergedYaml, clazz);
} catch (Exception e) {
throw new IllegalStateException(
"Fatal error: Environment configuration YAML file is invalid", e);
}
}
/**
* Loads the {@link RegistryConfigSettings} POJO from the YAML configuration files.
*
@ -47,20 +65,12 @@ public final class YamlUtils {
* thrown if it cannot be found or if there is an error parsing it. Separately, the
* environment-specific config file named {@code nomulus-config-ENVIRONMENT.yaml} is also loaded
* and those values merged into the POJO.
*
* @throws IllegalStateException if the configuration files don't exist or are invalid
*/
static RegistryConfigSettings getConfigSettings() {
String configFilePath =
String.format(ENVIRONMENT_CONFIG_FORMAT, toLowerCase(RegistryEnvironment.get().name()));
String customYaml = readResourceUtf8(RegistryConfig.class, configFilePath);
try {
String mergedYaml = mergeYaml(YAML_CONFIG_PROD, customYaml);
return new Yaml().loadAs(mergedYaml, RegistryConfigSettings.class);
} catch (Exception e) {
throw new IllegalStateException(
"Fatal error: Environment configuration YAML file is invalid", e);
}
return getConfigSettings(YAML_CONFIG_PROD, customYaml, RegistryConfigSettings.class);
}
/**