Help us shape the future of Go! Take the 2026 Go Developer Survey

memprovider

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 6 Imported by: 3

README

In-memory provider

InMemoryProvider is an OpenFeature compliant provider implementation with an in-memory flag storage.

While the main usage of this provider is SDK testing, you may use it for minimal OpenFeature use cases where appropriate.

Updating flags

UpdateFlags replaces the flag set and emits a PROVIDER_CONFIGURATION_CHANGED event listing the union of the previous and the new flag keys:

provider := memprovider.NewInMemoryProvider(map[string]memprovider.InMemoryFlag{
    "my-flag": {Key: "my-flag", State: memprovider.Enabled, DefaultVariant: "on", Variants: map[string]any{"on": true, "off": false}},
})
if err := openfeature.SetProviderAndWait(provider); err != nil {
    // handle err
}

// handlers registered for openfeature.ProviderConfigChange are invoked
provider.UpdateFlags(map[string]memprovider.InMemoryFlag{
    "my-flag": {Key: "my-flag", State: memprovider.Enabled, DefaultVariant: "off", Variants: map[string]any{"on": true, "off": false}},
})

The provider must be held as *InMemoryProvider, which is what NewInMemoryProvider returns. Events are buffered, so up to five updates made before the provider is registered are still delivered once it is; past that, events are dropped rather than blocking the caller.

Flag ownership

Both NewInMemoryProvider and UpdateFlags copy the map they are given, so mutating it afterwards does not affect the provider. The copy is shallow: the provider takes ownership of each flag's Variants and ContextEvaluator, which must not be mutated once the flag has been handed over. UpdateFlags is the only supported way to change the flag set — it is what emits the PROVIDER_CONFIGURATION_CHANGED event.

Documentation

Overview

Package memprovider provides an in-memory feature flag provider for OpenFeature.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextEvaluator

type ContextEvaluator *func(this InMemoryFlag, flatCtx openfeature.FlattenedContext) (any, openfeature.ProviderResolutionDetail)

ContextEvaluator is a callback to perform openfeature.EvaluationContext backed evaluations. This is a callback implemented by the flag definer.

type InMemoryEvent added in v1.14.0

type InMemoryEvent struct {
	Value             float64
	Data              map[string]any
	ContextAttributes map[string]any
}

type InMemoryFlag

type InMemoryFlag struct {
	Key              string
	State            State
	DefaultVariant   string
	Variants         map[string]any
	ContextEvaluator ContextEvaluator
}

InMemoryFlag is the feature flag representation accepted by InMemoryProvider

func (*InMemoryFlag) Resolve

func (flag *InMemoryFlag) Resolve(defaultValue any, flatCtx openfeature.FlattenedContext) (
	any, openfeature.ProviderResolutionDetail,
)

type InMemoryProvider

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

func NewInMemoryProvider

func NewInMemoryProvider(from map[string]InMemoryFlag) *InMemoryProvider

NewInMemoryProvider returns a provider serving a copy of the given flag set.

The map itself is copied, so mutating it afterwards does not affect the provider. The copy is shallow: the provider takes ownership of each flag's Variants and ContextEvaluator, which must not be mutated once the flag has been handed over. Use InMemoryProvider.UpdateFlags to change the flag set.

func (*InMemoryProvider) BooleanEvaluation

func (i *InMemoryProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, flatCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail

func (*InMemoryProvider) EventChannel added in v1.20.0

func (i *InMemoryProvider) EventChannel() <-chan openfeature.Event

EventChannel implements openfeature.EventHandler. The channel is never closed: the SDK stops listening via its own shutdown signal, and closing would panic a subsequent UpdateFlags.

func (*InMemoryProvider) FloatEvaluation

func (i *InMemoryProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, flatCtx openfeature.FlattenedContext) openfeature.FloatResolutionDetail

func (*InMemoryProvider) Hooks

func (i *InMemoryProvider) Hooks() []openfeature.Hook

func (*InMemoryProvider) IntEvaluation

func (i *InMemoryProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, flatCtx openfeature.FlattenedContext) openfeature.IntResolutionDetail

func (*InMemoryProvider) Metadata

func (i *InMemoryProvider) Metadata() openfeature.Metadata

func (*InMemoryProvider) ObjectEvaluation

func (i *InMemoryProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue any, flatCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail

func (*InMemoryProvider) StringEvaluation

func (i *InMemoryProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, flatCtx openfeature.FlattenedContext) openfeature.StringResolutionDetail

func (*InMemoryProvider) Track added in v1.14.0

func (i *InMemoryProvider) Track(ctx context.Context, trackingEventName string, evalCtx openfeature.EvaluationContext, details openfeature.TrackingEventDetails)

func (*InMemoryProvider) TrackingEvents added in v1.20.0

func (i *InMemoryProvider) TrackingEvents(trackingEventName string) []InMemoryEvent

TrackingEvents returns the events Track recorded for trackingEventName, in the order they were recorded, and an empty slice for a name never tracked.

The returned slice and each event's Data and ContextAttributes maps are copied. Values within those maps and the event Value are not cloned.

func (*InMemoryProvider) UpdateFlags added in v1.20.0

func (i *InMemoryProvider) UpdateFlags(flags map[string]InMemoryFlag)

UpdateFlags replaces the flag set and emits a PROVIDER_CONFIGURATION_CHANGED event naming the union of the previous and the new flag keys, as required by Appendix A of the OpenFeature specification.

As in NewInMemoryProvider, the map is copied shallowly: the provider takes ownership of each flag's Variants and ContextEvaluator, which must not be mutated once the flag has been handed over.

Events are buffered, so an update made before the provider is registered is still delivered once the SDK attaches a listener. Once the buffer is full the event is dropped rather than blocking the caller.

type State

type State string

State of the feature flag

const (
	Enabled  State = "ENABLED"
	Disabled State = "DISABLED"
)

Jump to

Keyboard shortcuts

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