Creating a number flag

5 minute read

Number flags can have multiple numerical values, and you must specify the default value when you create the flag. The default flag value, which is the flag value when configuration is disabled, is the value specified in the code. If configuration is enabled, the number flag value can be set in the CloudBees Feature Management UI. To learn more, refer to Setting the flag default values.

You may want a dynamic number flag. In this case, you can define the flag in the code with just the default number value. In the CloudBees Feature Management UI, a free text input field then allows you to enter any number you require.

Alternatively, you can set multiple flag values at the time you define the flag in the code, and choose among them to configure the flag in the CloudBees Feature Management UI.

The number flag name is derived from the flag variable name.

Creating a number flag in the UI

To create a number flag in the UI, select the Number flag type when you create your flag. Refer to Creating feature flags for more information.

Creating a container class and defining a feature flag

You can create number flags in your code using the static API. A container class is required, and you define your flag inside the container class.

In the following examples, the titleSize flag, which specifies the font size of a title, can be either 12, 14, 18, or 24. The default font size is 12.

React Native
JavaScript
Node.js
JavaScript SSR
Android
Java
.NET
Python
C
C Client Side
C++
C++ Client Side
Go
Ruby
Objective C
Swift
PHP
const flags = { titleSize: new Rox.RoxNumber(12, [12, 14, 18, 24]), };
const flags = { titleSize: new Rox.RoxNumber(12, [12, 14, 18, 24]), };
const flags = { titleSize: new Rox.RoxNumber(12, [12, 14, 18, 24]), };
import {RoxNumber} from 'rox-ssr'; const flags = { titleSize: new Rox.RoxNumber(12, [12, 14, 18, 24]), };
import io.rollout.configuration.RoxContainer; import io.rollout.flags.Freeze; import io.rollout.flags.RoxDouble; import io.rollout.flags.RoxInt; public class Container implements RoxContainer { 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); }
import io.rollout.configuration.RoxContainer; import io.rollout.flags.RoxDouble; import io.rollout.flags.RoxInt; public class Container implements RoxContainer { 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 }); }
using Io.Rollout.Rox.Server.Flags; public class Container : IRoxContainer { 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 }); }
from rox.core.entities.rox_int import RoxInt from rox.core.entities.rox_double import RoxDouble class MyContainer: def __init__(self): self.title_size = RoxInt(12, [14, 18, 24]) self.special_number = RoxDouble(3.14, [2.71, 0.577])
#import <rox/server.h> 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));
#import <rox/client.h> 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);
#import <roxx/server.h> using namespace Rox; auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577});
#import <roxx/client.h> using namespace Rox::Client; auto titleSize = Int::Create("titleSize", 12, {14, 18, 24}, RoxFreezeUntilLaunch); auto specialNumber = Double::Create("specialNumber", 3.14, {2.71, 0.577}, RoxFreezeUntilLaunch);
import "github.com/rollout/rox-go/v5/server" type Flags struct { EnableTutorial server.RoxFlag TitleColors server.RoxString Page server.RoxInt Percentage server.RoxDouble } var flags = & Flags { // Define the feature flags EnableTutorial: server.NewRoxFlag(false), TitleColors: server.NewRoxString("White", [] string {"White", "Blue", "Green", "Yellow"}), TitleSize: sever.NewRoxInt(12, [12, 14, 18, 24]), Percentage: server.NewRoxDouble(99.9, [10.5, 50.0, 99.9]), }
require 'rox/server/rox_server' require 'rox/server/flags/rox_int' require 'rox/server/flags/rox_double' class MyContainer: attr_reader :title_size, :special_number def initialize @title_size = Rox::Server::RoxInt.new(12, [14, 18, 24]) @special_number = Rox::Server::RoxDouble.new(3.14, [2.71, 0.577]) end end
#import <ROXCore/ROXCore.h> @interface Flags : ROXBaseContainer @property (nonatomic) ROXInt* titleSize; @property (nonatomic) ROXDouble* specialNumber; @end @implementation Flags - (instancetype)init { self = [super init]; if (self) { self.titleSize = [[ROXInt alloc] initWithDefault:12 variations:@[14, 18, 24]]; self.specialNumber = [[ROXDouble alloc] initWithDefault:3.14 variations:@[2.71, 0.577]]; } return self; }
public class Flags : RoxContainer { let titleSize = RoxInt(withDefault: 12, variations: [14, 18, 24]) let specialNumber = RoxDouble(withDefault: 3.14, variations: [2.71, 0.577]) }
use Rox\Server\Flags\RoxDouble; use Rox\Server\Flags\RoxInt; class Container { public $titleSize; public $specialNumber; public function __construct() { $this->titleSize = new RoxInt(12, [14, 18, 24]); $this->specialNumber = new RoxDouble(3.14, [2.71, 0.577]); } }

Registering the container class

To use the container class, register its instance to the CloudBees Feature Management SDK with the Rox.register() SDK function.

The namespace parameter is a logical separation between containers and can be used to find flags on the dashboard.

A namespace can only be registered once.

In the following examples, the Rox.register() SDK function is called to register the class instance:

React Native
JavaScript
Node.js
JavaScript SSR
Android
Java
.NET
Python
Go
Ruby
Objective C
Swift
PHP
const flags = { // define Rox entities here } Rox.register('<namespace>', flags); await Rox.setup('<YOUR_APP_KEY>');
const flags = { // define Rox entities here } Rox.register('<namespace>', flags); Rox.setup('<YOUR_APP_KEY>');
const flags = { // define Rox entities here } Rox.register('<namespace>', flags); await Rox.setup('<YOUR_APP_KEY>');
import {Rox} from 'rox-ssr'; const flags = { // define Rox entities here } Rox.register('<namespace>', flags); Rox.setup('<YOUR_APP_KEY>');
public class App extends Application { @Override public void onCreate() { super.onCreate(); Container flags = new Container(); Rox.register("<namespace>", flags); Rox.setup(this); // This can also be set up in MainActivity if your app does not extend the Application class } }
Container flags = new Container(); Rox.register("<namespace>", flags); Rox.setup("<YOUR_APP_KEY>");
Container flags = new Container(); Rox.Register("<namespace>", flags); await Rox.Setup(<YOUR_APP_KEY>);
flags = Container() Rox.register(flags) Rox.setup(<YOUR_APP_KEY>)
rox := server.NewRox() rox.Register(flags) <-rox.Setup("<YOUR_APP_KEY>")
flags = Container.new() Rox::Server::RoxServer.register(flags) Rox::Server::RoxServer.setup(<YOUR_APP_KEY>)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [ROX register:[[Flags alloc] init]]; [ROX setupWithKey:@<YOUR_APP_KEY>]; return YES; }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { ROX.register("<namespace>", container: Flags()) ROX.setup(withKey: YOUR_APP_KEY) return true }
use Rox\Server\Rox; $container = new Container(); Rox::register($container); Rox::setup("<ROLLOUT_ENV_KEY>");

Using a number flag

The following examples check the flag value:

React Native
JavaScript
Node.js
JavaScript SSR
Android
Java
.NET
Python
C
C++
Go
Ruby
Objective C
Swift
PHP
console.log('Title size is ' + flags.titleColors.getValue());
console.log('Title size is ' + flags.titleColors.getValue());
console.log('Title size is ' + flags.titleColors.getValue());
console.log('Title size is ' + flags.titleColors.getValue());
System.out.println("Title size is " + flags.titleSize.getValue()); System.out.println("The special number is " + flags.specialNumber.getValue());
System.out.println("Title size is " + flags.titleSize.getValue()); System.out.println("The special number is " + flags.specialNumber.getValue());
Console.WriteLine("Title size is {0}", flags.titleSize.GetValue()); Console.WriteLine("The special number is {0}, flags.specialNumber.GetValue());
print('Title size is {}'.format(flags.title_size.get_value())) print('The special number is {}'.format(flags.special_number.get_value()))
printf("Title size is %d\n", rox_get_int(titleSize)); printf("The special number is %f\n", rox_get_double(specialNumber));
std::cout << "Title size is " << titleSize.GetValue() << std::endl; std::cout << "The special number is " << specialNumber.GetValue() << std::endl);
fmt.Println("TitleSize is " + flags.TitleSize.GetValue()) fmt.Println("Percentage is " + flags.Percentage.GetValue())
puts("Title size is #{flags.title_size.value}") puts("The special number is #{flags.special_number.value}")
NSLog(@"Title size is %i", flags.titleSize.value); NSLog(@"The special number is %f", flags.specialNumber.value);
print("Title size is " + flags.titleSize.value) print("The special number is " + flags.specialNumber.value)
echo "Title size is " . $container->titleSize->getValue(); echo "The special number is " . $container->specialNumber->getValue();

Flag freeze is available on client-side SDKs by default.

  • For SDK version 4.x, the default is set to untilForeground.

  • For SDK version 5.0 or later, the default is set to none.

Refer to Understanding a flag freeze for more information.