feature

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: MIT Imports: 7 Imported by: 0

README

feature Go Reference Lint Test

Package feature implements a simple abstraction for feature flags with arbitrary values.

Examples

Registering a flag

A flag is registered on a FlagSet.

Flags are created using a specific method based on the type of the value of the flag, named after the type.

Currently, the supported methods are

Each method will return a callback that takes a context.Context and returns a value of the specific type.

Additionally each method can take an arbitrary number of options for adding metadata to the flag.

For example:

package main

import (
	"context"

	"github.com/nussjustin/feature"
)

func main() {
	var set feature.FlagSet

	myFeature := set.Bool("my-feature", flag.WithDescription("enables the new feature"))

	if myFeature(context.Background()) {
		println("my-feature enabled") // never runs, see next section
	}
}


Configuring a registry

By default, the values returned for each flag will be the zero value for the specific type.

A Registry can be used to dynamically generate / fetch values for each flag.

The package currently ships with a single implementation SimpleStrategy. External implementations are currently not supported.

Once created, a registry can used by calling the FlagSet.SetRegistry method.

Example:

package main

import (
	"context"

	"github.com/nussjustin/feature"
)

func main() {
	var set feature.FlagSet

	set.SetStrategy(&feature.SimpleStrategy{
		BoolFunc: func(ctx context.Context, name string) bool {
			return name == "my-feature"
		},
	})

	myFeature := set.Bool("my-feature", flag.WithDescription("enables the new feature"))

	if myFeature(context.Background()) {
		println("my-feature enabled")
	}
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Overview

Package feature implements a simple abstraction for feature flags with dynamic values.

Index

Constants

This section is empty.

Variables

View Source
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) All added in v0.6.0

func (s *FlagSet) All(yield func(Flag) bool)

All yields all registered flags sorted by name.

func (*FlagSet) Bool added in v0.6.0

func (s *FlagSet) Bool(name string, opts ...Option) func(context.Context) bool

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

func (s *FlagSet) Float(name string, opts ...Option) func(context.Context) float64

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

func (s *FlagSet) Int(name string, opts ...Option) func(context.Context) int

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) Lookup added in v0.6.0

func (s *FlagSet) Lookup(name string) (Flag, bool)

Lookup returns the flag with the given name.

func (*FlagSet) SetRegistry added in v0.6.0

func (s *FlagSet) SetRegistry(r Registry)

SetRegistry sets the Registry to be used for looking up flag values.

A nil value will cause all flags to return zero values.

func (*FlagSet) String added in v0.6.0

func (s *FlagSet) String(name string, opts ...Option) func(context.Context) string

String registers a new flag that represents a string value.

If a Flag with the same name is already registered, the call will panic with an error that is ErrDuplicateFlag.

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.

func (*Labels) All added in v0.6.0

func (l *Labels) All(yield func(string, string) bool)

All yields all labels.

func (*Labels) Len added in v0.6.0

func (l *Labels) Len() int

Len returns the number of labels.

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

func WithDescription(desc string) Option

WithDescription sets the description for a flag.

if given multiple times, only the last value is used.

func WithLabel added in v0.6.0

func WithLabel(key, value string) Option

WithLabel adds a label to a flag.

func WithLabels added in v0.5.0

func WithLabels(labels map[string]string) Option

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.

func (*SimpleRegistry) Int added in v0.6.0

func (s *SimpleRegistry) Int(ctx context.Context, name string) int

Int implements the Registry interface by calling s.IntFunc and returning the result.

func (*SimpleRegistry) String added in v0.6.0

func (s *SimpleRegistry) String(ctx context.Context, name string) string

String implements the Registry interface by calling s.StringFunc and returning the result.

Jump to

Keyboard shortcuts

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