forked from vert-x3/vertx-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses vert-x3#49 Using toml4j adds TOML support. Note: I have used Jetbrains annotations in this commit, if that is not desired I can remove them and stick with standard Java 8 methods of ensuring method contracts when it comes to null values.
- Loading branch information
Lyndon Armitage
committed
Sep 24, 2018
1 parent
f9b737a
commit 050929d
Showing
15 changed files
with
732 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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>io.vertx</groupId> | ||
<artifactId>vertx-config-parent</artifactId> | ||
<version>3.6.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>vertx-config-toml</artifactId> | ||
|
||
<properties> | ||
<doc.skip>false</doc.skip> | ||
<toml4j.version>0.7.2</toml4j.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.vertx</groupId> | ||
<artifactId>vertx-config</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.moandjiezana.toml</groupId> | ||
<artifactId>toml4j</artifactId> | ||
<version>${toml4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains</groupId> | ||
<artifactId>annotations</artifactId> | ||
<version>16.0.1</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== TOML Configuration Format | ||
|
||
The TOML Configuration Format extends the Vert.x Configuration Retriever and | ||
provides the support for the TOML Configuration Format. | ||
|
||
==== Using the TOML Configuration Format | ||
|
||
To use the TOML Configuration Format, add the following dependency to the | ||
_dependencies_ section of your build descriptor: | ||
|
||
* Maven (in your `pom.xml`): | ||
|
||
[source,xml,subs="+attributes"] | ||
---- | ||
<dependency> | ||
<groupId>io.vertx.config.toml</groupId> | ||
<artifactId>vertx-config-toml</artifactId> | ||
<version>${maven.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.vertx</groupId> | ||
<artifactId>vertx-config</artifactId> | ||
<version>${maven.version}</version> | ||
</dependency> | ||
---- | ||
|
||
* Gradle (in your `build.gradle` file): | ||
|
||
[source,groovy,subs="+attributes"] | ||
---- | ||
compile 'io.vertx:vertx-config:${maven.version}' | ||
compile 'io.vertx.config.toml:vertx-config-toml:${maven.version}' | ||
---- | ||
|
||
==== Configuring the store to use TOML | ||
|
||
Once added to your classpath or dependencies, you need to configure the | ||
{@link io.vertx.config.ConfigRetriever} to use this format: | ||
|
||
[source, $lang] | ||
---- | ||
{@link examples.ConfigTomlExamples#example1(io.vertx.core.Vertx)} | ||
---- | ||
|
||
You just need to set `format` to `toml`. |
29 changes: 29 additions & 0 deletions
29
vertx-config-toml/src/main/java/examples/ConfigTomlExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package examples; | ||
|
||
import io.vertx.config.ConfigRetriever; | ||
import io.vertx.config.ConfigRetrieverOptions; | ||
import io.vertx.config.ConfigStoreOptions; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
/** | ||
* @author <a href='lyndon.codes'>Lyndon Armitage</a> | ||
*/ | ||
public final class ConfigTomlExamples { | ||
|
||
|
||
public void example1(Vertx vertx) { | ||
JsonObject storeConfig = new JsonObject().put("path", "my-config.toml"); | ||
ConfigStoreOptions storeOptions = new ConfigStoreOptions() | ||
.setType("file") | ||
.setFormat("toml") | ||
.setConfig(storeConfig); | ||
|
||
ConfigRetriever retriever = ConfigRetriever.create( | ||
vertx, | ||
new ConfigRetrieverOptions().addStore(storeOptions) | ||
); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
@Source | ||
package examples; | ||
|
||
import io.vertx.docgen.Source; |
84 changes: 84 additions & 0 deletions
84
vertx-config-toml/src/main/java/io/vertx/config/toml/TomlProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.vertx.config.toml; | ||
|
||
import com.moandjiezana.toml.Toml; | ||
import io.vertx.config.spi.ConfigProcessor; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonObject; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <p> | ||
* A processor for the <a href='https://github.com/toml-lang/toml'>TOML</a> | ||
* format. | ||
* </p> | ||
* <p> | ||
* Currently using the | ||
* <a href='https://github.com/mwanji/toml4j'>toml4j library</a>. If a newer | ||
* version of this library becomes available, you can override the dependency | ||
* (assuming the API does not change). | ||
* </p> | ||
* | ||
* @author <a href='lyndon.codes'>Lyndon Armitage</a> | ||
*/ | ||
public final class TomlProcessor implements ConfigProcessor { | ||
|
||
public TomlProcessor() { | ||
} | ||
|
||
@NotNull | ||
@Contract(pure = true) | ||
@Override | ||
public final String name() { | ||
return "toml"; | ||
} | ||
|
||
@Override | ||
public final void process( | ||
@NotNull Vertx vertx, | ||
@Nullable JsonObject configuration, | ||
@NotNull Buffer input, | ||
@NotNull Handler<AsyncResult<JsonObject>> handler | ||
) { | ||
Objects.requireNonNull(vertx, "vertx cannot be null"); | ||
Objects.requireNonNull(input, "input cannot be null"); | ||
Objects.requireNonNull(handler, "handler cannot be null"); | ||
|
||
if (input.length() == 0) { | ||
handler.handle(Future.succeededFuture(new JsonObject())); | ||
return; | ||
} | ||
|
||
vertx.executeBlocking( | ||
future -> { | ||
// Currently loads the whole config file into memory | ||
try ( | ||
ByteArrayInputStream inputStream = new ByteArrayInputStream( | ||
input.getBytes() | ||
) | ||
) { | ||
|
||
Toml toml = new Toml().read(inputStream); | ||
Map<String, Object> asMap = Objects.requireNonNull( | ||
toml.toMap(), | ||
"toml library returned a null map" | ||
); | ||
JsonObject asJson = new JsonObject(asMap); | ||
future.complete(asJson); | ||
} catch (Exception e) { | ||
future.fail(e); | ||
} | ||
}, | ||
handler | ||
); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
vertx-config-toml/src/main/java/io/vertx/config/toml/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@ModuleGen(name = "vertx-config", groupPackage = "io.vertx") | ||
package io.vertx.config.toml; | ||
|
||
import io.vertx.codegen.annotations.ModuleGen; |
1 change: 1 addition & 0 deletions
1
vertx-config-toml/src/main/resources/META-INF/services/io.vertx.config.spi.ConfigProcessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.vertx.config.toml.TomlProcessor |
32 changes: 32 additions & 0 deletions
32
vertx-config-toml/src/test/java/examples/ConfigTomlExamplesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package examples; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.RunTestOnContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(VertxUnitRunner.class) | ||
public class ConfigTomlExamplesTest { | ||
|
||
@Rule | ||
public RunTestOnContext contextRule = new RunTestOnContext(); | ||
|
||
private Vertx vertx; | ||
|
||
@Before | ||
public void setUp(TestContext context) throws Exception { | ||
vertx = contextRule.vertx(); | ||
} | ||
|
||
@Test | ||
public void example1_doesnt_throw(TestContext context) { | ||
ConfigTomlExamples examples = new ConfigTomlExamples(); | ||
examples.example1(vertx); | ||
} | ||
} |
Oops, something went wrong.