I use the following code to directly access any property in the structure of nested maps, as in the example.
import com.google.common.collect.ImmutableMap;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.core.env.MapPropertySource;
import java.util.Map;
public class MapPropertySourceLearningTest {
@Test
public void testFlattenedMap() {
Map map = ImmutableMap.of(
"meta", ImmutableMap.of(
"pagination", ImmutableMap.of(
"position", "1",
"itemsPerPage", "50",
"totalPages", "9",
"totalItems", "438"
)
)
);
MapPropertySource source = new MapPropertySource("map", new YamlProcessor() {
public Map<String, Object> flatten(Map<String, Object> source) {
return super.getFlattenedMap(source);
}
}.flatten(map));
Assert.assertEquals("1", source.getProperty("meta.pagination.position"));
Assert.assertEquals("9", source.getProperty("meta.pagination.totalPages"));
}
}
I do not like to extend the YamlProcessor class. ¿Is there a better way to do the same?
source
share