diff --git a/README.md b/README.md index a376f92..1884bb0 100644 --- a/README.md +++ b/README.md @@ -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.** diff --git a/modules/core/build.gradle b/modules/core/build.gradle index cdacff4..4b506b8 100644 --- a/modules/core/build.gradle +++ b/modules/core/build.gradle @@ -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' diff --git a/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParser.java b/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParser.java new file mode 100644 index 0000000..3d4ea15 --- /dev/null +++ b/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParser.java @@ -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 parse(String content) { + val result = new HashMap(); + 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; + } +} diff --git a/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParserFactory.java b/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParserFactory.java new file mode 100644 index 0000000..f061e9a --- /dev/null +++ b/modules/core/src/main/java/net/ashald/envfile/providers/toml/TomlEnvFileParserFactory.java @@ -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 baseEnvVars, Consumer 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; + } +} diff --git a/modules/core/src/test/java/net/ashald/envfile/providers/dotenv/DotEnvFileParserTest.java b/modules/core/src/test/java/net/ashald/envfile/providers/dotenv/DotEnvFileParserTest.java index 323a693..c517e3f 100644 --- a/modules/core/src/test/java/net/ashald/envfile/providers/dotenv/DotEnvFileParserTest.java +++ b/modules/core/src/test/java/net/ashald/envfile/providers/dotenv/DotEnvFileParserTest.java @@ -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") ); diff --git a/modules/core/src/test/java/net/ashald/envfile/providers/toml/TomlEnvFileParserTest.java b/modules/core/src/test/java/net/ashald/envfile/providers/toml/TomlEnvFileParserTest.java new file mode 100644 index 0000000..5879265 --- /dev/null +++ b/modules/core/src/test/java/net/ashald/envfile/providers/toml/TomlEnvFileParserTest.java @@ -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 + ); + } + +} diff --git a/modules/core/src/test/resources/providers/toml/mise.toml b/modules/core/src/test/resources/providers/toml/mise.toml new file mode 100644 index 0000000..c8b407d --- /dev/null +++ b/modules/core/src/test/resources/providers/toml/mise.toml @@ -0,0 +1,7 @@ +[env] +A = 'A' +B = 'B' +C = 'C' + +[tools] +go = '1.22' diff --git a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationEditor.java b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationEditor.java index b5b247f..d4162a3 100644 --- a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationEditor.java +++ b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationEditor.java @@ -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; diff --git a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationPanel.java b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationPanel.java index b4df4e7..b8122bf 100644 --- a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationPanel.java +++ b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/EnvFileConfigurationPanel.java @@ -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; diff --git a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/table/EnvFilePathColumnInfo.java b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/table/EnvFilePathColumnInfo.java index 149f6dc..8c4ad33 100644 --- a/modules/platform/src/main/java/net/ashald/envfile/platform/ui/table/EnvFilePathColumnInfo.java +++ b/modules/platform/src/main/java/net/ashald/envfile/platform/ui/table/EnvFilePathColumnInfo.java @@ -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; diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index d0f0bf9..6f478d8 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -21,6 +21,7 @@
  • .env
  • YAML dictionary
  • JSON dictionary
  • +
  • TOML env table dictionary

  • @@ -64,6 +65,7 @@ +