-
Notifications
You must be signed in to change notification settings - Fork 51
Enable and disable production features
An interface for representing a feature that can be enabled (switched on) or disabled.
FeatureToggle is an instance of the following cyclops types (and others) ApplicativeFunctor, Filterable, Foldable, Functor, MonadicValue1, To, Value,Visitable and Zippable
Concrete type that conveys that a feature may be disabled or may be enabled (switchable).
####Features
- Enable / Disable classes (Pattern Match by type)
- convert to Optional or Stream
- standard Java 8 operators (map, flatMap, peek, filter, forEach) + flatten etc
- isEnabled / isDisabled
- Biased towards enabled (right biased).
The most basic way to use it is (if you are used to programming imperatively)
if(featureDisabled)
return FeatureToggle.disable(data);
else
return FeatureToggle.enable(data);
Now elsewhere you can check if the switch is enabled or disabled
if(toggle.isEnabled()){
loadDataToDb(switch.get());
}
Switch can abstract away entirely the logic for managing whether a feature is enabled or disabled. Users can just code the enabled case and Switch will automatically make sure nothing happens when disabled.
The statement above can be rewritten as -
toggle.map(this::loadDataToTheDb);
In cyclops we can use the visit operator to pattern match on the state the Feature Toggle is in.
String result = FeatureToggle.enabled(processor)
.visit(enabled->enabled.run(),disabled->"default value");
Combine values of any type asynchronously
FeatureToggle.enabled(5)
.combine(Maybe.just(10),(a,b)->a+b);
//FeatureToggle.enabled[15]