From 8108b4239792c2964def42a43aa173fab8541355 Mon Sep 17 00:00:00 2001 From: Max Filenko Date: Fri, 18 Mar 2022 20:50:14 +0100 Subject: [PATCH] Add option to treat empty strings as `null` values --- README.md | 13 +++++++++++++ .../io/github/cdimascio/dotenv/DotenvBuilder.java | 11 +++++++++++ .../cdimascio/dotenv/internal/DotenvParser.java | 8 +++++++- src/test/java/tests/DotenvTests.java | 11 +++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0d1f80..ee58d5b 100644 --- a/README.md +++ b/README.md @@ -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(...)` diff --git a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java index 6c44776..c27c4e3 100644 --- a/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java +++ b/src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java @@ -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 env = reader.parse(); diff --git a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java index 6288cbd..2efe5f3 100644 --- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java @@ -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 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; diff --git a/src/test/java/tests/DotenvTests.java b/src/test/java/tests/DotenvTests.java index 28988bd..8bfc2be 100644 --- a/src/test/java/tests/DotenvTests.java +++ b/src/test/java/tests/DotenvTests.java @@ -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")); + } }