Creating a string flag

7 minute read

String flags can have multiple text 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 string 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 string flag, with values such as URLs. In this case, you can define the flag in the code with just the default string value. In the CloudBees Feature Management UI, a free text input field then allows you to enter any string 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 string flag name is derived from the flag variable name.

Creating a string flag in the UI

To create a string flag in the UI, select the String 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 string flags in your code using the static API. A container class is required, and you define your flag inside the container class.

For SDK version 5.0 or later

For SDK version 5.0 or later, use the following Rox.RoxString examples defining a container class and a string flag. In these examples, the titleColors flag, which specifies the color of a title, can be either White, Blue, Green or Yellow. By default, the color is White.

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 = { titleColors: new Rox.RoxString('White', ['White', 'Blue', 'Green', 'Yellow']), };
const flags = { titleColors: new Rox.RoxString('White', ['White', 'Blue', 'Green', 'Yellow']), };
const flags = { titleColors: new Rox.RoxString('White', ['White', 'Blue', 'Green', 'Yellow']), };
import {RoxString} from 'rox-ssr'; const flags = { titleColors: new RoxString('White', ['White', 'Blue', 'Green', 'Yellow']), };
import io.rollout.configuration.RoxContainer; import io.rollout.flags.Freeze; import io.rollout.flags.RoxString; public static class Container implements RoxContainer { public final RoxString titleColors = new RoxString("White", new String[]{ "White", "Blue", "Green", "Yellow" }, Freeze.UntilLaunch); }
import io.rollout.configuration.RoxContainer; import io.rollout.flags.RoxString; public static class Container implements RoxContainer { public final RoxString titleColors = new RoxString("White", new String[]{ "White", "Blue", "Green", "Yellow" }); }
using Io.Rollout.Rox.Server.Flags; public class Container : IRoxContainer { public RoxString TitleColorsVariant = new RoxString("White", new String[] {"White", "Blue", "Green", "Yellow"}); }
from rox.core.entities.rox_string import RoxString class MyContainer: def __init__(self): self.title_colors = RoxString('White', ['White', 'Blue', 'Green', 'Yellow'])
#include <rox/server.h> RoxStringBase* titleColors = rox_add_string_with_options("demo.titleColors", "White", ROX_LIST_COPY_STR( "White", "Blue", "Green", "Yellow" ));
#include <rox/client.h> RoxStringBase* titleColors = rox_add_string_with_freeze_and_options("demo.titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow"), RoxFreezeUntilLaunch);
#include <roxx/server.h> using namespace Rox; auto titleColors = String::Create("demo.titleColors", "White", {"White", "Blue", "Green", "Yellow"});
#include <roxx/client.h> using namespace Rox::Client; auto titleColors = String::Create("demo.titleColors", "White", {"White", "Blue", "Green", "Yellow"}, RoxFreezeNone);
import "github.com/rollout/rox-go/v5/server" type Flags struct { TitleColors server.RoxString } var flags = & Flags { TitleColors: server.NewRoxString("White", [] string {"White", "Blue", "Green", "Yellow"}), }
require 'rox/server/rox_server' require 'rox/server/flags/rox_string' class Container attr_accessor :title_colors def initialize @title_colors = Rox::Server::RoxString.new('White', ['White', 'Blue', 'Green', 'Yellow']) end end
#import <ROXCore/ROXCore.h> @interface Flags : ROXBaseContainer @property (nonatomic) ROXString* titleColors; @end @implementation Flags - (instancetype)init { self = [super init]; if (self) { self.titleColors = [[ROXString alloc] initWithDefault:@"White" variations:@[@"Blue", @"Green", @"Yellow"]]; } return self; }
public class Flags : RoxContainer { let titleColors = RoxString(withDefault: "White", variations: ["White", "Blue", "Green", "Yellow"]) }
use Rox\Server\Rox; use Rox\Server\Flags\RoxString; class Container { public $titleColors; public function __construct() { $this->titleColors = new RoxString("White", ["Blue", "Green", "Yellow"]); } }

For SDK version 4.x

For SDK version 4.x, use the following Rox.RoxVariant examples defining a container class and a string flag. In these examples, the titleColors flag, which specifies the color of a title, can be either White, Blue, Green, or Yellow. The default color is White.

