-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9299 from poikilotherm/9297-feature-flags
9297 feature flags
- Loading branch information
Showing
6 changed files
with
145 additions
and
1 deletion.
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
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
78 changes: 78 additions & 0 deletions
78
src/main/java/edu/harvard/iq/dataverse/settings/FeatureFlags.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,78 @@ | ||
package edu.harvard.iq.dataverse.settings; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* <p> | ||
* This enum holds so-called "feature flags" aka "feature gates", etc. It can be used throughout the application | ||
* to avoid activating or using experimental functionality or feature previews that are opt-in. | ||
* </p><p> | ||
* The current implementation reuses {@link JvmSettings} to interpret any | ||
* <a href="https://download.eclipse.org/microprofile/microprofile-config-3.0/microprofile-config-spec-3.0.html#_built_in_converters">boolean values</a> | ||
* (true == case-insensitive one of "true", "1", "YES", "Y", "ON") and hook into the usual settings system | ||
* (any MicroProfile Config Source available). | ||
* </p><p> | ||
* If you add any new flags, please add it here, think of a default status, add some Javadocs about the flagged | ||
* feature and add a "@since" tag to make it easier to identify when a flag has been introduced. | ||
* </p><p> | ||
* When removing a flag because of a feature being removed, drop the entry. When a feature matures, drop the flag, | ||
* too! Flags are not meant to be switches for configuration! | ||
* </p> | ||
* | ||
* @see <a href="https://guides.dataverse.org/en/latest/installation/config.html#feature-flags">Configuration Guide</a> | ||
* @see <a href="https://guides.dataverse.org/en/latest/developers/configuration.html#adding-a-feature-flag">Developer Guide</a> | ||
*/ | ||
public enum FeatureFlags { | ||
|
||
/* None yet - please add the first here. Example code with JavaDoc example: | ||
* | ||
* /** | ||
* * Your (short) description of what will happen when enabling here. | ||
* * @apiNote Raise flag by setting "dataverse.feature.flag-name" | ||
* * @since Dataverse 5.13 | ||
* * / | ||
* DESCRIPTIVE_NAME_HERE("flag-name"), | ||
* | ||
*/ | ||
|
||
// This is used for testing only, thus no documentation! Might be removed when adding the first real flag | ||
// and replace it in the unit test! | ||
TEST_MODE("test_mode"), | ||
|
||
; | ||
|
||
final String flag; | ||
final boolean defaultStatus; | ||
|
||
/** | ||
* Construct a flag with default status "off". | ||
* | ||
* @param flag This flag name will be used to create a scoped String with {@link JvmSettings#FEATURE_FLAG}, | ||
* making it available as "dataverse.feature.${flag}". | ||
*/ | ||
FeatureFlags(String flag) { | ||
this(flag, false); | ||
} | ||
|
||
/** | ||
* Construct a flag. | ||
* @param flag This flag name will be used to create a scoped String with {@link JvmSettings#FEATURE_FLAG}, | ||
* making it available as "dataverse.feature.${flag}". | ||
* @param defaultStatus A sensible default should be given here. Probably this will be "false" for most | ||
* experimental feature previews. | ||
*/ | ||
FeatureFlags(String flag, boolean defaultStatus) { | ||
Objects.requireNonNull(flag); | ||
this.flag = flag; | ||
this.defaultStatus = defaultStatus; | ||
} | ||
|
||
/** | ||
* Determine the status of this flag via {@link JvmSettings}. | ||
* @return True or false, depending on the configuration or {@link #defaultStatus} if not found. | ||
*/ | ||
public boolean enabled() { | ||
return JvmSettings.FEATURE_FLAG.lookupOptional(Boolean.class, flag).orElse(defaultStatus); | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
src/test/java/edu/harvard/iq/dataverse/settings/FeatureFlagsTest.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,21 @@ | ||
package edu.harvard.iq.dataverse.settings; | ||
|
||
import edu.harvard.iq.dataverse.util.testing.JvmSetting; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class FeatureFlagsTest { | ||
|
||
@Test | ||
void testFlagDisabled() { | ||
assertFalse(FeatureFlags.TEST_MODE.enabled()); | ||
} | ||
|
||
@Test | ||
@JvmSetting(key = JvmSettings.FEATURE_FLAG, value = "on", varArgs = "test_mode") | ||
void testFlagEnabled() { | ||
assertTrue(FeatureFlags.TEST_MODE.enabled()); | ||
} | ||
|
||
} |