1package dev.rafex.ether.config.sources;
29import java.io.IOException;
30import java.nio.file.Path;
33import com.fasterxml.jackson.core.type.TypeReference;
34import com.fasterxml.jackson.databind.ObjectMapper;
35import com.fasterxml.jackson.databind.SerializationFeature;
36import com.fasterxml.jackson.dataformat.toml.TomlMapper;
37import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
38import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
40import dev.rafex.ether.config.internal.StructuredConfigSupport;
44 private static final TypeReference<Map<String, Object>> MAP_TYPE =
new TypeReference<>() {
47 StructuredConfigSource(
final String name,
final Map<String, String> values) {
51 static Map<String, String> loadJson(
final Path path)
throws IOException {
52 return load(path, mapper());
55 static Map<String, String> loadYaml(
final Path path)
throws IOException {
56 return load(path, YAMLMapper.builder().addModule(
new JavaTimeModule()).build());
59 static Map<String, String> loadToml(
final Path path)
throws IOException {
60 return load(path, TomlMapper.builder().addModule(
new JavaTimeModule()).build());
63 private static Map<String, String> load(
final Path path,
final ObjectMapper mapper)
throws IOException {
64 final Map<String, Object> tree = mapper.readValue(path.toFile(), MAP_TYPE);
65 return StructuredConfigSupport.flatten(tree);
68 private static ObjectMapper mapper() {
69 return new ObjectMapper().registerModule(
new JavaTimeModule())
70 .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);