Skip to content

Commit bbc4c8a

Browse files
committed
Add avaje-config-toml
Signed-off-by: Mechite <[email protected]>
1 parent ae61daf commit bbc4c8a

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

avaje-config-toml/pom.xml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.avaje</groupId>
6+
<artifactId>java11-oss</artifactId>
7+
<version>4.4</version>
8+
<relativePath/>
9+
</parent>
10+
11+
<groupId>io.avaje</groupId>
12+
<artifactId>avaje-config-toml</artifactId>
13+
<version>4.1-SNAPSHOT</version>
14+
15+
<scm>
16+
<connection>scm:git:[email protected]:avaje/avaje-config.git</connection>
17+
<developerConnection>scm:git:[email protected]:avaje/avaje-config.git</developerConnection>
18+
<tag>HEAD</tag>
19+
</scm>
20+
21+
<properties>
22+
<snakeyaml.version>2.3</snakeyaml.version>
23+
<nexus.staging.autoReleaseAfterClose>true</nexus.staging.autoReleaseAfterClose>
24+
<surefire.useModulePath>false</surefire.useModulePath>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>io.avaje</groupId>
30+
<artifactId>avaje-config</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.tomlj</groupId>
35+
<artifactId>tomlj</artifactId>
36+
<version>1.1.1</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>io.avaje</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>1.5</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>ch.qos.logback</groupId>
48+
<artifactId>logback-classic</artifactId>
49+
<version>1.5.7</version>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>io.avaje</groupId>
55+
<artifactId>avaje-applog-slf4j</artifactId>
56+
<version>1.0</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-repository-plugin</artifactId>
66+
<version>2.4</version>
67+
</plugin>
68+
<!-- <plugin>-->
69+
<!-- <groupId>org.sonatype.plugins</groupId>-->
70+
<!-- <artifactId>nexus-staging-maven-plugin</artifactId>-->
71+
<!-- <version>1.7.0</version>-->
72+
<!-- <extensions>true</extensions>-->
73+
<!-- <configuration>-->
74+
<!-- <serverId>ossrh</serverId>-->
75+
<!-- <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>-->
76+
<!-- <autoReleaseAfterClose>${nexus.staging.autoReleaseAfterClose}</autoReleaseAfterClose>-->
77+
<!-- </configuration>-->
78+
<!-- </plugin>-->
79+
</plugins>
80+
</build>
81+
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.avaje.config.toml;
2+
3+
import io.avaje.config.ConfigParser;
4+
import org.jspecify.annotations.NullMarked;
5+
import org.tomlj.Toml;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.Reader;
10+
import java.io.UncheckedIOException;
11+
import java.util.Map;
12+
import java.util.stream.Collectors;
13+
14+
@NullMarked
15+
final class TomlParser implements ConfigParser {
16+
17+
private static final String[] extensions = {"toml"};
18+
19+
@Override
20+
public String[] supportedExtensions() {
21+
return extensions;
22+
}
23+
24+
@Override
25+
public Map<String, String> load(Reader reader) {
26+
try {
27+
return Toml.parse(reader).dottedEntrySet()
28+
.stream()
29+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue())));
30+
} catch (IOException exception) {
31+
throw new UncheckedIOException(exception);
32+
}
33+
}
34+
35+
@Override
36+
public Map<String, String> load(InputStream is) {
37+
try {
38+
return Toml.parse(is).dottedEntrySet()
39+
.stream()
40+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> String.valueOf(entry.getValue())));
41+
} catch (IOException exception) {
42+
throw new UncheckedIOException(exception);
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module io.avaje.config.toml {
2+
3+
requires io.avaje.config;
4+
requires org.tomlj;
5+
6+
exports io.avaje.config.toml;
7+
8+
uses io.avaje.config.ConfigExtension;
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.avaje.config.toml;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.StringReader;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.Map;
9+
10+
import static org.assertj.core.api.Assertions.*;
11+
12+
public class TomlParserTest {
13+
14+
@Test
15+
void supportedExtensions() {
16+
var parser = new TomlParser();
17+
assertThat(parser.supportedExtensions()).isEqualTo(new String[]{"toml"});
18+
}
19+
20+
private static String input() {
21+
return "key3 = \"c\"\n" +
22+
"\n" +
23+
"[one]\n" +
24+
"key = \"a\"\n" +
25+
"key2 = \"b\"\n";
26+
}
27+
28+
@Test
29+
void load_reader() {
30+
var parser = new TomlParser();
31+
Map<String, String> map = parser.load(new StringReader(input()));
32+
33+
assertThat(map).hasSize(3);
34+
assertThat(map).containsOnlyKeys("one.key", "one.key2", "key3");
35+
assertThat(map).containsEntry("one.key", "a");
36+
assertThat(map).containsEntry("one.key2", "b");
37+
assertThat(map).containsEntry("key3", "c");
38+
}
39+
40+
@Test
41+
void load_inputStream() {
42+
var parser = new TomlParser();
43+
Map<String, String> map = parser.load(new ByteArrayInputStream(input().getBytes(StandardCharsets.UTF_8)));
44+
45+
assertThat(map).hasSize(3);
46+
assertThat(map).containsOnlyKeys("one.key", "one.key2", "key3");
47+
assertThat(map).containsEntry("one.key", "a");
48+
assertThat(map).containsEntry("one.key2", "b");
49+
assertThat(map).containsEntry("key3", "c");
50+
}
51+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<modules>
1717
<module>avaje-config</module>
1818
<module>avaje-config-json</module>
19+
<module>avaje-config-toml</module>
1920
<module>avaje-aws-appconfig</module>
2021
<module>avaje-dynamic-logback</module>
2122
</modules>

0 commit comments

Comments
 (0)