flags

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package flags is the domain of what a project serves: flags with their definition and variations, their configuration in each environment, and the segments their rules target. Every rule spanning several of them, such as a variation not being removed while a configuration serves it, is a pure function over what the handler loaded under its project's row lock, so it holds for as long as that lock.

Index

Constants

View Source
const (
	ConditionAttribute    = eval.ConditionAttribute
	ConditionInSegment    = eval.ConditionInSegment
	ConditionNotInSegment = eval.ConditionNotInSegment
	ConditionPrerequisite = eval.ConditionPrerequisite
)

The condition kinds.

View Source
const (
	ServeVariation  = eval.ServeVariation
	ServeRollout    = eval.ServeRollout
	ServeExperiment = eval.ServeExperiment
)

The serve kinds.

View Source
const MaxUnitIDBytes = 256

MaxUnitIDBytes bounds a unit ID, in an override as in a context evaluated: SDKs hash it on every evaluation of a rollout.

Variables

View Source
var ErrNoRule = apperrors.NotFound("not_found", "the configuration has no rule with this identifier")

ErrNoRule answers a rule the configuration does not have.

Functions

func ErrInUse

func ErrInUse(refs []Reference) error

ErrInUse refuses a write that would leave refs naming nothing, listing them so the caller knows what to change first. A reference naming no one environment carries environment null, so a client tells "every environment" from an environment's key.

func KillReason

func KillReason(reason *string) (string, error)

KillReason returns the reason of a kill switch trimmed, "" for none, or invalid_configuration when it is over 2,000 characters.

Types

type Changed

type Changed struct {
	// Any is false for a change to the values the entity already has,
	// which is stored and recorded nowhere.
	Any bool
	// Ruleset reports a change to what a ruleset carries of the flag: its
	// variations' keys, values and order, or automatic exposure.
	Ruleset bool
	// RemovedVariations are the keys of the variations the change removes,
	// which nothing may still serve.
	RemovedVariations []string
	// Switched reports a change to a configuration whose only change to
	// what a ruleset carries is switching the flag on or off.
	Switched bool
}

Changed is what a change did.

type Condition

type Condition struct {
	Kind      string
	Attribute string
	Op        string
	// Value is the condition's value as encoding/json decodes it: a string,
	// a float64, a bool or a []any; nil when absent.
	Value     any
	Segment   string
	Flag      string
	Variation string
}

Condition is one comparison inside a rule or a segment: an attribute, an operator and a value; membership in a segment; or a prerequisite. It is what a ruleset carries, so SDKs evaluate exactly what was validated.

type Configuration

type Configuration struct {
	Flag        FlagID
	Environment workspace.EnvironmentID
	// On is false while the flag serves its off variation to everyone.
	On bool
	// OffVariation is the key of the variation served while the flag is
	// off, and when a prerequisite fails.
	OffVariation string
	// Fallthrough is the key of the variation served when no rule matches.
	Fallthrough string
	// Prerequisites are the variations other flags must serve for this one
	// to be evaluated at all.
	Prerequisites []Prerequisite
	Overrides     []Override
	// Rules are checked in order; the first whose conditions all hold
	// serves.
	Rules     []Rule
	UpdatedAt time.Time
	// UpdatedBy is the member whose write made the configuration what it
	// is; nil for a project token.
	UpdatedBy *access.MemberID
	// Version counts the configuration's accepted changes, from 1.
	Version int64
}

Configuration is what one flag serves in one environment. Every flag has one in every environment of its project, each changing on its own.

func InitialConfiguration

func InitialConfiguration(f Flag, environment workspace.EnvironmentID, by *access.MemberID, now time.Time) Configuration

InitialConfiguration returns the configuration f arrives with in environment, written by by at now: switched off, so creating a flag or an environment never changes what any application is served. A boolean flag serves false when off and true when on with no rule matching; another type its first variation either way.

func (Configuration) Copy

func (c Configuration) Copy(f Flag, source Configuration, overrides bool, r Referents, w Write) (Configuration, Changed, error)

Copy returns the configuration of f made by w what source, f's configuration in another environment, serves: its rules with their identifiers and salts, so the same units fall in every rollout in both environments, its prerequisites and its off and fallthrough variations. It stays on or off as it is, so a copy never switches a flag on by accident, and keeps its overrides unless overrides asks for source's, notes and authors with them. It is checked against r, the requirements of c's environment, as a replace is. A configuration is not copied from its own environment, which would change nothing on purpose.

func (Configuration) Kill

Kill returns the configuration switched off by w, serving its off variation to everyone, and what changed: switching off a configuration that is off changes nothing.

func (Configuration) RegenerateSalt

func (c Configuration) RegenerateSalt(rule RuleID, m Mint, w Write) (Configuration, Changed, error)

RegenerateSalt returns the configuration whose rule keyed rule serves its rollout with a new salt from m, set by w, and what changed: units are bucketed afresh, so some in the rollout fall out and others come in. not_found for a rule it does not have, not_a_rollout for a rule serving no rollout.

func (Configuration) RemoveOverride

func (c Configuration) RemoveOverride(unitType, unitID string, w Write) (Configuration, Changed, error)

RemoveOverride returns the configuration without the override of the unit of unitType with unitID, removed by w, and what changed; not_found when it has none.

func (Configuration) Replace

Replace returns the configuration of f replaced by in, checked against r, its version incremented when anything changed, and what changed; or invalid_configuration with every field to fix. A rule keeps its identifier and, while it stays a rollout, its salt; an override left as it was keeps its author.

func (Configuration) Revert

func (c Configuration) Revert(f Flag, recorded Configuration, r Referents, w Write) (Configuration, Changed, error)

Revert returns the configuration of f made recorded again by w, the document an earlier version of it was, whole: rule identifiers and salts, so the same units fall in every rollout again, and override notes and authors as they were. It is checked against r as a replace is, since a variation, segment, declaration or flag it names may be gone since.

func (Configuration) SameRuleset

func (c Configuration) SameRuleset(d Configuration) bool

SameRuleset reports whether c and d carry the same ruleset: on or off, the off and fallthrough variations, the prerequisites, the overrides by unit type, unit ID and variation, and the rules with their identifiers, conditions and serves, salts included, each in order. Rule descriptions and override notes and authors stay on the server, and the version and who made it describe the write rather than what SDKs receive, so none of them counts. Whether a write changed what an environment's SDKs receive, and whether two environments differ for a flag, are judged by it alike.

func (Configuration) SetOverride

SetOverride returns the configuration of f with o set by w, checked against d, and what changed; or invalid_configuration with every field to fix. An override of the same unit is replaced in place, variation and note together, and is then w's; another is added, up to 200.

type ConfigurationInput

type ConfigurationInput struct {
	On                        bool
	OffVariation, Fallthrough string
	Prerequisites             []Prerequisite
	Overrides                 []OverrideInput
	Rules                     []RuleInput
}

ConfigurationInput is a configuration as a write gives it whole.

type Declarations

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

Declarations are what a project's conditions may name: its attributes by type, and its unit types, each working as a string attribute holding that unit's ID.

func NewDeclarations

func NewDeclarations(units []workspace.UnitType, attributes []workspace.Attribute) Declarations

NewDeclarations returns the declarations of a project with units and attributes.

type Dependent

type Dependent struct {
	// Flag is the key of the requiring flag.
	Flag string
	// Environment is the key of the environment it requires it in.
	Environment string
	// Variation is the key of the variation it requires.
	Variation string
}

Dependent is a flag whose configuration in one environment requires a variation of another flag, as a prerequisite or in a rule's condition.

type EnvironmentConfiguration

type EnvironmentConfiguration struct {
	Environment   string
	Configuration Configuration
}

EnvironmentConfiguration is a flag's configuration with the key of its environment, by which a reference names it.

type Flag

type Flag struct {
	ID      FlagID
	Project workspace.ProjectID
	// Key names the flag in code and never changes; unique, case
	// sensitively, in its project.
	Key string
	// Type never changes either: code reads the flag's value as it.
	Type        Type
	Name        string
	Description string
	Owner       access.MemberID
	Tags        []string
	Lifetime    Lifetime
	// ExpectedRemovalAt is set for a temporary flag only.
	ExpectedRemovalAt *time.Time
	// AutomaticExposure makes SDKs record an exposure after every evaluation
	// whose result carries an experiment.
	AutomaticExposure bool
	Variations        []Variation
	// ArchivedAt is set while the flag is archived: hidden from SDKs and
	// kept until deleted.
	ArchivedAt *time.Time
	// CreatedBy is the member who created the flag; nil for a project token.
	CreatedBy *access.MemberID
	CreatedAt time.Time
	UpdatedAt time.Time
	// Version counts the definition's accepted changes, from 1.
	Version int64
}

Flag is a flag's definition: what it is in every environment at once.

func NewFlag

func NewFlag(in FlagInput) (Flag, error)

NewFlag returns the flag in, at version 1, or invalid_flag with every field to fix.

func (Flag) Archive

func (f Flag) Archive(now time.Time) (Flag, bool)

Archive returns the flag archived at now, hidden from SDKs in every environment, and whether it changed: archiving an archived flag does not.

func (Flag) CanConfigure

func (f Flag) CanConfigure() error

CanConfigure refuses to write a configuration of f while it is archived.

func (Flag) CanDelete

func (f Flag) CanDelete() error

CanDelete refuses to delete a flag that is not archived.

func (Flag) Change

func (f Flag) Change(ch FlagChange, now time.Time) (Flag, Changed, error)

Change returns the flag with ch applied at now, its version incremented when anything changed, and what changed. The owner is judged only when the change names a new one: ownership is never checked afterwards, so a member losing their grants never blocks a change to their flags.

func (Flag) Restore

func (f Flag) Restore(now time.Time) (Flag, bool)

Restore returns the flag brought back to SDKs at now, and whether it changed: restoring a flag that is not archived does not.

type FlagChange

type FlagChange struct {
	// Key and Type are refused whatever their value: code reading the flag
	// relies on both.
	Key               *string
	Type              *Type
	Name              *string
	Description       *string
	Owner             *Owner
	Tags              *[]string
	Lifetime          *Lifetime
	ExpectedRemovalAt *time.Time
	AutomaticExposure *bool
	Variations        *[]VariationInput
}

FlagChange is a change to a flag's definition; a nil field is left as it is.

type FlagID

type FlagID = id.Of[Flag]

FlagID identifies a flag.

type FlagInput

type FlagInput struct {
	ID          FlagID
	Project     workspace.ProjectID
	Key         string
	Type        Type
	Name        string
	Description string
	Owner       Owner
	Tags        []string
	// Lifetime is temporary when empty.
	Lifetime          Lifetime
	ExpectedRemovalAt *time.Time
	AutomaticExposure bool
	Variations        *[]VariationInput
	CreatedBy         *access.MemberID
	Now               time.Time
}

FlagInput is what a flag is created with. A nil Variations gives a boolean flag its two; every other type needs them listed.

type Lifetime

type Lifetime string

Lifetime says whether a flag is meant to be removed one day.

const (
	// Permanent flags stay, such as an operational switch.
	Permanent Lifetime = "permanent"
	// Temporary flags are removed once their feature is released, by an
	// expected date after which they are reported.
	Temporary Lifetime = "temporary"
)

The lifetimes.

type Mint

type Mint struct {
	RuleID func() RuleID
	Salt   func() string
}

Mint draws what a write gives what it creates: a new rule's identifier, and a new rollout's salt. The handler supplies both, since drawing either is not a rule of the domain.

type Override

type Override struct {
	UnitType, UnitID, Variation string
	// Note says why, "" for nothing. It and AddedBy stay on the server: no
	// ruleset carries either.
	Note string
	// AddedBy is the member who last wrote the override; nil for a project
	// token.
	AddedBy *access.MemberID
}

Override pins a variation for one unit.

type OverrideInput

type OverrideInput struct {
	UnitType, UnitID, Variation string
	Note                        *string
}

OverrideInput is an override as a write gives it: a note that may be absent, and no author, who is the writer.

type Owner

type Owner struct {
	Member   access.MemberID
	Eligible bool
}

Owner is the member a write names as a flag's owner, and whether they may own one: an active member holding a grant in the project, or an instance admin. The handler finds that out; the flag only refuses one who may not.

type Prerequisite

type Prerequisite struct {
	Flag, Variation string
}

Prerequisite requires the flag keyed Flag to serve its variation keyed Variation to the same context.

type Reference

type Reference struct {
	// Kind is the kind of what names it, such as flag or segment.
	Kind string
	// Key is its key.
	Key string
	// Environment is the key of the environment it names it in, "" when it
	// names it in every environment, as a segment does.
	Environment string
}

Reference is something that names an entity a write would remove or hide, such as a flag's configuration in one environment serving a variation.

func DependentReferences

func DependentReferences(dependents []Dependent) []Reference

DependentReferences returns the flags in dependents, one reference per flag and environment in the order of dependents: what archiving the flag they require would leave requiring a flag no SDK receives.

func VariationReferences

func VariationReferences(f Flag, removed []string, configs []EnvironmentConfiguration, dependents []Dependent) []Reference

VariationReferences returns what still names one of the variations of f in removed: f's configurations in configs naming it, one reference per environment in the order of configs, then the flags in dependents requiring it, one per flag and environment in the order of dependents.

type Referents

type Referents struct {
	Flags        []Flag
	Segments     []string
	Declarations Declarations
	Requirements Requirements
}

Referents are what a configuration may name, as the handler loaded them under its project's row lock: every flag of the project, archived ones and the configured one included, the keys of its segments, its declarations, and the requirements between flags in the configuration's environment.

type Requirements

type Requirements map[string][]string

Requirements are, by flag key, the keys of the flags each flag's configuration in one environment requires, through its prerequisites and its rules' prerequisite conditions, archived flags included: an archived flag can be restored, and its chains with it.

type Rule

type Rule struct {
	ID RuleID
	// Description stays on the server: no ruleset carries it.
	Description string
	Conditions  []Condition
	Serve       Serve
}

Rule is one ordered set of conditions, all of which a context meets for the rule to serve it.

type RuleID

type RuleID = id.Of[Rule]

RuleID identifies a rule across the versions of its configuration.

type RuleInput

type RuleInput struct {
	ID          *RuleID
	Description string
	Conditions  []Condition
	Serve       ServeInput
}

RuleInput is a rule as a write gives it: with the identifier of the rule of the current configuration it is, nil for a new rule.

type Segment

type Segment struct {
	ID      SegmentID
	Project workspace.ProjectID
	// Key names the segment in rules and never changes.
	Key         string
	Name        string
	Description string
	// Private keeps the segment's conditions on the server: only
	// server-side evaluation may use it.
	Private    bool
	Conditions []Condition
	CreatedAt  time.Time
	UpdatedAt  time.Time
	// Version counts the segment's accepted changes, from 1.
	Version int64
}

Segment is a reusable, named set of attribute conditions, all of which a context meets to be in it.

func NewSegment

func NewSegment(in SegmentInput) (Segment, error)

NewSegment returns the segment in, at version 1, or invalid_segment with every field to fix.

func (Segment) Change

func (s Segment) Change(ch SegmentChange, d Declarations, now time.Time) (Segment, Changed, error)

Change returns the segment with ch applied at now, its conditions checked against d, its version incremented when anything changed, and what changed: a change to its conditions or to private changes what every ruleset carries of it.

type SegmentChange

type SegmentChange struct {
	// Key is refused whatever its value: rules name the segment by it.
	Key         *string
	Name        *string
	Description *string
	Private     *bool
	Conditions  *[]Condition
}

SegmentChange is a change to a segment; a nil field is left as it is.

type SegmentID

type SegmentID = id.Of[Segment]

SegmentID identifies a segment.

type SegmentInput

type SegmentInput struct {
	ID                SegmentID
	Project           workspace.ProjectID
	Key               string
	Name, Description string
	Private           bool
	Conditions        []Condition
	Declarations      Declarations
	Now               time.Time
}

SegmentInput is what a segment is created with, and the declarations of its project its conditions are checked against.

type Serve

type Serve struct {
	Kind      string
	Variation string
	// UnitType, Percentage and Salt are a rollout's: the unit type whose
	// units are bucketed, the share served in basis points, and the salt
	// mixed into each unit's bucket.
	UnitType   string
	Percentage int
	Salt       string
}

Serve is what a rule serves: a variation, or a rollout of one variation to a share of one unit type's units, the others continuing to the next rule.

type ServeInput

type ServeInput struct {
	Kind, Variation, UnitType string
	Percentage                *int
	Salt                      string
}

ServeInput is a serve as a write gives it. Percentage is nil when absent; Salt is "" when absent, since only the server mints one.

type Type

type Type string

Type is the kind of value a flag serves.

const (
	TypeBoolean Type = "boolean"
	TypeString  Type = "string"
	TypeNumber  Type = "number"
	TypeJSON    Type = "json"
)

The flag types.

func Types

func Types() []Type

Types returns every flag type.

type Variation

type Variation struct {
	Key string
	// Value is compact JSON of the flag's type.
	Value       json.RawMessage
	Name        string
	Description string
}

Variation is one of the values a flag can serve.

type VariationInput

type VariationInput struct {
	Key               string
	Value             json.RawMessage
	Name, Description *string
}

VariationInput is a variation as a write gives it: a value as JSON, nil when absent, and a name and description that may be absent.

type Write

type Write struct {
	// By is the member writing; nil for a project token.
	By  *access.MemberID
	Now time.Time
}

Write is who makes a write to a configuration, and when.

Jump to

Keyboard shortcuts

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