Skip to content

Commit

Permalink
Add avaje-config-toml
Browse files Browse the repository at this point in the history
Signed-off-by: Mechite <[email protected]>
  • Loading branch information
Mechite committed Sep 6, 2024
1 parent ae61daf commit bbc4c8a
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
82 changes: 82 additions & 0 deletions avaje-config-toml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.avaje</groupId>
<artifactId>java11-oss</artifactId>
<version>4.4</version>
<relativePath/>
</parent>

<groupId>io.avaje</groupId>
<artifactId>avaje-config-toml</artifactId>
<version>4.1-SNAPSHOT</version>

<scm>
<connection>scm:git:[email protected]:avaje/avaje-config.git</connection>
<developerConnection>scm:git:[email protected]:avaje/avaje-config.git</developerConnection>
<tag>HEAD</tag>
</scm>

<properties>
<snakeyaml.version>2.3</snakeyaml.version>
<nexus.staging.autoReleaseAfterClose>true</nexus.staging.autoReleaseAfterClose>
<surefire.useModulePath>false</surefire.useModulePath>
</properties>

<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.tomlj</groupId>
<artifactId>tomlj</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-applog-slf4j</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-repository-plugin</artifactId>
<version>2.4</version>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.sonatype.plugins</groupId>-->
<!-- <artifactId>nexus-staging-maven-plugin</artifactId>-->
<!-- <version>1.7.0</version>-->
<!-- <extensions>true</extensions>-->
<!-- <configuration>-->
<!-- <serverId>ossrh</serverId>-->
<!-- <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>-->
<!-- <autoReleaseAfterClose>${nexus.staging.autoReleaseAfterClose}</autoReleaseAfterClose>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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);
}
}
}
10 changes: 10 additions & 0 deletions avaje-config-toml/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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");
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<modules>
<module>avaje-config</module>
<module>avaje-config-json</module>
<module>avaje-config-toml</module>
<module>avaje-aws-appconfig</module>
<module>avaje-dynamic-logback</module>
</modules>
Expand Down

0 comments on commit bbc4c8a

Please sign in to comment.