From bbc4c8a0517afc7ceb205c52f3404e38e2c54502 Mon Sep 17 00:00:00 2001 From: Mechite Date: Fri, 6 Sep 2024 23:38:10 +0100 Subject: [PATCH] Add avaje-config-toml Signed-off-by: Mechite --- avaje-config-toml/pom.xml | 82 +++++++++++++++++++ .../java/io/avaje/config/toml/TomlParser.java | 45 ++++++++++ .../src/main/java/module-info.java | 10 +++ .../io/avaje/config/toml/TomlParserTest.java | 51 ++++++++++++ pom.xml | 1 + 5 files changed, 189 insertions(+) create mode 100644 avaje-config-toml/pom.xml create mode 100644 avaje-config-toml/src/main/java/io/avaje/config/toml/TomlParser.java create mode 100644 avaje-config-toml/src/main/java/module-info.java create mode 100644 avaje-config-toml/src/test/java/io/avaje/config/toml/TomlParserTest.java diff --git a/avaje-config-toml/pom.xml b/avaje-config-toml/pom.xml new file mode 100644 index 0000000..3f80aec --- /dev/null +++ b/avaje-config-toml/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + org.avaje + java11-oss + 4.4 + + + + io.avaje + avaje-config-toml + 4.1-SNAPSHOT + + + scm:git:git@github.com:avaje/avaje-config.git + scm:git:git@github.com:avaje/avaje-config.git + HEAD + + + + 2.3 + true + false + + + + + io.avaje + avaje-config + ${project.version} + + + org.tomlj + tomlj + 1.1.1 + + + + io.avaje + junit + 1.5 + test + + + + ch.qos.logback + logback-classic + 1.5.7 + test + + + + io.avaje + avaje-applog-slf4j + 1.0 + test + + + + + + + org.apache.maven.plugins + maven-repository-plugin + 2.4 + + + + + + + + + + + + + + + + diff --git a/avaje-config-toml/src/main/java/io/avaje/config/toml/TomlParser.java b/avaje-config-toml/src/main/java/io/avaje/config/toml/TomlParser.java new file mode 100644 index 0000000..826d453 --- /dev/null +++ b/avaje-config-toml/src/main/java/io/avaje/config/toml/TomlParser.java @@ -0,0 +1,45 @@ +package io.avaje.config.toml; + +import io.avaje.config.ConfigParser; +import org.jspecify.annotations.NullMarked; +import org.tomlj.Toml; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.io.UncheckedIOException; +import java.util.Map; +import java.util.stream.Collectors; + +@NullMarked +final class TomlParser implements ConfigParser { + + private static final String[] extensions = {"toml"}; + + @Override + public String[] supportedExtensions() { + return extensions; + } + + @Override + public Map load(Reader reader) { + try { + return Toml.parse(reader).dottedEntrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue()))); + } catch (IOException exception) { + throw new UncheckedIOException(exception); + } + } + + @Override + public Map load(InputStream is) { + try { + return Toml.parse(is).dottedEntrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue()))); + } catch (IOException exception) { + throw new UncheckedIOException(exception); + } + } +} diff --git a/avaje-config-toml/src/main/java/module-info.java b/avaje-config-toml/src/main/java/module-info.java new file mode 100644 index 0000000..e851650 --- /dev/null +++ b/avaje-config-toml/src/main/java/module-info.java @@ -0,0 +1,10 @@ +module io.avaje.config.toml { + + requires io.avaje.config; + requires org.tomlj; + + exports io.avaje.config.toml; + + uses io.avaje.config.ConfigExtension; + +} diff --git a/avaje-config-toml/src/test/java/io/avaje/config/toml/TomlParserTest.java b/avaje-config-toml/src/test/java/io/avaje/config/toml/TomlParserTest.java new file mode 100644 index 0000000..81e07fe --- /dev/null +++ b/avaje-config-toml/src/test/java/io/avaje/config/toml/TomlParserTest.java @@ -0,0 +1,51 @@ +package io.avaje.config.toml; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +import static org.assertj.core.api.Assertions.*; + +public class TomlParserTest { + + @Test + void supportedExtensions() { + var parser = new TomlParser(); + assertThat(parser.supportedExtensions()).isEqualTo(new String[]{"toml"}); + } + + private static String input() { + return "key3 = \"c\"\n" + + "\n" + + "[one]\n" + + "key = \"a\"\n" + + "key2 = \"b\"\n"; + } + + @Test + void load_reader() { + var parser = new TomlParser(); + Map map = parser.load(new StringReader(input())); + + assertThat(map).hasSize(3); + assertThat(map).containsOnlyKeys("one.key", "one.key2", "key3"); + assertThat(map).containsEntry("one.key", "a"); + assertThat(map).containsEntry("one.key2", "b"); + assertThat(map).containsEntry("key3", "c"); + } + + @Test + void load_inputStream() { + var parser = new TomlParser(); + Map map = parser.load(new ByteArrayInputStream(input().getBytes(StandardCharsets.UTF_8))); + + assertThat(map).hasSize(3); + assertThat(map).containsOnlyKeys("one.key", "one.key2", "key3"); + assertThat(map).containsEntry("one.key", "a"); + assertThat(map).containsEntry("one.key2", "b"); + assertThat(map).containsEntry("key3", "c"); + } +} diff --git a/pom.xml b/pom.xml index ec53806..9882b15 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ avaje-config avaje-config-json + avaje-config-toml avaje-aws-appconfig avaje-dynamic-logback