Feature

@Serializable
data class Feature(val uid: String, val isEnabled: Boolean = false, val description: String? = null, val group: String? = null, val permissions: Set<String> = emptySet(), val flippingStrategy: FlippingStrategy? = null, val customProperties: Map<String, Property<*>> = emptyMap())(source)

Represents a feature flag (feature toggle) identified by a unique identifier.

Feature flags enable/disable functionality at runtime, supporting continuous delivery and controlled feature rollouts as introduced by Martin Fowler.

Core Capabilities

  • Runtime Control: Enable or disable features without code changes or redeployment

  • Security Management: Limit feature access to specific user roles/permissions

  • Grouping: Organize features into groups for bulk operations

  • Advanced Strategies: Use FlippingStrategy for A/B testing, gradual rollouts, etc.

  • Custom Properties: Attach metadata and configuration to features

Immutability

This class is immutable by design. All state-changing operations (enable, disable, toggle, addProperty) return a new Feature instance rather than modifying the current one.

Example usage:

val feature = Feature(
uid = "new-checkout-flow",
isEnabled = true,
description = "New streamlined checkout process",
group = "checkout",
permissions = setOf("BETA_TESTERS", "ADMIN")
)

// State changes return new instances
val disabled = feature.disable()
val toggled = feature.toggle()

// Add custom properties
val withProperty = feature.addProperty(
intProperty("maxRetries") {
value = 3
}
)

Throws

if uid is blank or group is blank (when non-null)

Constructors

Link copied to clipboard
constructor(uid: String, isEnabled: Boolean = false, description: String? = null, group: String? = null, permissions: Set<String> = emptySet(), flippingStrategy: FlippingStrategy? = null, customProperties: Map<String, Property<*>> = emptyMap())

Properties

Link copied to clipboard

Custom properties attached to this feature.

Link copied to clipboard

Optional human-readable description of this feature.

Link copied to clipboard

The simple class name of the flippingStrategy, or null if no strategy is set.

Link copied to clipboard

Optional strategy for advanced feature activation logic.

Link copied to clipboard

Optional group name for organizing related features.

Link copied to clipboard

Checks if this feature has a flipping strategy configured.

Link copied to clipboard

Checks if this feature has any permission restrictions.

Link copied to clipboard

Checks if this feature is disabled.

Link copied to clipboard

Whether this feature is currently enabled.

Link copied to clipboard

Set of roles or permissions required to access this feature.

Link copied to clipboard

Returns the set of all custom property names attached to this feature.

Link copied to clipboard
val uid: String

Unique identifier for this feature.

Functions

Link copied to clipboard
fun Feature.addGroup(groupName: String): Feature

Returns a new Feature associated with the specified group.

Link copied to clipboard
fun Feature.addProperties(vararg properties: Property<*>): Feature

Returns a new Feature with multiple properties added.

Link copied to clipboard
fun addProperty(property: Property<*>): Feature

Returns a new Feature instance with the given property added to customProperties.

Link copied to clipboard

Returns a new Feature with all permissions removed.

Link copied to clipboard

Returns a new Feature with all custom properties removed.

Link copied to clipboard

Returns a new Feature instance with isEnabled set to false.

Link copied to clipboard

Returns a new Feature instance with isEnabled set to true.

Link copied to clipboard

Returns all custom properties whose values are of the specified type.

Link copied to clipboard
fun <T> getProperty(propertyId: String): Property<T>?

Retrieves a custom property by name.

Link copied to clipboard

Retrieves a custom property by name, throwing an exception if not found.

Link copied to clipboard
inline fun <T> Feature.getPropertyValueOrDefault(name: String, default: T): T

Retrieves a custom property's value by name, returning a default if not found.

Link copied to clipboard
fun Feature.grantPermissions(vararg permissions: String): Feature

Returns a new Feature with the specified permissions granted.

Link copied to clipboard
fun Feature.hasAllPermissions(vararg permissions: String): Boolean

Checks if this feature has all of the specified permissions.

Link copied to clipboard
fun Feature.hasAnyPermission(vararg permissions: String): Boolean

Checks if this feature has at least one of the specified permissions.

Link copied to clipboard

Checks if a property exists and has a specific value.

Link copied to clipboard

Returns a new Feature removed from its current group.

Link copied to clipboard

Returns a new Feature with the specified properties removed.

Link copied to clipboard
fun Feature.revokePermissions(vararg permissions: String): Feature

Returns a new Feature with the specified permissions revoked.

Link copied to clipboard

Returns a new Feature instance with isEnabled toggled.