feature

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package feature provides a tiny, allocation-light feature-flag / rollout evaluator that piggybacks on FastConf's strongly-typed configuration. Rules live in the same YAML / overlay tree as the rest of the config; the runtime evaluator (Rule.Evaluate) is a pure function over a map[string]string context, so it composes naturally with FastConf's lock-free read path.

The design borrows the shape used by ConfigCat / LaunchDarkly / Unleash / OpenFeature: a default value, an ordered list of targeted overrides (first match wins), and an optional percentage rollout keyed on a context attribute. Unlike those SDKs, feature does not require a separate service — every rule is just a value in your config tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(rules map[string]Rule, key string, ctx EvalContext, def any) any

Eval is the convenience entry point for evaluating a named rule from a rule table. Returns def when key is missing.

Types

type EvalContext

type EvalContext map[string]string

EvalContext is the per-request bag of attributes used by Rule.Evaluate to pick targeted overrides or compute rollout buckets. Keep keys short and stable across services (e.g. "user.id", "region", "tier").

type Rollout

type Rollout struct {
	Percent int    `json:"percent" yaml:"percent"`
	HashKey string `json:"hashKey" yaml:"hashKey"`
	Value   any    `json:"value" yaml:"value"`
}

Rollout deterministically buckets a context attribute into a 0-99 space. When HashKey is missing from ctx the rollout is skipped (it cannot decide deterministically without an anchor).

type Rule

type Rule struct {
	// Key is the dotted name of this rule (e.g. "features.darkMode").
	// It is informational — Evaluate does not consult Key.
	Key string `json:"key,omitempty" yaml:"key,omitempty"`
	// Default is the value returned when no Target / Rollout matches.
	Default any `json:"default" yaml:"default"`
	// Targets are deterministic equality matches evaluated in order.
	// The first Target whose When clauses all match wins.
	Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
	// Rollouts evaluate in order after Targets. A request lands in a
	// rollout bucket when HashKey is present in ctx and its hash
	// modulo 100 falls below Percent.
	Rollouts []Rollout `json:"rollouts,omitempty" yaml:"rollouts,omitempty"`
}

Rule is one feature flag entry. Unmarshal it from YAML/JSON via the usual codec round-trip:

features:
  darkMode:
    default: false
    targets:
      - when: { region: "eu-west" }
        value: true
    rollouts:
      - percent: 30
        hashKey: "user.id"
        value: true

func (Rule) Evaluate

func (r Rule) Evaluate(ctx EvalContext) any

Evaluate returns Value for the first matching Target or Rollout, or Default when nothing matches. Evaluation is pure and deterministic for the same (Rule, ctx) pair.

type Target

type Target struct {
	When  map[string]string `json:"when" yaml:"when"`
	Value any               `json:"value" yaml:"value"`
}

Target matches when every key/value pair in When equals the corresponding value in the evaluation context.

Jump to

Keyboard shortcuts

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