feature

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package feature provides feature flag management with pluggable backends. Flags can be checked globally or per-user, with memory and database drivers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTableSQL

func CreateTableSQL() string

CreateTableSQL returns the SQL to create the feature_flags and feature_flag_users tables.

func RequireFeature

func RequireFeature(mgr *Manager, name string) router.MiddlewareFunc

RequireFeature returns middleware that responds with 404 if the named flag is not active. This hides routes behind feature flags without changing handler code.

func RequireFeatureForUser

func RequireFeatureForUser(mgr *Manager, name string, userIDFunc func(ctx *router.Context) string) router.MiddlewareFunc

RequireFeatureForUser returns middleware that responds with 404 if the named flag is not active for the given user. The userIDFunc extracts the user ID from the request context.

Types

type Database

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

Database is a database-backed feature flag store. It stores flags in a "feature_flags" table and per-user overrides in a "feature_flag_users" table. Create these tables with the provided migration SQL before using this driver.

func NewDatabase

func NewDatabase(db Querier) *Database

NewDatabase creates a database-backed feature flag store.

func (*Database) Active

func (d *Database) Active(ctx context.Context, name string) (bool, error)

Active returns whether a flag is globally enabled.

func (*Database) ActiveFor

func (d *Database) ActiveFor(ctx context.Context, name string, userID string) (bool, error)

ActiveFor returns whether a flag is enabled for a specific user. Falls back to the global flag state if no per-user override exists.

func (*Database) All

func (d *Database) All(ctx context.Context) ([]Flag, error)

All returns all registered flags.

func (*Database) Delete

func (d *Database) Delete(ctx context.Context, name string) error

Delete removes a flag and all its per-user overrides.

func (*Database) Set

func (d *Database) Set(ctx context.Context, name string, active bool) error

Set enables or disables a flag globally. Creates the flag if it doesn't exist.

func (*Database) SetForUser

func (d *Database) SetForUser(ctx context.Context, name string, userID string, active bool) error

SetForUser enables or disables a flag for a specific user. The global flag must exist before setting per-user overrides.

type Flag

type Flag struct {
	Name   string `json:"name"`
	Active bool   `json:"active"`
}

Flag represents a feature flag with its current state.

type Manager

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

Manager provides a convenient API over a Reader for checking feature flags. It silently treats errors as inactive to simplify handler code.

func NewManager

func NewManager(store Reader) *Manager

NewManager creates a feature flag manager backed by the given reader.

func (*Manager) Active

func (m *Manager) Active(ctx context.Context, name string) bool

Active returns true if the named flag is globally enabled. Returns false on any error.

func (*Manager) ActiveFor

func (m *Manager) ActiveFor(ctx context.Context, name string, userID string) bool

ActiveFor returns true if the named flag is enabled for the given user. Returns false on any error.

func (*Manager) Reader

func (m *Manager) Reader() Reader

Reader returns the underlying reader for direct access.

type Memory

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

Memory is an in-memory feature flag store. Thread-safe.

func NewMemory

func NewMemory(initial map[string]bool) *Memory

NewMemory creates an in-memory feature flag store with initial flag values. Pass nil for an empty store.

func (*Memory) Active

func (m *Memory) Active(_ context.Context, name string) (bool, error)

Active returns whether a flag is globally enabled.

func (*Memory) ActiveFor

func (m *Memory) ActiveFor(ctx context.Context, name string, userID string) (bool, error)

ActiveFor returns whether a flag is enabled for a specific user. Falls back to the global flag state if no per-user override exists.

func (*Memory) All

func (m *Memory) All(_ context.Context) ([]Flag, error)

All returns all registered flags.

func (*Memory) Delete

func (m *Memory) Delete(_ context.Context, name string) error

Delete removes a flag entirely, including all per-user overrides.

func (*Memory) Set

func (m *Memory) Set(_ context.Context, name string, active bool) error

Set enables or disables a flag globally.

func (*Memory) SetForUser

func (m *Memory) SetForUser(_ context.Context, name string, userID string, active bool) error

SetForUser enables or disables a flag for a specific user.

type Querier

type Querier interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

Querier abstracts database query execution for the feature flag store. Satisfied by both *database.DB and *database.TX without importing the database package, avoiding circular dependencies.

type Reader

type Reader interface {
	// Active returns whether a flag is globally enabled.
	Active(ctx context.Context, name string) (bool, error)
	// ActiveFor returns whether a flag is enabled for a specific user.
	// Falls back to the global flag state if no per-user override exists.
	ActiveFor(ctx context.Context, name string, userID string) (bool, error)
}

Reader checks feature flag state. This is the interface consumed by Manager and middleware — keep it small for easy faking in tests.

type Store

type Store interface {
	Reader
	Writer
}

Store combines Reader and Writer for full feature flag backends.

type Writer

type Writer interface {
	// Set enables or disables a flag globally.
	Set(ctx context.Context, name string, active bool) error
	// SetForUser enables or disables a flag for a specific user.
	SetForUser(ctx context.Context, name string, userID string, active bool) error
	// Delete removes a flag entirely.
	Delete(ctx context.Context, name string) error
	// All returns all registered flags.
	All(ctx context.Context) ([]Flag, error)
}

Writer mutates feature flag state.

Jump to

Keyboard shortcuts

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