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

feat: Add support for fallback-action #93

Merged
merged 11 commits into from
Dec 6, 2019
41 changes: 33 additions & 8 deletions src/main/java/no/finn/unleash/DefaultUnleash.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package no.finn.unleash;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import no.finn.unleash.event.EventDispatcher;
import no.finn.unleash.event.ToggleEvaluated;
import no.finn.unleash.metric.UnleashMetricService;
Expand All @@ -17,6 +11,8 @@
import no.finn.unleash.strategy.*;
import no.finn.unleash.util.UnleashConfig;

import java.util.*;

import static java.util.Optional.ofNullable;
import static no.finn.unleash.Variant.DISABLED_VARIANT;
import static no.finn.unleash.variant.VariantUtil.selectVariant;
Expand Down Expand Up @@ -63,6 +59,14 @@ public DefaultUnleash(UnleashConfig unleashConfig, ToggleRepository toggleReposi
metricService.register(strategyMap.keySet());
}

private static FeatureToggleRepository defaultToggleRepository(UnleashConfig unleashConfig) {
return new FeatureToggleRepository(
unleashConfig,
new HttpToggleFetcher(unleashConfig),
new ToggleBackupHandlerFile(unleashConfig)
);
}

@Override
public boolean isEnabled(final String toggleName) {
return isEnabled(toggleName, false);
Expand All @@ -73,12 +77,33 @@ public boolean isEnabled(final String toggleName, final boolean defaultSetting)
return isEnabled(toggleName, contextProvider.getContext(), defaultSetting);
}

public boolean isEnabled(final String toggleName, final FallbackAction fallbackAction) {
FeatureToggle featureToggle = toggleRepository.getToggle(toggleName);
boolean enabled = isEnabled(featureToggle, contextProvider.getContext(), false);

if(!enabled && fallbackAction != null) {
fallbackAction.apply(toggleName, contextProvider.getContext());
}

return enabled;
}

public boolean isEnabled(final String toggleName, boolean defaultSetting, final FallbackAction fallbackAction) {
FeatureToggle featureToggle = toggleRepository.getToggle(toggleName);

if(featureToggle == null && fallbackAction != null) {
fallbackAction.apply(toggleName, contextProvider.getContext());
}

return isEnabled(featureToggle, contextProvider.getContext(), defaultSetting);
Copy link
Member

@ivarconr ivarconr Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather invert it and have this function being the master and have the other isEnabled calls send in a proper fallback-action.

}

@Override
public boolean isEnabled(final String toggleName, final UnleashContext context ,final boolean defaultSetting) {
public boolean isEnabled(final String toggleName, final UnleashContext context, final boolean defaultSetting) {
FeatureToggle featureToggle = toggleRepository.getToggle(toggleName);
boolean enabled = isEnabled(featureToggle, context, defaultSetting);
count(toggleName, enabled);
eventDispatcher.dispatch(new ToggleEvaluated(toggleName,enabled));
eventDispatcher.dispatch(new ToggleEvaluated(toggleName, enabled));
return enabled;
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/no/finn/unleash/FakeUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ public boolean isEnabled(String toggleName, boolean defaultSetting) {
}
}

@Override
public boolean isEnabled(String toggleName, boolean defaultSetting, FallbackAction fallbackAction) {
if(enableAll) {
return true;
} else if(disableAll) {

return false;
} else {
if(!features.containsKey(toggleName)) {
fallbackAction.apply(toggleName, UnleashContext.builder().build());
}
return features.getOrDefault(toggleName, defaultSetting);
}
}

@Override
public boolean isEnabled(String toggleName, FallbackAction fallbackAction) {
if(enableAll) {
return true;
} else if(disableAll) {
return false;
} else {
if(!features.containsKey(toggleName)) {
fallbackAction.apply(toggleName, UnleashContext.builder().build());
}
return features.getOrDefault(toggleName, false);
}
}
dennis-menge marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Variant getVariant(String toggleName, UnleashContext context) {
return getVariant(toggleName, Variant.DISABLED_VARIANT);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/no/finn/unleash/FallbackAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.finn.unleash;

public interface FallbackAction {
void apply(String toggleName, UnleashContext unleashContext);
}
4 changes: 4 additions & 0 deletions src/main/java/no/finn/unleash/Unleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ default boolean isEnabled(String toggleName, UnleashContext context, boolean def
return isEnabled(toggleName, defaultSetting);
}

boolean isEnabled(final String toggleName, boolean defaultSetting, final FallbackAction fallbackAction);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining a new interface we could actually use one of the functional interfaces: BiFunction<String, UnleashContext, Boolean> instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, completely forgot about these, I will change that accordingly.


boolean isEnabled(final String toggleName, final FallbackAction fallbackAction);

Variant getVariant(final String toggleName, final UnleashContext context);

Variant getVariant(final String toggleName, final UnleashContext context, final Variant defaultValue);
Expand Down