Documentation
¶
Overview ¶
Package flags is a feature flag engine: flags are defined in code with a resolver, evaluated per scope (a user, a team, anything with a key), and the resolved value is persisted in a Store so it stays stable until changed.
The root package depends only on the standard library. Persistent stores live in subpackages (flags/nock) and test helpers in flags/flagstest.
Index ¶
- Variables
- func DefineLike(m *Manager, name string, sample any) error
- func FlagName(ctx context.Context) string
- func MemoizeMiddleware(next http.Handler) http.Handler
- func Require(m *Manager, name string, scope ScopeFunc, opts ...RequireOption) func(http.Handler) http.Handler
- func RequireAny(m *Manager, scope ScopeFunc, names ...string) func(http.Handler) http.Handler
- func RequireAnyWith(m *Manager, scope ScopeFunc, names []string, opts ...RequireOption) func(http.Handler) http.Handler
- func ScopedValue[V any](ctx context.Context, s Scoped, name string) (V, error)
- func SortEntries(es []Entry)
- func TemplateFuncs(ctx context.Context, m *Manager, scope Scope) map[string]any
- func Value[V any](ctx context.Context, m *Manager, name string, scope Scope) (V, error)
- func WithRequestCache(ctx context.Context) context.Context
- type BoolFlag
- type Entry
- type Flag
- func (f Flag[V]) Active(ctx context.Context, scope Scope) (bool, error)
- func (f Flag[V]) Forget(ctx context.Context, scope Scope) error
- func (f Flag[V]) Name() string
- func (f Flag[V]) Set(ctx context.Context, scope Scope, v V) error
- func (f Flag[V]) Value(ctx context.Context, scope Scope) (V, error)
- type Manager
- func (m *Manager) Activate(ctx context.Context, name string, scope Scope) error
- func (m *Manager) Active(ctx context.Context, name string, scope Scope) (bool, error)
- func (m *Manager) AllActive(ctx context.Context, scope Scope) ([]string, error)
- func (m *Manager) Deactivate(ctx context.Context, name string, scope Scope) error
- func (m *Manager) Define(name string, r Resolver[bool]) BoolFlag
- func (m *Manager) FlushCache(ctx context.Context)
- func (m *Manager) For(scope Scope) Scoped
- func (m *Manager) Forget(ctx context.Context, name string, scope Scope) error
- func (m *Manager) Names() []string
- func (m *Manager) Purge(ctx context.Context, names ...string) error
- func (m *Manager) Set(ctx context.Context, name string, scope Scope, value any) error
- func (m *Manager) SetForEveryone(ctx context.Context, name string, value any) error
- func (m *Manager) Store() Store
- type MemStore
- func (s *MemStore) Delete(_ context.Context, flag, scope string) error
- func (s *MemStore) Get(_ context.Context, flag, scope string) ([]byte, bool, error)
- func (s *MemStore) List(_ context.Context, flag string) ([]Entry, error)
- func (s *MemStore) Purge(_ context.Context, flags ...string) error
- func (s *MemStore) Set(_ context.Context, flag, scope string, value []byte) error
- type Option
- type RequireOption
- type Resolver
- type Scope
- type ScopeFunc
- type Scoped
- func (s Scoped) Activate(ctx context.Context, name string) error
- func (s Scoped) Active(ctx context.Context, name string) (bool, error)
- func (s Scoped) AllActive(ctx context.Context) ([]string, error)
- func (s Scoped) Deactivate(ctx context.Context, name string) error
- func (s Scoped) Forget(ctx context.Context, name string) error
- func (s Scoped) Set(ctx context.Context, name string, value any) error
- type Store
- type Variant
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnknownFlag is returned for a flag that was never defined. ErrUnknownFlag = errors.New("flags: unknown flag") // ErrTypeMismatch is returned when a value does not have the flag's type. ErrTypeMismatch = errors.New("flags: type mismatch") )
Functions ¶
func DefineLike ¶
DefineLike defines a flag called name whose type is the type of sample and whose resolver yields that type's zero value. It does nothing if name is already defined. It exists for tools and tests that learn a flag's type only at run time; application code uses Define.
func FlagName ¶
FlagName returns the name of the flag whose resolver is running, or "" when ctx does not belong to a resolver.
func MemoizeMiddleware ¶
MemoizeMiddleware gives every request its own request cache.
func Require ¶
func Require(m *Manager, name string, scope ScopeFunc, opts ...RequireOption) func(http.Handler) http.Handler
Require returns middleware that serves the route only when the flag is active for the request's scope, and the fallback (404 by default) otherwise. It works with net/http and with github.com/fletchhq/fletch/router, whose middleware are func(http.Handler) http.Handler. The request gets a request cache if it has none.
func RequireAny ¶
RequireAny is Require satisfied by any one of names.
func RequireAnyWith ¶
func RequireAnyWith(m *Manager, scope ScopeFunc, names []string, opts ...RequireOption) func(http.Handler) http.Handler
RequireAnyWith is RequireAny with options.
func ScopedValue ¶
ScopedValue returns the value of a flag for the scope bound to s.
func SortEntries ¶
func SortEntries(es []Entry)
SortEntries orders entries by flag, then scope, the order List promises.
func TemplateFuncs ¶
TemplateFuncs returns helper functions for templates, bound to ctx and scope: flagActive(name) bool and flagValue(name) any. Both treat an evaluation error as off (false and nil), so a broken store hides a feature instead of failing the page. Merge the map into the template engine's function set; pass a context from WithRequestCache so repeated calls in one render read the Store once.
func WithRequestCache ¶
WithRequestCache returns a context that memoizes flag values: within it, each (flag, scope) pair is read from the Store at most once. Set, Forget, Purge, and FlushCache on a Manager invalidate the memoized entries. A context that already has a request cache is returned unchanged.
Types ¶
type Flag ¶
type Flag[V any] struct { // contains filtered or unexported fields }
Flag is a defined flag whose values have type V.
func Define ¶
Define registers a flag called name with the manager and returns a typed handle. It panics if name is empty, r is nil, or name is already defined.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager evaluates flags against a Store.
func NewManager ¶
NewManager returns a Manager that persists values in store.
func (*Manager) Active ¶
Active reports whether the flag is on for scope. A boolean flag is on when true; a flag of any other type is on when its value is not the zero value.
func (*Manager) AllActive ¶
AllActive returns the sorted names of the defined flags that are on for scope.
func (*Manager) Deactivate ¶
Deactivate turns a boolean flag off for scope.
func (*Manager) FlushCache ¶
FlushCache empties the request cache of ctx. It does nothing when ctx has none.
func (*Manager) Forget ¶
Forget removes the stored value for scope, so the resolver runs again on the next read. Forgetting Everyone removes the override.
func (*Manager) Purge ¶
Purge removes every stored value of the named flags, or of all flags when none is named.
func (*Manager) Set ¶
Set stores value as the flag's value for scope. value must have the flag's type.
func (*Manager) SetForEveryone ¶
SetForEveryone stores value as an override that wins over every scope's own value and over the resolver.
type MemStore ¶
type MemStore struct {
// contains filtered or unexported fields
}
MemStore is a Store held in memory.
type Option ¶
type Option func(*Manager)
Option configures a Manager.
func WithAllowUnknown ¶
func WithAllowUnknown() Option
WithAllowUnknown makes Active report false, and Set and Forget do nothing, for flags that were never defined, instead of returning ErrUnknownFlag.
type RequireOption ¶
type RequireOption func(*requireConfig)
RequireOption configures Require.
func WithErrorHandler ¶
func WithErrorHandler(fn func(w http.ResponseWriter, r *http.Request, err error)) RequireOption
WithErrorHandler sets what happens when evaluating a flag fails. The default responds 500.
func WithFallback ¶
func WithFallback(h http.Handler) RequireOption
WithFallback sets the handler that runs instead of the route when the flag is off. The default responds 404.
type Resolver ¶
Resolver computes a flag's value for a scope the first time it is needed. The result is stored, so the resolver does not run again for that scope until the value is forgotten. FlagName(ctx) returns the flag's name.
func Rollout ¶
Rollout returns a resolver that is true for roughly pct percent of scopes. A scope's bucket depends only on the flag name and the scope key, so a scope keeps its answer across calls and raising pct never turns anyone off. Values of pct at or below 0 match nobody; at or above 100 match everyone.
func RolloutVariants ¶
RolloutVariants returns a resolver that assigns each scope one of the variants in proportion to its weight, deterministically per flag and scope. Variants with non-positive weight are never chosen; with no positive weight the zero V is returned.
type Scope ¶
type Scope interface {
// ScopeKey returns a stable identifier such as "user:42".
ScopeKey() string
}
Scope is what a flag is evaluated for: a user, a team, a tenant.
var ( // Global is the scope of flags that do not depend on who asks. A nil // Scope is treated as Global. Global Scope = keyScope(globalKey) // Everyone is the scope of an override that applies to every scope. // Setting a flag for Everyone wins over per-scope values and resolvers. Everyone Scope = keyScope(everyoneKey) )
func StringScope ¶
StringScope returns a scope keyed "kind:id".
type ScopeFunc ¶
ScopeFunc picks the scope a request is evaluated for, typically the signed in user. A nil ScopeFunc, or one that returns nil, means Global.
type Scoped ¶
type Scoped struct {
// contains filtered or unexported fields
}
Scoped is a Manager bound to one scope.
func (Scoped) Deactivate ¶
Deactivate turns the flag off for the bound scope.
type Store ¶
type Store interface {
// Get returns the value stored for flag and scope; ok is false when none
// is stored.
Get(ctx context.Context, flag, scope string) (value []byte, ok bool, err error)
// Set stores value, replacing any earlier value.
Set(ctx context.Context, flag, scope string, value []byte) error
// Delete removes one value. Deleting a missing value is not an error.
Delete(ctx context.Context, flag, scope string) error
// List returns the entries of flag ordered by scope, or the entries of
// every flag ordered by flag then scope when flag is empty.
List(ctx context.Context, flag string) ([]Entry, error)
// Purge removes every value of the named flags, or of all flags when none
// is named.
Purge(ctx context.Context, flags ...string) error
}
Store persists resolved flag values, keyed by flag name and scope key. Values are opaque encoded bytes. Implementations must be safe for concurrent use.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package flagstest helps test code that uses flags: an in-memory manager with forced values, and a contract suite for Store implementations.
|
Package flagstest helps test code that uses flags: an in-memory manager with forced values, and a contract suite for Store implementations. |
|
Package nock is a flags.Store backed by a SQL database through github.com/fletchhq/fletch/nock.
|
Package nock is a flags.Store backed by a SQL database through github.com/fletchhq/fletch/nock. |