feature

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 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", false, "some new feature")

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


Context-specific values

By default, the values returned for each flag will be the default value specified when creating the flag.

The FlagSet.Context method can be used to set custom values for feature flags on a per-context basis.

Example:

package main

import (
	"context"

	"github.com/nussjustin/feature"
)

func main() {
	var set feature.FlagSet

	myFeature := set.Bool("my-feature", false, "some new feature")

	// Enable the feature for our context
	ctx := set.Context(context.Background(),
		feature.BoolValue("my-feature", true))
	
	if myFeature(ctx) {
		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 thrown by methods like FlagSet.Bool if a flag with a given name is already registered.

Functions

This section is empty.

Types

type Flag

type Flag struct {
	// Kind contains the flags kind or type.
	Kind FlagKind

	// Name is the name of the feature flag.
	Name string

	// Value is the default value for the flag as specified on creation.
	Value any

	// Description is an optional description specified using [WithDescription].
	Description string
}

Flag represents a flag registered with a FlagSet.

type FlagKind added in v0.7.0

type FlagKind uint8

FlagKind is an enum of potential flag kinds.

const (
	// FlagKindInvalid is the zero value of FlagKind and is not considered valid value.
	FlagKindInvalid FlagKind = iota

	// FlagKindBool denotes a boolean flag created using [FlagSet.Bool].
	FlagKindBool

	// FlagKindInt denotes a boolean flag created using [FlagSet.Int].
	FlagKindInt

	// FlagKindFloat denotes a boolean flag created using [FlagSet.Float].
	FlagKindFloat

	// FlagKindString denotes a boolean flag created using [FlagSet.String].
	FlagKindString

	// FlagKindUint denotes a boolean flag created using [FlagSet.Uint].
	FlagKindUint
)

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.

A FlagSet must not be copied and should instead be passed around via pointer.

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, value bool, desc string) 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) Context added in v0.7.0

func (s *FlagSet) Context(ctx context.Context, values ...Value) context.Context

Context returns a new context based on ctx which will use the given values when checking feature flags of this set.

If a values type does not match the flags type, Context will panic.

Values with no matching flag are ignored.

func (*FlagSet) Float added in v0.6.0

func (s *FlagSet) Float(name string, value float64, desc string) 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, value int, desc string) 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) String added in v0.6.0

func (s *FlagSet) String(name string, value string, desc string) 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.

func (*FlagSet) Uint added in v0.7.0

func (s *FlagSet) Uint(name string, value uint, desc string) func(context.Context) uint

Uint registers a new flag that represents an uint value.

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

type Value added in v0.7.0

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

Value specifies a custom value for a feature flag, which can be assigned to a context.Context.

A Value must be created using one of BoolValue, FloatValue, IntValue, StringValue or UintValue.

func BoolValue added in v0.7.0

func BoolValue(name string, value bool) Value

BoolValue returns a Value that can be passed to FlagSet.Context to override the value for the given flag.

func FloatValue added in v0.7.0

func FloatValue(name string, value float64) Value

FloatValue returns a Value that can be passed to FlagSet.Context to override the value for the given flag.

func IntValue added in v0.7.0

func IntValue(name string, value int) Value

IntValue returns a Value that can be passed to FlagSet.Context to override the value for the given flag.

func StringValue added in v0.7.0

func StringValue(name string, value string) Value

StringValue returns a Value that can be passed to FlagSet.Context to override the value for the given flag.

func UintValue added in v0.7.0

func UintValue(name string, value uint) Value

UintValue returns a Value that can be passed to FlagSet.Context to override the value for the given flag.

Jump to

Keyboard shortcuts

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