generic

package
v0.0.0-...-9dee9fb Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlagRow

type FlagRow struct {
	Key         string    `json:"key"`
	Type        string    `json:"type"`
	Description string    `json:"description"`
	Enabled     bool      `json:"enabled"`
	DefaultVal  string    `json:"default_val"`
	Tags        []string  `json:"tags"`
	Percentage  float64   `json:"percentage"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

FlagRow represents a row in the feature_flags table.

type OverrideRow

type OverrideRow struct {
	ID       int64  `json:"id"`
	FlagKey  string `json:"flag_key"`
	Scope    string `json:"scope"`     // "user" or "group"
	ScopeKey string `json:"scope_key"` // user ID or group name
	Value    string `json:"value"`
}

OverrideRow represents a flag override for a specific user or group.

type Provider

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

Provider is the built-in generic feature-flag provider. Flag definitions, targeting rules, and overrides are stored in a SQLite database.

Evaluation order:

  1. Disabled check -> return default value
  2. User overrides (scope="user", scope_key=UserKey)
  3. Group overrides (scope="group", matched via "groups" attribute)
  4. Targeting rules (evaluated top-to-bottom by priority)
  5. Percentage rollout (deterministic hash of userKey+flagKey)
  6. Default value

func NewProvider

func NewProvider(store *Store, logger *slog.Logger) *Provider

NewProvider creates a generic provider backed by the given store.

func (*Provider) AllFlags

AllFlags implements featureflag.Provider.

func (*Provider) Evaluate

Evaluate implements featureflag.Provider. It resolves the flag through the six-step evaluation order documented on the Provider type.

func (*Provider) Name

func (p *Provider) Name() string

Name implements featureflag.Provider.

func (*Provider) NotifyChange

func (p *Provider) NotifyChange(key string, value any, flagType featureflag.FlagType)

NotifyChange should be called after mutating a flag (e.g., from the admin API) to inform subscribers of the change.

func (*Provider) Subscribe

func (p *Provider) Subscribe(fn func(featureflag.FlagChangeEvent)) (cancel func())

Subscribe implements featureflag.Provider.

type RuleRow

type RuleRow struct {
	ID         int64  `json:"id"`
	FlagKey    string `json:"flag_key"`
	Priority   int    `json:"priority"`
	Attribute  string `json:"attribute"`
	Operator   string `json:"operator"`
	Value      string `json:"value"`
	ServeValue string `json:"serve_value"`
}

RuleRow represents a targeting rule row.

type Store

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

Store persists flag definitions, targeting rules, and overrides in SQLite.

func NewStore

func NewStore(dbPath string) (*Store, error)

NewStore opens (or creates) a SQLite database at the given path and ensures the schema tables exist.

func NewStoreFromDB

func NewStoreFromDB(db *sql.DB) (*Store, error)

NewStoreFromDB wraps an existing *sql.DB (useful for testing).

func (*Store) AddRule

func (s *Store) AddRule(ctx context.Context, r *RuleRow) error

AddRule inserts a new targeting rule.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database connection.

func (*Store) DeleteFlag

func (s *Store) DeleteFlag(ctx context.Context, key string) error

DeleteFlag removes a flag and its related rules and overrides (cascade).

func (*Store) DeleteOverride

func (s *Store) DeleteOverride(ctx context.Context, id int64) error

DeleteOverride removes an override by ID.

func (*Store) DeleteRule

func (s *Store) DeleteRule(ctx context.Context, id int64) error

DeleteRule removes a targeting rule by ID.

func (*Store) GetFlag

func (s *Store) GetFlag(ctx context.Context, key string) (*FlagRow, error)

GetFlag retrieves a single flag definition by key.

func (*Store) GetOverrides

func (s *Store) GetOverrides(ctx context.Context, flagKey string) ([]OverrideRow, error)

GetOverrides returns all overrides for a flag.

func (*Store) GetRules

func (s *Store) GetRules(ctx context.Context, flagKey string) ([]RuleRow, error)

GetRules returns all targeting rules for a flag, ordered by priority ascending.

func (*Store) ListFlags

func (s *Store) ListFlags(ctx context.Context) ([]FlagRow, error)

ListFlags returns all flag definitions.

func (*Store) UpsertFlag

func (s *Store) UpsertFlag(ctx context.Context, f *FlagRow) error

UpsertFlag creates or updates a flag definition.

func (*Store) UpsertOverride

func (s *Store) UpsertOverride(ctx context.Context, o *OverrideRow) error

UpsertOverride creates or updates an override.

Jump to

Keyboard shortcuts

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