Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TOML format #232

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from one or multiple files.
- **.env**
- **YAML** dictionary
- **JSON** dictionary *(parsed with YAML parser since [JSON is subset of YAML][json-is-yaml])*
- **TOML** env table dictionary

**All formats assume that both keys and values are strings.**

Expand Down
1 change: 1 addition & 0 deletions modules/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
dependencies {
implementation 'org.jetbrains:annotations:23.0.0'
implementation 'org.yaml:snakeyaml:1.32'
implementation 'com.moandjiezana.toml:toml4j:0.7.2'

implementation 'com.google.guava:guava:31.1-jre'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.ashald.envfile.providers.toml;

import com.moandjiezana.toml.Toml;
import lombok.AllArgsConstructor;
import lombok.val;
import net.ashald.envfile.providers.EnvFileParser;

import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
public class TomlEnvFileParser implements EnvFileParser {
private final Toml toml;

@Override
public Map<String, String> parse(String content) {
val result = new HashMap<String, String>();
val envTable = toml
.read(content)
.getTable("env");

if (envTable == null) {
return result;
}

for (val entry : envTable.entrySet()) {
result.put(entry.getKey(), entry.getValue().toString());
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.ashald.envfile.providers.toml;

import com.moandjiezana.toml.Toml;
import net.ashald.envfile.EnvVarsProvider;
import net.ashald.envfile.EnvVarsProviderFactory;
import net.ashald.envfile.providers.EnvFileExecutor;
import net.ashald.envfile.providers.EnvFileReader;
import net.ashald.envfile.providers.SingleFileEnvVarsProvider;

import java.util.Map;
import java.util.function.Consumer;

public class TomlEnvFileParserFactory implements EnvVarsProviderFactory {
private static final Toml TOML = new Toml();

@Override
public EnvVarsProvider createProvider(Map<String, String> baseEnvVars, Consumer<String> logger) {
return SingleFileEnvVarsProvider.builder()
.reader(EnvFileReader.DEFAULT)
.executor(EnvFileExecutor.DEFAULT)
.parser(
new TomlEnvFileParser(TOML)
)
.logger(logger)
.build();
}

@Override
public String getTitle() {
return "TOML";
}

@Override
public boolean isEditable() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static String getFile(String name) {
}

@Test
public void GIVEN_parse_WEHN_charSequenceLooksLikeMalformedUnicode_THEN_doesNotFail() {
public void GIVEN_parse_WHEN_charSequenceLooksLikeMalformedUnicode_THEN_doesNotFail() {
val result = PARSER.parse(
getFile("malformed-unicode.env")
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.ashald.envfile.providers.toml;

import com.google.common.collect.ImmutableList;
import com.moandjiezana.toml.Toml;
import lombok.SneakyThrows;
import lombok.val;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;

public class TomlEnvFileParserTest {
private final TomlEnvFileParser PARSER = new TomlEnvFileParser(new Toml());

/**
* TODO: generalize
*/
@SneakyThrows
private static String getFile(String name) {
val bytes = Files.readAllBytes(Paths.get("src", "test", "resources", "providers", "toml", name));
return new String(bytes, StandardCharsets.UTF_8);
}

@Test
public void GIVEN_parse_WHEN_parses_THEN_preservesOrder() {
val result = PARSER.parse(
getFile("mise.toml")
);

val keys = ImmutableList.copyOf(
result.keySet()
);

assertEquals(
ImmutableList.of("A", "B", "C"),
keys
);
}

}
7 changes: 7 additions & 0 deletions modules/core/src/test/resources/providers/toml/mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[env]
A = 'A'
B = 'B'
C = 'C'

[tools]
go = '1.22'
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.ashald.envfile.platform.EnvFileSettings;
import net.ashald.envfile.platform.EnvVarsProviderExtension;
import net.ashald.envfile.platform.ProjectFileResolver;
import net.ashald.envfile.providers.toml.TomlEnvFileParser;
import net.ashald.envfile.providers.runconfig.RunConfigEnvVarsProvider;
import org.jdom.Element;
import org.jetbrains.annotations.Contract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.ashald.envfile.platform.ui.table.EnvFileIsExecutableColumnInfo;
import net.ashald.envfile.platform.ui.table.EnvFilePathColumnInfo;
import net.ashald.envfile.platform.ui.table.EnvFileTypeColumnInfo;
import net.ashald.envfile.providers.toml.TomlEnvFileParser;
import net.ashald.envfile.providers.runconfig.RunConfigEnvVarsProvider;

import javax.swing.BoxLayout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.ashald.envfile.platform.EnvFileEntry;
import net.ashald.envfile.platform.EnvVarsProviderExtension;
import net.ashald.envfile.platform.ProjectFileResolver;
import net.ashald.envfile.providers.toml.TomlEnvFileParser;
import net.ashald.envfile.providers.runconfig.RunConfigEnvVarsProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<li><b>.env</b></li>
<li><b>YAML</b> dictionary</li>
<li><b>JSON</b> dictionary</li>
<li><b>TOML</b> env table dictionary</li>
</ul>

<br/>
Expand Down Expand Up @@ -64,6 +65,7 @@
<extensions defaultExtensionNs="net.ashald.envfile">
<envVarsProvider id="env" factory="net.ashald.envfile.providers.dotenv.DotEnvFileParserFactory"/>
<envVarsProvider id="yaml" factory="net.ashald.envfile.providers.yaml.YamlEnvFileParserFactory"/>
<envVarsProvider id="toml" factory="net.ashald.envfile.providers.toml.TomlEnvFileParserFactory"/>
<envVarsProvider id="runconfig" factory="net.ashald.envfile.providers.runconfig.RunConfigEnvVarsProviderFactory"/>
</extensions>

Expand Down