Skip to content

Commit

Permalink
add api to iterator over env entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 2, 2019
1 parent fad0c61 commit f6439c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/main/kotlin/io/github/cdimascio/dotenv/Dotenv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.Collections
* Dotenv
* @see <a href="https:/cdimascio/java-dotenv">The complete dotenv documentation</a>
*/
abstract class Dotenv : Iterable<DotenvEntry> {
abstract class Dotenv {
/**
* The dotenv instance
*/
Expand All @@ -39,7 +39,7 @@ abstract class Dotenv : Iterable<DotenvEntry> {
*/
abstract operator fun get(envName: String): String?

abstract override operator fun iterator(): Iterator<DotenvEntry>
abstract fun entries(): Set<DotenvEntry>

/**
* Returns the value for the environment variable, or the default value if absent
Expand Down Expand Up @@ -119,11 +119,11 @@ class DotenvBuilder internal constructor() {
data class DotenvEntry(val key: String, val value: String)

private class DotenvImpl(envVars: List<Pair<String, String>>) : Dotenv() {

private val map = envVars.associateBy({ it.first }, { it.second })
private val iter = Collections.unmodifiableList(envVars.map { DotenvEntry(it.first, it.second) }).iterator()

override fun iterator(): Iterator<DotenvEntry> = iter
private val set = Collections.unmodifiableSet(
map.entries.map { DotenvEntry(it.key, it.value) }.toSet()
)

override fun entries() = set
override fun get(envName: String): String? = System.getenv(envName) ?: map[envName]
}
24 changes: 19 additions & 5 deletions src/test/java/tests/JavaTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;

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

import static org.junit.Assert.assertEquals;
Expand All @@ -17,7 +18,7 @@ public class JavaTests {

@Before
public void setUp() {
envVars = new HashMap<String, String>();
envVars = new HashMap<>();
envVars.put("MY_TEST_EV1", "my test ev 1");
envVars.put("MY_TEST_EV2", "my test ev 2");
envVars.put("WITHOUT_VALUE", "");
Expand Down Expand Up @@ -50,21 +51,34 @@ public void iteratorOverDotenv() {

dotenv.entries().forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));

for (DotenvEntry e : dotenv) {
for (DotenvEntry e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}

@Test(expected = UnsupportedOperationException.class)
public void failToRemoveFromIterator() {
public void failToRemoveFromDotenv() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
while (dotenv.iterator().hasNext()) {
dotenv.iterator().remove();

Iterator<DotenvEntry> iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
}

@Test(expected = UnsupportedOperationException.class)
public void failToAddToDotenv() {

Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();

dotenv.entries().add(new DotenvEntry("new", "value"));
}

@Test
public void configureWithIgnoreMalformed() {
Dotenv dotenv = Dotenv.configure()
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/tests/BasicTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class DotEnvTest {
.ignoreIfMalformed()
.load()

for (e in dotenv) {
for (e in dotenv.entries()) {
assertEquals(dotenv[e.key], e.value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/tests/DslTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DotEnvDslTest {
ignoreIfMalformed = true
}

for (e in env) {
for (e in env.entries()) {
assertEquals(env[e.key], e.value)
}
}
Expand Down

0 comments on commit f6439c9

Please sign in to comment.