featureflag

package
v0.0.4-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package featureflag is a thin wrapper around github.com/thomaspoignant/go-feature-flag (GOFF) that gives hex applications feature-flagging with rule-based targeting, percentage rollouts, and typed variations.

See ADR-0013 for the wrap decision. Design summary:

  • Type aliases (Client, Context, EvaluationContext) so consumers can call every GOFF method through the alias.
  • hex-owned constructors for the two common shapes: a file on disk and a file in an embed.FS. Advanced retrievers (HTTP, S3, K8s) are supplied by importing GOFF's retriever subpackage directly and passing it via Options.Retrievers.
  • Package-level convenience (SetDefault + Bool/Int/String/Float64/JSON) mirroring hex/config and hex/i18n.

Example (embed.FS):

//go:embed flags.yaml
var flagsFS embed.FS

client, err := featureflag.NewFromFS(flagsFS, "flags.yaml", featureflag.Options{
    PollingInterval: 30 * time.Second,
})
if err != nil { return err }
defer client.Close()

featureflag.SetDefault(client)

ctx := featureflag.NewContext("user-42").AddCustom("beta", "true")
if featureflag.Bool("new-checkout", ctx, false) {
    // ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(flag string, ctx Context, defaultValue bool) bool

Bool evaluates flag as a boolean. Returns defaultValue if the flag is missing, the default client is unset, or evaluation fails.

func Float64

func Float64(flag string, ctx Context, defaultValue float64) float64

Float64 evaluates flag as a float64.

func Int

func Int(flag string, ctx Context, defaultValue int) int

Int evaluates flag as an int.

func JSON

func JSON(flag string, ctx Context, defaultValue map[string]any) map[string]any

JSON evaluates flag as a JSON object.

func JSONArray

func JSONArray(flag string, ctx Context, defaultValue []any) []any

JSONArray evaluates flag as a JSON array.

func SetDefault

func SetDefault(c *Client)

SetDefault installs c as the package-level default client. Subsequent calls to Bool/Int/String/Float64/JSON delegate to c. Safe to call more than once.

func String

func String(flag string, ctx Context, defaultValue string) string

String evaluates flag as a string.

Types

type Client

type Client = ffclient.GoFeatureFlag

Client is the type alias for GOFF's evaluation client. Consumers get the full upstream API through this alias (BoolVariation, IntVariation, AllFlagsState, RawVariation, etc.).

func Default

func Default() *Client

Default returns the current default Client, or nil if none is set.

func New

func New(cfg ffclient.Config) (*Client, error)

New builds a Client from a full ffclient.Config. Use this when the upstream Config surface has knobs the hex helpers do not expose.

func NewFromFS

func NewFromFS(fsys fs.FS, path string, opts Options) (*Client, error)

NewFromFS builds a Client backed by a flag file in an fs.FS (typically an //go:embed FS). Flags are read once at start and are immutable at runtime — polling is disabled because embed.FS content cannot change.

func NewFromFile

func NewFromFile(path string, opts Options) (*Client, error)

NewFromFile builds a Client backed by a flag file on disk.

type Context

type Context = ffcontext.Context

Context is the type alias for GOFF's evaluation context interface.

type ContextBuilder

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

ContextBuilder is a fluent-chain wrapper around EvaluationContext.

func ContextWith

func ContextWith(ec EvaluationContext) *ContextBuilder

ContextWith wraps an EvaluationContext with a fluent builder that makes attribute chains readable at call sites:

ctx := featureflag.ContextWith(featureflag.NewContext("u1")).
	Set("beta", "true").
	Set("region", "us").
	Context()

The underlying upstream API uses void mutators (AddCustomAttribute); this helper preserves the standard EvaluationContext type for hand-off while giving callers a fluent construction path.

func (*ContextBuilder) Context

func (b *ContextBuilder) Context() EvaluationContext

Context returns the underlying EvaluationContext ready to hand to a Variation call.

func (*ContextBuilder) Set

func (b *ContextBuilder) Set(name string, value any) *ContextBuilder

Set attaches a custom attribute for targeting rules and returns the builder for chaining.

type EvaluationContext

type EvaluationContext = ffcontext.EvaluationContext

EvaluationContext is the concrete builder type consumers use to attach user attributes for targeting rules.

func NewAnonymousContext

func NewAnonymousContext(key string) EvaluationContext

NewAnonymousContext returns an anonymous EvaluationContext for unauthenticated / pre-identification traffic.

func NewContext

func NewContext(userKey string) EvaluationContext

NewContext returns an EvaluationContext keyed by userKey. Wrap in ContextWith to attach attributes with a fluent chain.

type Notifier

type Notifier = notifier.Notifier

Notifier is the type alias for GOFF's Notifier interface for flag-change hooks (webhook, log, custom).

type Options

type Options struct {
	// PollingInterval is how often retrievers re-fetch the flag file.
	// Zero uses GOFF's default (60s). Values below the GOFF minimum are
	// clamped upstream.
	PollingInterval time.Duration

	// StartWithRetrieverError, when true, lets the client start even if
	// the initial retrieve fails. Useful when the flag source is
	// intermittent and you want to fall back on defaults rather than
	// bail out at startup.
	StartWithRetrieverError bool

	// Retrievers lets consumers supply additional retrievers alongside
	// the primary one built by New*/NewFromFile/NewFromFS. Later
	// retrievers take precedence over earlier ones in GOFF's merge logic.
	Retrievers []Retriever

	// Notifiers registers flag-change listeners.
	Notifiers []Notifier

	// FileFormat overrides retriever content-type detection. Supported:
	// "yaml", "json", "toml". Empty means auto-detect from extension.
	FileFormat string
}

Options tune a Client. Only the knobs hex applications typically touch are surfaced; consumers who need advanced upstream options can construct ffclient.Config directly and call ffclient.New.

type Retriever

type Retriever = retriever.Retriever

Retriever is the type alias for GOFF's Retriever interface. Consumers who want an unusual retriever (HTTP, S3, K8s, Postgres, etc.) can import GOFF's retriever subpackage and pass an instance directly.

Directories

Path Synopsis
Package provider is the default hex/featureflag service provider.
Package provider is the default hex/featureflag service provider.

Jump to

Keyboard shortcuts

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