flags

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Active

func Active(name string) bool

Active checks a flag on the global manager (no user context).

func Define

func Define(name string, cfg Config)

Define registers a flag on the global manager.

func FlagGate

func FlagGate(m *Manager, flagName string) router.Middleware

FlagGate returns middleware that gates a route behind a feature flag.

func RequireFlag

func RequireFlag(m *Manager) router.Middleware

RequireFlag returns middleware that returns 404 if the flag is not active. Usage: router.Use(flags.RequireFlag(manager)("flag-name")) Or via named middleware: "feature:flag-name"

func Variant

func Variant(name, userID string) string

Variant on the global manager.

Types

type Config

type Config struct {
	Default        bool       `json:"default"`
	Description    string     `json:"description,omitempty"`
	RolloutPercent int        `json:"rollout_percent,omitempty"` // 0-100
	Groups         []string   `json:"groups,omitempty"`          // group names
	Users          []string   `json:"users,omitempty"`           // specific user IDs
	Variants       []string   `json:"variants,omitempty"`        // for A/B tests
	ExpiresAt      *time.Time `json:"expires_at,omitempty"`
}

Config describes a feature flag.

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

FileStore persists flags to a JSON file on disk.

func NewFileStore

func NewFileStore(path string) *FileStore

func (*FileStore) Load

func (s *FileStore) Load(_ context.Context) (map[string]*Flag, error)

func (*FileStore) Save

func (s *FileStore) Save(_ context.Context, flags map[string]*Flag) error

type Flag

type Flag struct {
	Name      string    `json:"name"`
	Config    Config    `json:"config"`
	Enabled   bool      `json:"enabled"`
	UpdatedAt time.Time `json:"updated_at"`
}

Flag is the runtime representation.

type FlagPlugin

type FlagPlugin struct {
	nimbus.BasePlugin
	Manager *Manager
}

FlagPlugin integrates feature flags with Nimbus.

func NewPlugin

func NewPlugin(store Store) *FlagPlugin

NewPlugin creates a new feature flag plugin.

func (*FlagPlugin) Boot

func (p *FlagPlugin) Boot(app *nimbus.App) error

func (*FlagPlugin) DefaultConfig

func (p *FlagPlugin) DefaultConfig() map[string]any

func (*FlagPlugin) Middleware

func (p *FlagPlugin) Middleware() map[string]router.Middleware

Middleware returns named middleware for flag-based access control.

func (*FlagPlugin) Register

func (p *FlagPlugin) Register(app *nimbus.App) error

func (*FlagPlugin) RegisterRoutes

func (p *FlagPlugin) RegisterRoutes(r *router.Router)

RegisterRoutes mounts the flags API.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is the central feature flag registry.

func Default

func Default() *Manager

Default returns the global manager.

func New

func New(store Store) *Manager

New creates a new feature flag manager.

func (*Manager) Active

func (m *Manager) Active(name string, user *UserContext) bool

Active checks if a flag is active for the given user context.

func (*Manager) All

func (m *Manager) All() map[string]*Flag

All returns all defined flags.

func (*Manager) Define

func (m *Manager) Define(name string, cfg Config)

Define registers a feature flag.

func (*Manager) Disable

func (m *Manager) Disable(name string) error

Disable turns a flag off globally.

func (*Manager) Enable

func (m *Manager) Enable(name string) error

Enable turns a flag on globally.

func (*Manager) Group

func (m *Manager) Group(name string, resolver func(UserContext) bool)

Group registers a group resolver function.

func (*Manager) LoadFromEnv

func (m *Manager) LoadFromEnv()

LoadFromEnv loads flags from environment variables. Pattern: FEATURE_FLAG_NAME=true|false

func (*Manager) SetRollout

func (m *Manager) SetRollout(name string, percent int) error

SetRollout updates the rollout percentage.

func (*Manager) Variant

func (m *Manager) Variant(name string, userID string) string

Variant returns which variant a user gets for an A/B test flag.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

func (*MemoryStore) Load

func (s *MemoryStore) Load(_ context.Context) (map[string]*Flag, error)

func (*MemoryStore) Save

func (s *MemoryStore) Save(_ context.Context, flags map[string]*Flag) error

type Store

type Store interface {
	Load(ctx context.Context) (map[string]*Flag, error)
	Save(ctx context.Context, flags map[string]*Flag) error
}

Store persists feature flag state.

type UserContext

type UserContext struct {
	ID     string
	Groups []string
	Attrs  map[string]string
}

UserContext represents a user for flag evaluation.

type UserEvaluator

type UserEvaluator struct {
	// contains filtered or unexported fields
}

UserEvaluator evaluates flags for a specific user.

func For

func For(userID string, groups ...string) *UserEvaluator

For creates a user-scoped evaluator.

func (*UserEvaluator) Active

func (e *UserEvaluator) Active(name string) bool

func (*UserEvaluator) Variant

func (e *UserEvaluator) Variant(name string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL