ffcontext

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ffcontext defines the evaluation context used to evaluate the flags.

Index

Constants

View Source
const LegacyReservedKey = "gofeatureflag"

LegacyReservedKey is the name that GO Feature Flag clients send for ReservedKey. It is still accepted, ReservedKey wins when both are set.

View Source
const ReservedKey = "semaphore"

ReservedKey is the evaluation context attribute holding the Semaphore specific fields (currentDateTime, flagList, exporterMetadata).

Variables

This section is empty.

Functions

func HasReservedKey

func HasReservedKey(attributes map[string]any) bool

HasReservedKey tells if the attributes carry the reserved attribute, under any of its names.

Types

type Context

type Context interface {
	// GetKey return the unique targetingKey for the context.
	GetKey() string
	// IsAnonymous return if the context is about an anonymous user or not.
	IsAnonymous() bool
	// GetCustom return all the attributes properties added to the context.
	GetCustom() map[string]any
	// AddCustomAttribute allows to add a attributes attribute into the context.
	AddCustomAttribute(name string, value any)
	// ExtractGOFFProtectedFields extract the goff specific attributes from the evaluation context.
	ExtractGOFFProtectedFields() GoffContextSpecifics
}

Context is the evaluation context interface used to evaluate the flags.

type EvaluationContext

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

EvaluationContext contains specific attributes for your evaluation. Most of the time it is identifying a user browsing your site. The only mandatory property is the Key, which must a unique identifier. For authenticated users, this may be a username or e-mail address. For anonymous users, this could be an IP address or session ID.

EvaluationContext fields are immutable and can be accessed only via getter methods. To construct an EvaluationContext, use either a simple constructor (NewEvaluationContext) or the builder pattern with NewEvaluationContextBuilder.

func NewAnonymousEvaluationContext deprecated

func NewAnonymousEvaluationContext(key string) EvaluationContext

NewAnonymousEvaluationContext returns an anonymous evaluation context with the given key.

Deprecated: NewAnonymousEvaluationContext is here for compatibility reason. Please use NewEvaluationContext instead and add a attributes attribute to know that it is an anonymous user.

ctx := NewEvaluationContext("my-targetingKey") ctx.AddCustomAttribute("anonymous", true).

func NewEvaluationContext

func NewEvaluationContext(key string) EvaluationContext

NewEvaluationContext creates a new evaluation context identified by the given targetingKey.

func (EvaluationContext) AddCustomAttribute

func (u EvaluationContext) AddCustomAttribute(name string, value any)

AddCustomAttribute allows to add a attributes attribute into the user.

func (EvaluationContext) ExtractGOFFProtectedFields

func (u EvaluationContext) ExtractGOFFProtectedFields() GoffContextSpecifics

ExtractGOFFProtectedFields extract the Semaphore specific attributes from the evaluation context. They are read from the "semaphore" attribute and, for the clients of Semaphore, from the legacy "gofeatureflag" attribute. When both are set, a field of "semaphore" wins.

func (EvaluationContext) GetCustom

func (u EvaluationContext) GetCustom() map[string]any

GetCustom return all the attributes properties of a user.

func (EvaluationContext) GetKey

func (u EvaluationContext) GetKey() string

GetKey return the unique targetingKey for the user.

func (EvaluationContext) IsAnonymous

func (u EvaluationContext) IsAnonymous() bool

IsAnonymous return if the user is anonymous or not.

func (EvaluationContext) MarshalJSON

func (u EvaluationContext) MarshalJSON() ([]byte, error)

MarshalJSON is a custom JSON marshaller for EvaluationContext. It will only marshal the targetingKey and the attributes of the context and avoid to expose the internal structure.

func (EvaluationContext) ToMap

func (u EvaluationContext) ToMap() map[string]any

ToMap returns a new map containing the context attributes plus the targetingKey. The returned map is a shallow copy: the top-level map is independent from the context, so adding or removing keys does not affect it, but nested reference values (maps, slices, pointers) are shared and mutating them will also mutate the context.

type EvaluationContextBuilder

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

EvaluationContextBuilder is a builder to create an EvaluationContext.

func NewEvaluationContextBuilder

func NewEvaluationContextBuilder(key string) *EvaluationContextBuilder

NewEvaluationContextBuilder constructs a new EvaluationContextBuilder, specifying the user targetingKey.

For authenticated users, the targetingKey may be a username or e-mail address. For anonymous users, this could be an IP address or session ID.

func (*EvaluationContextBuilder) AddCustom

func (u *EvaluationContextBuilder) AddCustom(
	key string,
	value any,
) *EvaluationContextBuilder

AddCustom allows you to add an attributes attribute to the EvaluationContext.

func (*EvaluationContextBuilder) Anonymous deprecated

func (u *EvaluationContextBuilder) Anonymous(anonymous bool) *EvaluationContextBuilder

Anonymous marks the evaluation context as anonymous and returns the builder.

Deprecated: Anonymous is to flag the context for an anonymous context or not. This function is here for compatibility reason, please consider using AddCustom("anonymous", true) instead of using this function.

func (*EvaluationContextBuilder) Build

Build is creating the EvaluationContext.

type GoffContextSpecifics

type GoffContextSpecifics struct {
	// CurrentDateTime is the current date time to use for the evaluation.
	CurrentDateTime *time.Time `json:"currentDateTime"`
	// ExporterMetadata is the metadata to be used by the exporter.
	ExporterMetadata map[string]any `json:"exporterMetadata"`
	// FlagList is the list of flags to evaluate in a bulk evaluation.
	FlagList []string `json:"flagList"`
	// UsedLegacyKey is true when the context carried these fields under the legacy attribute name
	// (LegacyReservedKey). The code that evaluates the flags reports it, once, through its own
	// legacy.Warner: extracting the fields has no owner to warn.
	UsedLegacyKey bool `json:"-"`
}

GoffContextSpecifics holds the context fields specific to GO Feature Flag.

Jump to

Keyboard shortcuts

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