mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 08:27:14 +02:00
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:
parent
2fe82921a7
commit
dedabfb076
1 changed files with 19 additions and 9 deletions
|
@ -40,6 +40,24 @@ public final class YamlUtils {
|
||||||
private static final String YAML_CONFIG_PROD =
|
private static final String YAML_CONFIG_PROD =
|
||||||
readResourceUtf8(RegistryConfig.class, "files/default-config.yaml");
|
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.
|
* 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
|
* 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
|
* environment-specific config file named {@code nomulus-config-ENVIRONMENT.yaml} is also loaded
|
||||||
* and those values merged into the POJO.
|
* and those values merged into the POJO.
|
||||||
*
|
|
||||||
* @throws IllegalStateException if the configuration files don't exist or are invalid
|
|
||||||
*/
|
*/
|
||||||
static RegistryConfigSettings getConfigSettings() {
|
static RegistryConfigSettings getConfigSettings() {
|
||||||
String configFilePath =
|
String configFilePath =
|
||||||
String.format(ENVIRONMENT_CONFIG_FORMAT, toLowerCase(RegistryEnvironment.get().name()));
|
String.format(ENVIRONMENT_CONFIG_FORMAT, toLowerCase(RegistryEnvironment.get().name()));
|
||||||
String customYaml = readResourceUtf8(RegistryConfig.class, configFilePath);
|
String customYaml = readResourceUtf8(RegistryConfig.class, configFilePath);
|
||||||
try {
|
return getConfigSettings(YAML_CONFIG_PROD, customYaml, RegistryConfigSettings.class);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue