Create flags in code

9 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 flag, a number flag, and a string flag are defined.

Android
C client-side
C server-side
C++ client-side
C++ server-side
Go
Java
JavaScript
JavaScript SSR
.NET/C#
Node.js
Objective-C
PHP
Python
React Native
Ruby
Swift
import io.rollout.configuration.RoxContainer; import io.rollout.flags.Freeze; import io.rollout.flags.RoxDouble; import io.rollout.flags.RoxInt; import io.rollout.flags.RoxString; public class Container implements RoxContainer { public final RoxFlag videoChat = new RoxFlag(); public final RoxInt titleSize = new RoxInt(12, new int[]{ 14, 18, 24 }, Freeze.UntilLaunch); public final RoxDouble specialNumber = new RoxDouble(3.14, new double[]{ 2.71, 0.577 }, Freeze.UntilLaunch); public final RoxString titleColors = new RoxString("White", new String[]{ "White", "Blue", "Green", "Yellow" }, Freeze.UntilLaunch); }
#import <rox/client.h> RoxStringBase *videoChat = rox_add_flag_with_freeze("demo.video_chat", false, RoxFreezeUntilLaunch); RoxStringBase* titleSize = rox_add_int_with_freeze_and_options("titleSize", 12, ROX_INT_LIST(14, 18, 24), RoxFreezeUntilLaunch); RoxStringBase* specialNumber = rox_add_double_with_freeze_and_options("specialNumber", 3.14, ROX_DBL_LIST(2.71, 0.577), RoxFreezeUntilLaunch); RoxStringBase* titleColors = rox_add_string_with_freeze_and_options("demo.titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow"), RoxFreezeUntilLaunch);
#import <rox/server.h> RoxStringBase *videoChat = rox_add_flag("demo.video_chat", false); RoxStringBase* titleSize = rox_add_int_with_options("titleSize", 12, ROX_INT_LIST(14, 18, 24)); RoxStringBase* specialNumber = rox_add_double_with_options("specialNumber", 3.14, ROX_DBL_LIST(2.71, 0.577)); RoxStringBase* titleColors = rox_add_string_with_options("demo.titleColors", "White", ROX_LIST_COPY_STR( "White", "Blue", "Green", "Yellow" ));
#import <roxx/client.h> using namespace Rox::Client; auto videoChat = Rox::Client::Flag::Create("demo.video_chat", false, RoxFreezeUntilLaunch); auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}, RoxFreezeUntilLaunch); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577}, RoxFreezeUntilLaunch); auto titleColors = String::Create("demo.titleColors", "White", {"White", "Blue", "Green", "Yellow"}, RoxFreezeNone);
#import <roxx/server.h> using namespace Rox; auto videoChat = Rox::Flag::Create("demo.video_chat", false); auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577}); auto titleColors = String::Create("demo.titleColors", "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 videoChat: server.NewRoxFlag(false), // Define number flags inside the container 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 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 public RoxFlag videoChat = new RoxFlag(); // Define number flags inside the container 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 public final RoxString titleColor = new RoxString("White", new String[]{ "White", "Blue", "Green", "Yellow" }); }
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']) };
import {Flag} from 'rox-ssr'; 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']) };
using Io.Rollout.Rox.Server.Flags; public class Container : IRoxContainer { // Define a Boolean flag inside the container public RoxFlag videoChat = new RoxFlag(); // Define number flags inside the container public RoxInt titleSize = new RoxInt(12, new List<int>() { 14, 18, 24 }); public RoxDouble specialNumber = new RoxDouble(3.14, new List<int>() { 2.71, 0.577 }); // Define a string flag inside the container public RoxString TitleColorsVariant = new RoxString("White", new String[] {"White", "Blue", "Green", "Yellow"}); }
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']) };
#import <ROXCore/ROXCore.h> @interface Flags : ROXBaseContainer @property (nonatomic) ROXFlag* videoChat; @property (nonatomic) ROXInt* titleSize; @property (nonatomic) ROXDouble* specialNumber; @property (nonatomic) ROXString* titleColors; @end #import "Flags.h" @implementation Flags - (instancetype)init { self = [super init]; if (self) { self.videoChat = [[ROXFlag alloc] init]; self.titleColors = [[ROXString alloc] initWithDefault:@"White" variations:@[@"Blue", @"Green", @"Yellow"]]; self.titleSize = [[ROXInt alloc] initWithDefault:12 variations:@[14, 18, 24]]; self.specialNumber = [[ROXDouble alloc] initWithDefault:3.14 variations:@[2.71, 0.577]]; } return self; } @end
use Rox\Server\Rox; use Rox\Server\Flags\RoxFlag; use Rox\Server\Flags\RoxDouble; use Rox\Server\Flags\RoxInt; use Rox\Server\Flags\RoxString; require __DIR__ . '/vendor/autoload.php'; class Container { public $videoChat; public $titleSize; public $specialNumber; public $titleColors; public function __construct() { $this->videoChat = new RoxFlag(false); $this->titleSize = new RoxInt(12, [14, 18, 24]); $this->specialNumber = new RoxDouble(3.14, [2.71, 0.577]); $this->titleColors = new RoxString("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 self.video_chat = RoxFlag() # Define a number flag inside the container self.title_size = RoxNumber(12, [12, 14, 18, 24]) # Define a string flag inside the container self.title_color = RoxString('White', ['White', 'Blue', 'Green', 'Yellow'])
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']) };
require 'rox/server/rox_server' require 'rox/server/flags/rox_int' require 'rox/server/flags/rox_double' require 'rox/server/flags/rox_string' class MyContainer: attr_reader :video_chat, :title_size, :special_number, :title_colors def initialize @video_chat = Rox::Server::RoxFlag.new @title_size = Rox::Server::RoxInt.new(12, [14, 18, 24]) @special_number = Rox::Server::RoxDouble.new(3.14, [2.71, 0.577]) @title_colors = Rox::Server::RoxString.new('White', ['White', 'Blue', 'Green', 'Yellow']) end end
import Foundation import ROX import ROXCore public class Flags: RoxContainer{ // Define a Boolean flag inside the container public let videoChat = RoxFlag(); // Define number flags inside the container public let titleSize = RoxInt(withDefault: 12, variations: [14, 18, 24]) public let specialNumber = RoxDouble(withDefault: 3.14, variations: [2.71, 0.577]) // Define a string flag inside the container public let titleColors = ROXVariant(withDefault: "White", options: ["White", "Blue", "Green", "Yellow"]) // for Enum-based string flag public let titleColorsEnum = RoxEnumVariant(Colors.White) } enum Colors : Int { case White case Blue case Green case Yellow }

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.

Android
C client-side
C server-side
C++ client-side
C++ server-side
Go
Java
JavaScript
JavaScript SSR
.NET/C#
Node.js
Objective-C
PHP
Python
React Native
Ruby
Swift
public class App extends Application { @Override public void onCreate() { super.onCreate(); Container flags = new Container(); Rox.register("<namespace>", flags); Rox.setup(<YOUR-SDK-KEY>); // This can also be set up in MainActivity if your app doesn't extend the Application class } }
#include <rox/client.h> #define DEFAULT_API_KEY "<YOUR-SDK-KEY>" int main(int argc, char **argv) { RoxStringBase *videoChat = rox_add_flag_with_freeze("demo.video_chat", false, RoxFreezeUntilLaunch); RoxStringBase* titleSize = rox_add_int_with_freeze_and_options("titleSize", 12, ROX_INT_LIST(14, 18, 24), RoxFreezeUntilLaunch); RoxStringBase* specialNumber = rox_add_double_with_freeze_and_options("specialNumber", 3.14, ROX_DBL_LIST(2.71, 0.577), RoxFreezeUntilLaunch); RoxStringBase* titleColors = rox_add_string_with_freeze_and_options("demo.titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow"), RoxFreezeUntilLaunch); rox_setup(DEFAULT_API_KEY, NULL); rox_shutdown(); }
#include <rox/server.h> #define DEFAULT_API_KEY "<YOUR-SDK-KEY>" int main(int argc, char **argv) { RoxStringBase *videoChat = rox_add_flag("demo.video_chat", false); RoxStringBase* titleSize = rox_add_int_with_options("titleSize", 12, ROX_INT_LIST(14, 18, 24)); RoxStringBase* specialNumber = rox_add_double_with_options("specialNumber", 3.14, ROX_DBL_LIST(2.71, 0.577)); RoxStringBase* titleColors = rox_add_string_with_options("demo.titleColors", "White", ROX_LIST_COPY_STR( "White", "Blue", "Green", "Yellow" )); rox_setup(DEFAULT_API_KEY, NULL); rox_shutdown(); }
#include <roxx/client.h> #define DEFAULT_API_KEY "<YOUR-SDK-KEY>" using namespace Rox::Client; int main(int argc, char **argv) { auto videoChat = Rox::Client::Flag::Create("demo.video_chat", false, RoxFreezeUntilLaunch); auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}, RoxFreezeUntilLaunch); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577}, RoxFreezeUntilLaunch); auto titleColors = String::Create("demo.titleColors", "White", {"White", "Blue", "Green", "Yellow"}, RoxFreezeNone); Rox::Setup(DEFAULT_API_KEY, NULL); Rox::Shutdown(); }
#include <roxx/server.h> #define DEFAULT_API_KEY "<YOUR-SDK-KEY>" using namespace Rox; int main(int argc, char **argv) { auto videoChat = Rox::Flag::Create("demo.video_chat", false); auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577}); auto titleColors = String::Create("demo.titleColors", "White", {"White", "Blue", "Green", "Yellow"}); Rox::Setup(DEFAULT_API_KEY, NULL); Rox::Shutdown(); }
rox := server.NewRox() rox.Register(myFlags) <-rox.Setup("<YOUR-SDK-KEY>")
Flags myFlags = new Flags(); Rox.register("<namespace>", myFlags); Rox.setup(<YOUR-SDK-KEY>);
Rox.register('<namespace>', flags); Rox.setup('<YOUR-SDK-KEY>');
Rox.register('<namespace>', flags); Rox.setup('<YOUR-SDK-KEY>');
Rox.Register("<namespace>", flags); await Rox.Setup(<YOUR-SDK-KEY>);
Rox.register('<namespace>', flags); await Rox.setup('<YOUR-SDK-KEY>');
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [ROX register:[[Flags alloc] init]]; [ROX setupWithKey:@<YOUR-SDK-KEY>]; return YES; }
use Rox\Server\Rox; use Rox\Server\Flags\RoxFlag; use Rox\Server\Flags\RoxString; class Container { public $videoChat; public $titleColors; public function __construct() { $this->videoChat = new RoxFlag(); $this->titleColors = new RoxString("White", ["White", "Blue", "Green", "Yellow"]); } } $container = new Container(); Rox::register(<namespace>, $container); 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>)
Rox.register('<namespace>', flags); await Rox.setup('<YOUR-SDK-KEY>');
require 'rox/server/rox_server' class Container end flags = Container.new Rox::Server::RoxServer.register(<namespace>, flags) Rox::Server::RoxServer.setup(<YOUR-SDK-KEY>).join
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { ROX.register("<namespace>", container: Flags()) ROX.setup(withKey: <YOUR-SDK-KEY>) return true }

Check the flag values

The following examples check the feature flag values.

Android
C
C++
Go
Java
JavaScript
JavaScript SSR
.NET/C#
Node.js
Objective-C
PHP
Python
React Native
Ruby
Swift
if (flags.videoChat.isEnabled()) { Log.i(tag, "VideoChat Feature is Enabled"); } System.out.println("Title size is " + flags.titleSize.getValue()); System.out.println("The special number is " + flags.specialNumber.getValue()); if (roxContainer.titleColors.getValue() == "Blue") { Log.i(tag, "Title color is blue"); } else if (roxContainer.titleColors.getValue() == "Green") { Log.i(tag, "Title color is green"); } else if (roxContainer.titleColors.getValue() == "Yellow") { Log.i(tag, "Title color is yellow"); } else if (roxContainer.titleColors.getValue() == "White") { Log.i(tag, "Title color is white"); } switch (roxContainer.titleColorsEnum.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 (rox_is_enabled(videoChat)) { // ... } // Check the number flag value printf("Title size is %d\n", rox_get_int(titleSize)); printf("The special number is %f\n", rox_get_double(specialNumber)); // Check the string flag value printf("color is: %s", rox_get_string(titleColor));
// Check the Boolean flag value if (videoChat->IsEnabled()) { // ... } // Check the number flag value std::cout << "Title size is " << titleSize.GetValue() << std::endl; std::cout << "The special number is " << specialNumber.GetValue() << std::endl); // Check the string flag value printf("color is: %s", titleColor->GetValue());
// 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; }
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'); }
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'); }
if (flags.videoChat.isEnabled() == true) { // videoChat is enabled } Console.WriteLine("Title size is {0}", flags.titleSize.GetValue()); Console.WriteLine("The special number is {0}, flags.specialNumber.GetValue()); if (flags.TitleColorsVariant.GetValue().Equals("Black")) { // set title color to black } else if (flags.TitleColorsVariant.GetValue().Equals("Green")) { // set title color to green }
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'); }
[myContainer.videoChat enabled:^{ NSLog(@"VideoChat Feature is Enabled"); } disabled:^{ // ... }]; NSLog(@"Title size is %i", flags.titleSize.value); NSLog(@"The special number is %f", flags.specialNumber.value); if ([roxContainer.titleColors.value isEqualToString:@"Blue"]) { NSLog(@"Title color is Blue"); } else if ([roxContainer.titleColors.value isEqualToString:@"White"]) { NSLog(@"Title color is White"); } else if ([roxContainer.titleColors.value isEqualToString:@"Green"]) { NSLog(@"Title color is Green"); } else if ([roxContainer.titleColors.value isEqualToString:@"Yellow"]) { NSLog(@"Title color is Yellow"); }
// Check the Boolean flag value if ($container->videoChat->isEnabled()) { echo 'video chat is enabled'; } echo "Title size is " . $container->titleSize->getValue(); echo "The special number is " . $container->specialNumber->getValue(); echo "color is " . $flags->titleColors->getValue();
# 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())
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 container.video_chat.enabled? puts "video_chat is enabled" end # Check the number flag values puts("Title size is #{flags.title_size.value}") puts("The special number is #{flags.special_number.value}") puts "color is #{flags.title_colors.value}"
flags.videoChat.enabled { print("VideoChat Feature is Enabled") } print("Title size is " + flags.titleSize.value) print("The special number is " + flags.specialNumber.value) switch roxContainer.titleColors!.value()! { case "White": print("Title color is White") case "Blue": print("Title color is Blue") case "Green": print("Title color is Green") case "Yellow": print("Title color is Yellow") default: print("Title color is default - White") } switch roxContainerEnum.titleColors.value { case .White: print("Title color is White") case .Blue: print("Title color is Blue") case .Green: print("Title color is Green") case .Yellow: print("Title color is Yellow") }

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. The default is set to none.