Skip to content

Commit

Permalink
Change property name from prefixProperty to prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschwarz committed Mar 24, 2022
1 parent 863bf89 commit 3950192
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ This library supports the same configuration values as the underlying [dotenv-ja
ignoreIfMalformed: <boolean>
ignoreIfMissing: <boolean>
systemProperties: <boolean>
prefix: <string>
```
By default, this library sets `ignoreIfMissing` to `true`. You may change this behaviour as follows:
Expand All @@ -107,6 +108,16 @@ By default, this library sets `ignoreIfMissing` to `true`. You may change this b
ignoreIfMissing: false
```

This library expects properties to be prefixed with `env.` as follows. However, you may configure a different prefix.

```yaml
.env:
prefix: ""
example:
name: ${EXAMPLE_NAME:World}
```

If you prefer .properties files:

```properties
Expand All @@ -115,6 +126,7 @@ If you prefer .properties files:
.env.ignoreIfMalformed: <boolean>
.env.ignoreIfMissing: <boolean>
.env.systemProperties: <boolean>
.end.prefix: <string>
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion application/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ignoreIfMalformed: true
# ignoreIfMissing: false
# systemProperties: true
prefixProperty: ""
prefix: ""

example:
name: ${EXAMPLE_NAME:World}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class DotenvConfig {
private Boolean ignoreIfMalformed;
private Boolean ignoreIfMissing = true;
private Boolean systemProperties;
private String prefixProperty;
private String prefix;

public DotenvConfig(ConfigurableEnvironment environment) {
directory = getEnvironmentProperty(environment, "directory", directory);
filename = getEnvironmentProperty(environment, "filename", filename);
ignoreIfMalformed = getEnvironmentProperty(environment, "ignoreIfMalformed", ignoreIfMalformed);
ignoreIfMissing = getEnvironmentProperty(environment, "ignoreIfMissing", ignoreIfMissing);
systemProperties = getEnvironmentProperty(environment, "systemProperties", systemProperties);
prefixProperty = getEnvironmentProperty(environment, "prefixProperty", prefixProperty);
prefix = getEnvironmentProperty(environment, "prefix", prefix);
}

private String getEnvironmentProperty(ConfigurableEnvironment environment, String key, String defaultValue) {
Expand All @@ -46,8 +46,8 @@ public Optional<String> getFilenameOptional() {
return Optional.ofNullable(filename);
}

public Optional<String> getPrefixPropertyOptional() {
return Optional.ofNullable(prefixProperty);
public Optional<String> getPrefixOptional() {
return Optional.ofNullable(prefix);
}

public Optional<Boolean> getIgnoreIfMalformedTruth() {
Expand All @@ -73,7 +73,7 @@ public String toString() {
", ignoreIfMalformed=" + ignoreIfMalformed +
", ignoreIfMissing=" + ignoreIfMissing +
", systemProperties=" + systemProperties +
", prefixProperty=" + prefixProperty +
", prefix=" + prefix +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public DotenvPropertySource(String name, DotenvConfig dotenvConfig) {
public DotenvPropertySource(DotenvConfig dotenvConfig) {
this(DOTENV_PROPERTY_SOURCE_NAME, dotenvConfig);

if (dotenvConfig.getPrefixPropertyOptional().isPresent()) {
this.prefix = dotenvConfig.getPrefixPropertyOptional().get();
}else {
this.prefix = DEFAULT_PREFIX;
}

this.prefix = dotenvConfig.getPrefixOptional().orElse(DEFAULT_PREFIX);
}

public DotenvPropertySource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

Expand All @@ -23,7 +21,7 @@ void defaults() {
assertTrue(dotenvConfig.getIgnoreIfMissingTruth().isPresent());
assertFalse(dotenvConfig.getSystemPropertiesTruth().isPresent());
assertTrue(dotenvConfig.getIgnoreIfMissingTruth().get());
assertFalse(dotenvConfig.getPrefixPropertyOptional().isPresent());
assertFalse(dotenvConfig.getPrefixOptional().isPresent());

}

Expand All @@ -35,7 +33,7 @@ void withValues() {
doReturn("true").when(environment).getProperty(".env.ignoreIfMalformed", "false");
doReturn("false").when(environment).getProperty(".env.ignoreIfMissing", "true");
doReturn("true").when(environment).getProperty(".env.systemProperties", "false");
doReturn("").when(environment).getProperty(".env.prefixProperty", (String) null);
doReturn("").when(environment).getProperty(".env.prefix", (String) null);

DotenvConfig dotenvConfig = new DotenvConfig(environment);

Expand All @@ -44,13 +42,13 @@ void withValues() {
assertTrue(dotenvConfig.getIgnoreIfMalformedTruth().isPresent());
assertFalse(dotenvConfig.getIgnoreIfMissingTruth().isPresent());
assertTrue(dotenvConfig.getSystemPropertiesTruth().isPresent());
assertTrue(dotenvConfig.getPrefixPropertyOptional().isPresent());
assertTrue(dotenvConfig.getPrefixOptional().isPresent());

assertEquals("/some/dir", dotenvConfig.getDirectoryOptional().get());
assertEquals(".env", dotenvConfig.getFilenameOptional().get());
assertTrue(dotenvConfig.getIgnoreIfMalformedTruth().get());
assertTrue(dotenvConfig.getSystemPropertiesTruth().get());

assertEquals("", dotenvConfig.getPrefixPropertyOptional().get());
assertEquals("", dotenvConfig.getPrefixOptional().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.env.ConfigurableEnvironment;

class DotenvPropertySourceEnvironmentTest {

private ConfigurableEnvironment configurableEnvironment;


private DotenvPropertySource source;

@BeforeEach
void init(){
void init() {
configurableEnvironment = mock(ConfigurableEnvironment.class);
doReturn("").when(configurableEnvironment).getProperty(".env.prefixProperty", (String)null);
doReturn("").when(configurableEnvironment).getProperty(".env.prefix", (String) null);
source = new DotenvPropertySource(new DotenvConfig(configurableEnvironment));
}

@Test
void irrelevant() {
assertThat(source.getProperty("other.VALUE")).isNull();
Expand Down

0 comments on commit 3950192

Please sign in to comment.