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

Add option to treat empty strings as null values #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -172,6 +172,19 @@ for (DotenvEntry e : dotenv.entries()) {
.load();
```

### *optional* `ignoreEmpty`

* Treat empty or blank values as `null`.

**example**

```java
Dotenv
.configure()
.ignoreEmpty()
.load();
```

### *optional* `systemProperties`

* Load environment variables into System properties, thus making all environment variables accessible via `System.getProperty(...)`
11 changes: 11 additions & 0 deletions src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public class DotenvBuilder {
private String filename = ".env";
private String directoryPath = "./";
private boolean systemProperties = false;
private boolean ignoreEmpty = false;
private boolean throwIfMissing = true;
private boolean throwIfMalformed = true;

@@ -37,6 +38,15 @@ public DotenvBuilder filename(String name) {
return this;
}

/**
* Returns `null` for empty or blank values.
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder ignoreEmpty() {
ignoreEmpty = true;
return this;
}

/**
* Does not throw an exception when .env is missing.
* @return this {@link DotenvBuilder}
@@ -72,6 +82,7 @@ public DotenvBuilder systemProperties() {
public Dotenv load() throws DotenvException {
DotenvParser reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
ignoreEmpty,
throwIfMissing,
throwIfMalformed);
List<DotenvEntry> env = reader.parse();
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
*/
public class DotenvParser {
private final DotenvReader reader;
private final boolean ignoreEmtpy;
private final boolean throwIfMissing;
private final boolean throwIfMalformed;

@@ -28,11 +29,13 @@ public class DotenvParser {
/**
* Creates a dotenv parser
* @param reader the dotenv reader
* @param ignoreEmpty if true, returns `null` for empty or blank values
* @param throwIfMissing if true, throws when the .env file is missing
* @param throwIfMalformed if true, throws when the .env file is malformed
*/
public DotenvParser(DotenvReader reader, boolean throwIfMissing, boolean throwIfMalformed) {
public DotenvParser(DotenvReader reader, boolean ignoreEmpty, boolean throwIfMissing, boolean throwIfMalformed) {
this.reader = reader;
this.ignoreEmtpy = ignoreEmpty;
this.throwIfMissing = throwIfMissing;
this.throwIfMalformed = throwIfMalformed;
}
@@ -55,6 +58,9 @@ public List<DotenvEntry> parse() throws DotenvException {
}
String key = entry.getKey();
String value = normalizeValue(entry.getValue());
if (value.isEmpty() && ignoreEmtpy) {
continue;
}
entries.add(new DotenvEntry(key, value));
}
return entries;
11 changes: 11 additions & 0 deletions src/test/java/tests/DotenvTests.java
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class DotenvTests {
@@ -114,4 +115,14 @@ public void configureWithIgnoreMissingAndMalformed() {

assertNotNull(dotenv.get("PATH"));
}

@Test
public void configureWithIgnoreMalformedAndEmpty() {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMalformed()
.ignoreEmpty()
.load();

assertNull(dotenv.get("WITHOUT_VALUE"));
}
}