Skip to content

Commit ac7a6bd

Browse files
Maskim BuryshynetsMaskim Buryshynets
authored andcommitted
added Iterable implementation - currently iterates through .env file content only'
1 parent 1ecf71a commit ac7a6bd

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Here you can find the instructions for installing the library using various buil
4343
2. Add library dependency to you dependencies list
4444
```groovy
4545
dependencies {
46-
implementation 'com.github.arhor:java-dotenv-revised:0.1.4'
46+
implementation 'com.github.arhor:java-dotenv-revised:0.1.5'
4747
}
4848
```
4949
@@ -65,7 +65,7 @@ Here you can find the instructions for installing the library using various buil
6565
<dependency>
6666
<groupId>com.github.arhor</groupId>
6767
<artifactId>java-dotenv-revised</artifactId>
68-
<version>0.1.4</version>
68+
<version>0.1.5</version>
6969
</dependency>
7070
```
7171

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = 'com.github.arhor'
9-
version = '0.1.4'
9+
version = '0.1.5'
1010

1111
sourceCompatibility = property('version.java')
1212
targetCompatibility = property('version.java')

src/main/java/io/github/arhor/dotenv/Dotenv.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
import javax.annotation.Nonnull;
55
import javax.annotation.Nullable;
6+
import java.util.Iterator;
7+
import java.util.Map;
68

79
/**
810
* Interface representing loaded .env file.
911
*/
10-
public interface Dotenv {
12+
public interface Dotenv extends Iterable<Map.Entry<String, String>> {
1113

1214
/**
1315
* Factory method returning {@link DotenvConfigurer} instance.
@@ -46,4 +48,8 @@ static DotenvConfigurer configure() {
4648
*/
4749
@Nonnull
4850
String getRequired(@Nonnull String name) throws MissingPropertyException;
51+
52+
@Nonnull
53+
@Override
54+
Iterator<Map.Entry<String, String>> iterator();
4955
}

src/main/java/io/github/arhor/dotenv/DotenvImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.github.arhor.dotenv;
22

3+
import org.jetbrains.annotations.NotNull;
4+
35
import javax.annotation.Nonnull;
46
import javax.annotation.Nullable;
57
import java.util.HashMap;
8+
import java.util.Iterator;
69
import java.util.LinkedHashSet;
710
import java.util.Map;
811
import java.util.Objects;
@@ -50,6 +53,12 @@ public String getRequired(@Nonnull final String name) throws MissingPropertyExce
5053
throw new MissingPropertyException(name);
5154
}
5255

56+
@NotNull
57+
@Override
58+
public Iterator<Map.Entry<String, String>> iterator() {
59+
return properties.entrySet().stream().map(this::stringifyEntry).iterator();
60+
}
61+
5362
private String getPropertyThenClearSearchHistory(
5463
final String name,
5564
final String defaultValue,
@@ -82,7 +91,7 @@ private String getProperty(final String name) {
8291

8392
private String findProperty(final String name) {
8493
final var property = configurer.isIncludeSystemVariables()
85-
? System.getenv().get(name)
94+
? System.getenv(name)
8695
: null;
8796
return ((property == null) || configurer.isReplaceSystemVariables())
8897
? properties.getProperty(name)
@@ -132,4 +141,11 @@ private String findRefValue(final String refName) {
132141
private String currentSearchPath() {
133142
return String.join(" -> ", currentSearchHistory);
134143
}
144+
145+
private Map.Entry<String, String> stringifyEntry(final Map.Entry<Object, Object> it) {
146+
final var key = it.getKey().toString();
147+
final var value = it.getValue().toString();
148+
149+
return Map.entry(key, value);
150+
}
135151
}

src/test/kotlin/io/github/arhor/dotenv/DotenvImplTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.arhor.dotenv
22

33
import io.kotest.assertions.throwables.shouldThrowAny
44
import io.kotest.core.spec.style.DescribeSpec
5+
import io.kotest.matchers.collections.shouldContainExactly
56
import io.kotest.matchers.nulls.shouldBeNull
67
import io.kotest.matchers.nulls.shouldNotBeNull
78
import io.kotest.matchers.shouldBe
@@ -158,4 +159,18 @@ class DotenvImplTest : DescribeSpec({
158159
.shouldHaveMessage("Cannot find property: 'missing_property'")
159160
}
160161
}
162+
163+
describe("Dotenv.iterator()") {
164+
it("should return an iterator with only one expected entry") {
165+
// given
166+
val dotenv = Dotenv.configure().location("./directory").load()
167+
val result = ArrayList<Map.Entry<String, String>>()
168+
169+
// when
170+
dotenv.iterator().forEach(result::add)
171+
172+
// then
173+
result.shouldContainExactly(java.util.Map.entry("test_key", "test_val"))
174+
}
175+
}
161176
})

0 commit comments

Comments
 (0)