forked from smallrye/smallrye-config
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes smallrye#274. Added interceptor to activate a Configuration Pro…
…file.
- Loading branch information
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.smallrye.config; | ||
|
||
import javax.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
@Priority(600) | ||
public class ProfileConfigSourceInterceptor implements ConfigSourceInterceptor { | ||
public static final String SMALLRYE_PROFILE = "smallrye.config.profile"; | ||
|
||
private String profile; | ||
|
||
public ProfileConfigSourceInterceptor() { | ||
|
||
} | ||
|
||
public ProfileConfigSourceInterceptor(final String profile) { | ||
this.profile = profile; | ||
} | ||
|
||
@Override | ||
public void initialize(final Config config) { | ||
if (profile == null) { | ||
profile = config.getOptionalValue(SMALLRYE_PROFILE, String.class).orElse(null); | ||
} | ||
} | ||
|
||
@Override | ||
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { | ||
if (profile != null) { | ||
final ConfigValue profileValue = context.proceed("%" + profile + "." + name); | ||
if (profileValue != null) { | ||
return profileValue; | ||
} | ||
} | ||
|
||
return context.proceed(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
implementation/src/test/java/io/smallrye/config/ProfileConfigSourceInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.smallrye.config; | ||
|
||
import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.junit.Test; | ||
|
||
public class ProfileConfigSourceInterceptorTest { | ||
@Test | ||
public void profile() { | ||
final Config config = buildConfig("my.prop", "1", "%prof.my.prop", "2", SMALLRYE_PROFILE, "prof"); | ||
|
||
assertEquals("2", config.getValue("my.prop", String.class)); | ||
} | ||
|
||
@Test | ||
public void fallback() { | ||
final Config config = buildConfig("my.prop", "1", SMALLRYE_PROFILE, "prof"); | ||
|
||
assertEquals("1", config.getValue("my.prop", String.class)); | ||
} | ||
|
||
@Test | ||
public void expressions() { | ||
final Config config = buildConfig("my.prop", "1", "%prof.my.prop", "${my.prop}", SMALLRYE_PROFILE, "prof"); | ||
|
||
assertEquals("1", config.getValue("my.prop", String.class)); | ||
} | ||
|
||
@Test | ||
public void profileExpressions() { | ||
final Config config = buildConfig("my.prop", "1", | ||
"%prof.my.prop", "${%prof.my.prop.profile}", | ||
"%prof.my.prop.profile", "2", | ||
SMALLRYE_PROFILE, "prof"); | ||
|
||
assertEquals("2", config.getValue("my.prop", String.class)); | ||
} | ||
|
||
private static Config buildConfig(String... keyValues) { | ||
return new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.withSources(KeyValuesConfigSource.config(keyValues)) | ||
.withInterceptors(new ProfileConfigSourceInterceptor(), new ExpressionConfigSourceInterceptor()) | ||
.build(); | ||
} | ||
} |