Documentation
¶
Overview ¶
Package feature implements a simple abstraction for feature flags with dynamic values.
Index ¶
- Variables
- type Flag
- type FlagSet
- func (s *FlagSet) All(yield func(Flag) bool)
- func (s *FlagSet) Bool(name string, opts ...Option) func(context.Context) bool
- func (s *FlagSet) Float(name string, opts ...Option) func(context.Context) float64
- func (s *FlagSet) Int(name string, opts ...Option) func(context.Context) int
- func (s *FlagSet) Lookup(name string) (Flag, bool)
- func (s *FlagSet) SetRegistry(r Registry)
- func (s *FlagSet) String(name string, opts ...Option) func(context.Context) string
- type Labels
- type Option
- type Registry
- type SimpleRegistry
Constants ¶
This section is empty.
Variables ¶
var ErrDuplicateFlag = errors.New("duplicate flag")
ErrDuplicateFlag is returned by [Register] if a with the given name is already registered.
Functions ¶
This section is empty.
Types ¶
type Flag ¶
type Flag struct { // Name is the name of the feature as passed to [Register]. Name string // Description is an optional description specified using [WithDescription]. Description string // Labels contains the labels specified via [WithLabels]. Labels Labels // Func is callback that returns the value for the flag and is either a [BoolFunc], [IntFunc] or [StringFunc]. Func any }
Flag represents a flag registered with a FlagSet.
type FlagSet ¶ added in v0.6.0
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet represents a set of defined feature flags.
The zero value is valid and returns zero values for all flags.
func (*FlagSet) Bool ¶ added in v0.6.0
Bool registers a new flag that represents a boolean value.
If a Flag with the same name is already registered, the call will panic with an error that is ErrDuplicateFlag.
func (*FlagSet) Float ¶ added in v0.6.0
Float registers a new flag that represents a float value.
If a Flag with the same name is already registered, the call will panic with an error that is ErrDuplicateFlag.
func (*FlagSet) Int ¶ added in v0.6.0
Int registers a new flag that represents an int value.
If a Flag with the same name is already registered, the call will panic with an error that is ErrDuplicateFlag.
func (*FlagSet) SetRegistry ¶ added in v0.6.0
SetRegistry sets the Registry to be used for looking up flag values.
A nil value will cause all flags to return zero values.
type Labels ¶ added in v0.6.0
type Labels struct {
// contains filtered or unexported fields
}
Labels is a read only map collection of labels associated with a feature flag.
type Option ¶ added in v0.6.0
type Option func(*Flag)
Option defines options for new flags which can be passed to [Register].
func WithDescription ¶ added in v0.5.0
WithDescription sets the description for a flag.
if given multiple times, only the last value is used.
func WithLabels ¶ added in v0.5.0
WithLabels adds labels to a flag.
If used multiple times, the maps will be merged with later values replacing prior ones.
type Registry ¶ added in v0.6.0
type Registry interface { // Bool returns the boolean value for the flag with the given name. Bool(ctx context.Context, name string) bool // Float returns the float value for the flag with the given name. Float(ctx context.Context, name string) float64 // Int returns the integer value for the flag with the given name. Int(ctx context.Context, name string) int // String returns the string value for the flag with the given name. String(ctx context.Context, name string) string // contains filtered or unexported methods }
Registry defines method for getting the feature flag values by name.
Calling a method when the corresponding struct field is not set will cause the call to panic.
This interface can not be implemented by other packages other except by embedding an existing implementation.
type SimpleRegistry ¶ added in v0.6.0
type SimpleRegistry struct { // BoolFunc contains the implementation for the Registry.Bool function. BoolFunc func(ctx context.Context, name string) bool // FloatFunc contains the implementation for the Registry.Float function. FloatFunc func(ctx context.Context, name string) float64 // IntFunc contains the implementation for the Registry.Int function. IntFunc func(ctx context.Context, name string) int // StringFunc contains the implementation for the Registry.String function. StringFunc func(ctx context.Context, name string) string }
SimpleRegistry implements a Registry using callbacks set as struct fields.
func (*SimpleRegistry) Bool ¶ added in v0.6.0
func (s *SimpleRegistry) Bool(ctx context.Context, name string) bool
Bool implements the Registry interface by calling s.BoolFunc and returning the result.
func (*SimpleRegistry) Float ¶ added in v0.6.0
func (s *SimpleRegistry) Float(ctx context.Context, name string) float64
Float implements the Registry interface by calling s.FloatFunc and returning the result.