Skip to content
Ramesh Malla edited this page Jan 22, 2024 · 1 revision

Frequently, there is a need to automatically enable or disable a feature based on specific conditions, and this is precisely what Biagan Conditions support. As outlined in the configuration-schema, you can specify the conditions in the conditionType that must be satisfied, along with their respective values.

Baigan Context

Baigan context is used to propagate necessary values to successfully evaluate a baigan condition(s).

Example

To understand the concept of context and conditions, let's consider a scenario where you want to enable a feature for a specific country with the country code 3 (no bounds for imagination). Based on this scenario, our Baigan configuration would look like below:

[
  {
    "alias": "express.feature.enabled",
    "description": "Feature toggle",
    "defaultValue": false,
    "conditions": [
      {
        "value": true,
        "conditionType": {
          "onValue": "3",
          "type": "Equals"
        },
        "paramName": "country_code"
      }
    ]
  }
]

It basically states that express.feature.enabled should be true for requests containing country_code as 3, and for the remaining requests, it is set to false (defaultValue).

Now that we have defined the JSON configuration, our next step is to define the Baigan configuration interface, which would look like the following in Java:

@BaiganConfig
public interface ExpressFeature {
    Boolean enabled(ContextProvider contextProvider);
}

We have already discussed Baigan-config interfaces in the getting started guide, the only new addition is the method parameter ContextProvider. ContextProvider is an interface that should be used to define your own context values as required by your conditions. In this example, the context value is country_code = 3.

Taking this information, we will create our own context provider.

public class CountryCodeContextProvider implements ContextProvider {

    private final Set<String> PARAMS = Set.of("country_code");

    private final String countryCode;

    public CustomContextProvider(String countryCode) {
        this.countryCode = countryCode;
    }

    @Override
    public String getContextParam(@NotNull final String name) {
        return countryCode;
    }

    @Override
    public Set<String> getProvidedContexts() {
        return PARAMS;
    }
}

Now the last step is to use the context and conditions while accessing the feature.

@Component
public class ExpressServiceImpl implements ExpressService {

    @Inject
    private ExpressFeature expressFeature;

    @Override
    public void sendShipment(final Shipment shipment) {
        //Initialize context provider with country_code information
        CountryCodeContextProvider contextProvider = new CountryCodeContextProvider(shipment.getCountryCode());
        
        //send the contextProvider as input to evaluate and access the feature flow.
        if (expressFeature.enabled(contextProvider)) {
            // Enable express flow for country_code 3
        }
    }
}

Inbuilt Baigan conditions

By default, baigan supports the following conditions which can be used in the configurations

  • Equals
 "conditions": [
      {
        "value": true,
        "conditionType": {
          "onValue": "3",
          "type": "Equals"
        },
        "paramName": "country_code"
      }
    ]
  • In
 "conditions": [
      {
        "value": true,
        "conditionType": {
          "inValue": [3,4,5],
          "type": "In"
        },
        "paramName": "country_code"
      }
    ]
  • EndsWith
 "conditions": [
      {
        "value": true,
        "conditionType": {
          "endsWithValue": ["lando"],
          "type": "EndsWith"
        },
        "paramName": "name"
      }
    ]