Skip to content

Enable and disable production features

johnmcclean-aol edited this page Nov 23, 2016 · 5 revisions

User guide

FeatureToggle

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

Rationale

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).

Getting started

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());
    }

More advanced usage

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);

Pattern matching

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"); 

Applicative Functor

Combine values of any type asynchronously

FeatureToggle.enabled(5)
             .combine(Maybe.just(10),(a,b)->a+b);

//FeatureToggle.enabled[15]
Clone this wiki locally