Swift
Objective C
React Native
JavaScript
Node.js
JavaScript SSR
Android
Java
.NET
Python
Go
Ruby
PHP
C
C++
import ROX import ROXCore class Flags : RoxContainer { let titleColors = ROXVariant(withDefault: "White", options: ["White", "Blue", "Green", "Yellow"]) // for Enum-based string flag let titleColorsEnum = RoxEnumVariant(Colors.White) } enum Colors : Int { case White case Blue case Green case Yellow }
#import <ROXCore/ROXCore.h> @interface Flags : ROXBaseContainer @property (nonatomic) ROXVariant* titleColors; @end #import "Flags.h" @implementation Flags - (instancetype)init { self = [super init]; if (self) { self.titleColors = [[ROXVariant alloc] initWithDefault:@"White" options:@[@"Blue", @"Green", @"Yellow"]]; } return self; } @end
const flags = { titleColors: new Rox.Variant('White', ['White', 'Blue', 'Green', 'Yellow']), };
const flags = { titleColors: new Rox.Variant('White', ['White', 'Blue', 'Green', 'Yellow']), };
const flags = { titleColors: new Rox.Variant('White', ['White', 'Blue', 'Green', 'Yellow']), };
import {Variant} from 'rox-ssr'; const flags = { titleColors: new Variant('White', ['White', 'Blue', 'Green', 'Yellow']), };
public class Flags implements RoxContainer{ public RoxVariant titleColors = new RoxVariant("White", new String[] {"White", "Blue", "Green", "Yellow"}); public enum Color { WHITE, BLUE, GREEN, YELLOW } // for Enum-based string flag public RoxEnumVariant<Color> titleColorsEnum = new RoxEnumVariant<>(Color.WHITE);
public class Flags implements RoxContainer{ public RoxVariant titleColors = new RoxVariant("White", new String[] {"White", "Blue", "Green", "Yellow"}); public enum Color { WHITE, BLUE, GREEN, YELLOW } // for Enum-based string flag public RoxEnumVariant<Color> titleColorsEnum = new RoxEnumVariant<>(Color.WHITE);
using Io.Rollout.Rox.Server.Flags; public class Container : IRoxContainer { public RoxVariant TitleColorsVariant = new RoxVariant("White", new String[] {"White", "Blue", "Green", "Yellow"}); }
from roxserver.flags.rox_flag import RoxFlag from roxserver.flags.rox_variant import RoxVariant class MyContainer: def __init__(self): self.title_colors = RoxVariant('White', ['White', 'Blue', 'Green', 'Yellow'])
import ( "github.com/rollout/rox-go/v5/server" ) type Container struct { titleColors server.RoxVariant } var flags = &Container { titleColors: server.NewRoxVariant("White", []string{"White", "Blue", "Green", "Yellow"})}
require 'rox/server/rox_server' require 'rox/server/flags/rox_variant' class Container attr_accessor :title_colors def initialize @title_colors = Rox::Server::RoxVariant.new('White', ['White', 'Blue', 'Green', 'Yellow']) end end
use Rox\Server\Rox; use Rox\Server\Flags\RoxVariant; class Container { public $titleColors; public function __construct() { $this->titleColors = new RoxVariant("White", ["Blue", "Green", "Yellow"]); } }
RoxVariant *titleColors = rox_add_variant("demo.titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow"));
Rox::Variant *titleColors = Rox::Variant::Create("demo.titleColors", "White", std::vector<std::string>{"White", "Blue", "Green", "Yellow"});

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:

Swift
Objective C
React Native
JavaScript
Node.js
JavaScript SSR
Android
Java
.NET
Python
Go
Ruby
PHP
C
C Client Side
C++
C++ Client Side
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { ROX.register("<namespace>", container: Flags()) ROX.setup(withKey: YOUR_APP_KEY) return true }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [ROX register:[[Flags alloc] init]]; [ROX setupWithKey:@<YOUR_APP_KEY>]; return YES; }
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(); Flags flags = new Flags(); Rox.register("<namespace>", flags); Rox.setup(this); // This can also be done in the MainActivity if your app doesn't extend the Application class } }
Flags flags = new Flags(); Rox.register("<namespace>", flags); Rox.setup(YOUR_APP_KEY);
Container flags = new Container(); Rox.Register("<namespace>", flags); await Rox.Setup(<YOUR_APP_KEY>);
from rox.server.rox_server import Rox flags = MyContainer() Rox.register(<namespace>, flags) cancel_event = Rox.setup('<YOUR_APP_KEY>').result()
import "github.com/rollout/rox-go/v5/server" type Container struct {} var flags = &Container {} var rox *server.Rox func initRox() { options := server.NewRoxOptions(server.RoxOptionsBuilder {}) rox = server.NewRox() rox.Register("<namespace>", flags) rox.Setup("<YOUR_APP_KEY>", options) }
require 'rox/server/rox_server' class Container end flags = Container.new Rox::Server::RoxServer.register(<namespace>, flags) Rox::Server::RoxServer.setup(<YOUR_APP_KEY>).join
use Rox\Server\Rox; use Rox\Server\Flags\RoxString; class Container { public $titleColors; public function __construct() { $this->titleColors = new RoxString("White", ["White", "Blue", "Green", "Yellow"]); } } $flags = new Container(); Rox::register(<namespace>, $flags); Rox::setup(<YOUR_APP_KEY>);
#include <rox/server.h> #define DEFAULT_API_KEY "YOUR_APP_KEY" int main(int argc, char **argv) { RoxStringBase *titleColor = rox_add_string_with_options("titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow")); rox_setup(DEFAULT_API_KEY, NULL); rox_shutdown(); }
#include <rox/client.h> #define DEFAULT_API_KEY "YOUR_APP_KEY" int main(int argc, char **argv) { RoxStringBase *titleColor = rox_add_string_with_freeze_and_options("titleColors", "White", ROX_LIST_COPY_STR("White", "Blue", "Green", "Yellow"), RoxFreezeUntilLaunch); rox_setup(DEFAULT_API_KEY, NULL); rox_shutdown(); }
#include <roxx/server.h> #define DEFAULT_API_KEY "YOUR_APP_KEY" using namespace Rox; int main(int argc, char **argv) { auto titleColor = Rox::String::Create("titleColors", "White", {"White", "Blue", "Green", "Yellow"}); Rox::Setup(DEFAULT_API_KEY, NULL); Rox::Shutdown(); }
#include <roxx/client.h> #define DEFAULT_API_KEY "YOUR_APP_KEY" using namespace Rox::Client; int main(int argc, char **argv) { auto titleColor = Rox::Client::String::Create("titleColors", "White", {"White", "Blue", "Green", "Yellow"}, RoxFreezeUntilLaunch); Rox::Setup(DEFAULT_API_KEY, NULL); Rox::Shutdown(); }

Using a string flag

The following examples check the flag value:

Swift
Objective C
React Native
JavaScript
Node.js
JavaScript SSR
Java, Android
.NET
Python
Go
Ruby
PHP
C
C++
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") }
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"); }
if (flags.titleColors.getValue() === 'Blue') { console.log('Title color is blue'); } else if (flags.titleColors.getValue() === 'Green') { console.log('Title color is green'); } else if (flags.titleColors.getValue() === 'Yellow') { console.log('Title color is yellow'); } else if (flags.titleColors.getValue() === 'White') { console.log('Title color is white'); }
if (flags.titleColors.getValue() === 'Blue') { console.log('Title color is blue'); } else if (flags.titleColors.getValue() === 'Green') { console.log('Title color is green'); } else if (flags.titleColors.getValue() === 'Yellow') { console.log('Title color is yellow'); } else if (flags.titleColors.getValue() === 'White') { console.log('Title color is white'); }
if (flags.titleColors.getValue() === 'Blue') { console.log('Title color is blue'); } else if (flags.titleColors.getValue() === 'Green') { console.log('Title color is green'); } else if (flags.titleColors.getValue() === 'Yellow') { console.log('Title color is yellow'); } else if (flags.titleColors.getValue() === 'White') { console.log('Title color is white'); }
if (flags.titleColors.getValue() === 'Blue') { console.log('Title color is blue'); } else if (flags.titleColors.getValue() === 'Green') { console.log('Title color is green'); } else if (flags.titleColors.getValue() === 'Yellow') { console.log('Title color is yellow'); } else if (flags.titleColors.getValue() === 'White') { console.log('Title color is white'); }
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; }
if (flags.TitleColorsVariant.GetValue().Equals("Black")) { // set title color to black } else if (flags.TitleColorsVariant.GetValue().Equals("Green")) { // set title color to green }
print('color is %s' % my_container.title_colors.get_value())
switch flags.titleColors.GetValue(nil) { case "Green" : fmt.Println("green"); case "White" : fmt.Println("white"); case "Blue" : fmt.Println("blue"); }
puts "color is #{flags.title_colors.value}"
echo "color is " . $flags->titleColors->getValue();
printf("color is: %s", rox_get_string(titleColor));
printf("color is: %s", titleColor->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.