Create flags in code

3 minute read

Feature flags are conditional statements around a coded functionality in an application. Create a boolean, number, and/or string type flag in your application source code using the static API.

Create a container class and define feature flags

All flags are defined inside the required container class, and each flag name is derived from the flag variable name.

In each of the following code examples, a boolean, number and a string flag are defined.

JavaScript
Go
Java
Python
const flags = { // Define a boolean flag inside the container videoChat: new Rox.Flag(), // Define a number flag inside the container titleSize: new Rox.RoxNumber(12, [12, 14, 18, 24]), // Define a string flag inside the container titleColor: new Rox.RoxString('White', ['White', 'Blue', 'Green', 'Yellow']) };
package main import ( "github.com/rollout/rox-go/v5/server" ) type myContainer struct { videoChat server.RoxFlag titleSize server.RoxInt specialNumber server.RoxDouble titleColor server.RoxString } var myFlags = & myContainer { // Define a boolean flag inside the container class videoChat: server.NewRoxFlag(false), // Define number flags inside the container class titleSize: sever.NewRoxInt(12, [12, 14, 18, 24]), specialNumber: server.NewRoxDouble(99.9, [10.5, 50.0, 99.9]), // Define a string flag inside the container class titleColor: server.NewRoxString("White", [] string {"White", "Blue", "Green", "Yellow"}) }
import io.rollout.configuration.RoxContainer; import io.rollout.flags.RoxDouble; import io.rollout.flags.RoxInt; import io.rollout.flags.RoxString; public static class Flags implements RoxContainer { // Define a boolean flag inside the container class public RoxFlag videoChat = new RoxFlag(); // Define number flags inside the container class public final RoxInt titleSize = new RoxInt(12, new int[]{ 14, 18, 24 }); public final RoxDouble specialNumber = new RoxDouble(3.14, new double[]{ 2.71, 0.577 }); // Define a string flag inside the container class public final RoxString titleColor = new RoxString("White", new String[]{ "White", "Blue", "Green", "Yellow" }); }
from rox.server.flags.rox_flag import RoxFlag # Create a container class class MyContainer: def __init__(self): # Define a boolean flag inside the container class self.video_chat = RoxFlag() # Define a string flag inside the container class self.title_color = RoxString('White', ['White', 'Blue', 'Green', 'Yellow']) # Define a number flag inside the container class self.title_size = RoxNumber(12, [12, 14, 18, 24])

Register the container class of feature flags and set up the SDK key

To use the container class, its instance must be registered to the platform SDK. The namespace parameter is a logical separation between containers, and is a criterion in filtering flags.

A namespace can only be registered once.

In the following examples, a Rox SDK function is called to register the class instance, and then the environment key is set up.

JavaScript
Go
Java
Python
Rox.register('<namespace>', flags); Rox.setup('<YOUR_SDK_KEY>');
rox := server.NewRox() rox.Register(myFlags) <-rox.Setup("<YOUR_SDK_KEY>")
Flags myFlags = new Flags(); Rox.register("<namespace>", myFlags); Rox.setup(YOUR_SDK_KEY);
# Register the container class instance to the platform SDK from rox.server.rox_server import Rox my_flags = MyContainer() Rox.register(<namespace>, my_flags) Rox.setup(<YOUR_SDK_KEY>)

Check the flag values

The following examples check the feature flag values.

JavaScript
Go
Java
Python
if (flags.videoChat.isEnabled()) { console.log('The video chat feature is enabled'); } console.log('Title size is ' + flags.titleSize.getValue()); if (flags.titleColor.getValue() === 'Blue') { console.log('Title color is blue'); } else if (flags.titleColor.getValue() === 'Green') { console.log('Title color is green'); } else if (flags.titleColor.getValue() === 'Yellow') { console.log('Title color is yellow'); } else if (flags.titleColor.getValue() === 'White') { console.log('Title color is white'); }
// Check the boolean flag value if (myFlags.videoChat.IsEnabled(nil)) { // ... } // Check the number flag value console.log('Title size is ' + myFlags.titleSize.getValue()); // Check the string flag value switch myFlags.titleColor.GetValue(nil) { case "Green" : fmt.Println("green"); case "White" : fmt.Println("white"); case "Blue" : fmt.Println("blue"); }
// Check the boolean flag value if (myFlags.videoChat.isEnabled()) { Logger.info(tag, "The video chat feature is enabled"); } // Check the number flag value System.out.println("Title size is " + myFlags.titleSize.getValue()); System.out.println("The special number is " + myFlags.specialNumber.getValue()); // Check the string flag value if (myFlags.titleColor.getValue() == "Blue") { Log.i(tag, "Title color is blue"); } else if (myFlags.titleColor.getValue() == "Green") { Log.i(tag, "Title color is green"); } else if (myFlags.titleColor.getValue() == "Yellow") { Log.i(tag, "Title color is yellow"); } else if (myFlags.titleColor.getValue() == "White") { Log.i(tag, "Title color is white"); } switch (myFlags.titleColorEnum.getValue()) { case BLUE: Log.i(tag, "Title color is blue"); break; case GREEN: Log.i(tag, "Title color is green"); break; case YELLOW: Log.i(tag, "Title color is yellow"); break; case WHITE: Log.i(tag, "Title color is white"); break; }
# Check the boolean flag value if my_flags.video_chat.is_enabled(): print('video_chat is enabled') # Check the number flag value print('Title size is {}'.format(my_flags.title_size.get_value())) print('The special number is {}'.format(my_flags.special_number.get_value())) # Check the string flag value print('color is %s' % my_flags.title_color.get_value())

The default flag value

The default flag value, which is the flag value when configuration is disabled, is the value specified in the code. If no value for a boolean flag is specified in your code, the default value is false. Number and string flags must have a default value defined for when configuration is disabled.

Flag freeze is available on client-side SDKs by default. For SDK version 5.0 or later, the default is set to none.