FIX YAML Loader throw ex on missing key

master
Niclas Thobaben 2020-12-04 23:16:13 +01:00
parent 6b4c1b02da
commit b9322ebf24
2 changed files with 11 additions and 6 deletions

View File

@ -14,20 +14,22 @@ import static java.util.stream.Collectors.toSet;
public class YamlResourceManagerLoader {
private static final Yaml YAML = new Yaml(new Constructor(YamlResourceManager.class));
public ResourceManager load(InputStream stream) {
Yaml yaml = new Yaml(new Constructor(YamlResourceManager.class));
YamlResourceManager yamlResourceManager = yaml.load(stream);
YamlResourceManager yamlResourceManager = YAML.load(stream);
return mapYamlResourceManager(yamlResourceManager);
}
private ResourceManager mapYamlResourceManager(YamlResourceManager yaml) {
ResourceManager manager = new ResourceManager();
for (YamlResource yamlResource : yaml.getResources()) {
manager.addMapping(yamlResource.getKey(), mapYamlResourceMapping(yamlResource));
String key = yamlResource.getKey();
if (key == null) {
throw new IllegalArgumentException("Missing Resource Key!");
}
manager.addMapping(key, mapYamlResourceMapping(yamlResource));
}
return manager;
@ -35,11 +37,13 @@ public class YamlResourceManagerLoader {
private ResourceMapping mapYamlResourceMapping(YamlResource resource) {
Set<String> options = resource.getOptions() != null ? resource.getOptions() : Collections.emptySet();
return ResourceMapping.builder()
.defaultResource(Resource.load(resource.getDefaultLocation()))
.label(resource.getLabel())
.description(resource.getDescription())
.options(options.stream()
.map(String::toUpperCase)
.map(ResourceMapping.Option::valueOf)
.collect(toSet()))
.build();

View File

@ -3,6 +3,7 @@ resources:
label: Airplane Image
description: An image of an airplane
options:
- required
defaultLocation: classpath:/data/airplane.png
types:
- image/png