class YamlMapPropertySourceLoader extends YamlPropertySourceLoader {
@Override
public PropertySource load(String name, Resource resource, String profile) throws IOException {
if (ClassUtils.isPresent(Yaml.class.name, null)) {
final YamlMapFactoryBean bean = new YamlMapFactoryBean();
YamlMapFactoryBean factory = new YamlMapFactoryBean()
factory.setDocumentMatchers(new DefaultProfileDocumentMatcher(), new SpringProfileDocumentMatcher(profile))
factory.setResolutionMethod(ResolutionMethod.OVERRIDE)
factory.setResources([resource] as Resource[])
return new MapPropertySource(name, flattenMap(factory.getObject()))
}
null
}
private Map flattenMap(Map aMap, prefix=null) {
aMap.inject([:]) { map, entry ->
if(entry.value instanceof Map) {
map += flattenMap(entry.value, createKey(prefix, entry.key))
} else {
map."${createKey(prefix, entry.key)}" = entry.value
}
map
}
}
private String createKey(prefix, key) {
(prefix?.length() > 0) ? "${prefix}.${key}" : key
}
}