flags

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 14 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 flags is the Go client for the Hanzo native flags engine — cloud's /v1/flags (stateless Rust evaluator, per-org/project SQLite definitions, PostHog-compatible semantics).

c := flags.New("https://api.hanzo.ai", flags.WithToken(os.Getenv("HANZO_API_KEY")))
res, _ := c.Evaluate(ctx, flags.Context{DistinctID: "user-42",
	PersonProperties: map[string]any{"plan": "pro"}})
if res.IsEnabled("new-nav") { ... }
switch res.Variant("checkout-exp") { case "treatment": ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvaluateJSON

func EvaluateJSON(defsJSON, ctxJSON []byte) ([]byte, error)

EvaluateJSON is the wire-shaped entry point: a definitions array and an evaluation context, both JSON, in — the PostHog-shaped response out. Numbers keep their literal form through the decode so property comparisons see exactly what the caller sent.

Types

type Client

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

Client evaluates flags over POST {host}/v1/flags.

func New

func New(host string, opts ...Option) *Client

func (*Client) Evaluate

func (c *Client) Evaluate(ctx context.Context, ec Context) (Result, error)

Evaluate runs the caller's flags for one identity.

type ConditionGroup

type ConditionGroup struct {
	Properties []PropertyFilter `json:"properties,omitempty"`
	// RolloutPercentage nil means 100%.
	RolloutPercentage *float64 `json:"rollout_percentage,omitempty"`
	// Variant pins a specific variant when this group matches (it must exist in
	// Multivariate to take effect).
	Variant *string `json:"variant,omitempty"`
}

ConditionGroup is one release condition: property filters plus a rollout.

type Context

type Context struct {
	DistinctID       string              `json:"distinct_id"`
	PersonProperties map[string]any      `json:"person_properties,omitempty"`
	Groups           map[string]GroupCtx `json:"groups,omitempty"` // group_type_index -> identity
}

Context is one evaluation identity.

type Def

type Def struct {
	Key     string  `json:"key"`
	Active  bool    `json:"active"`
	Deleted bool    `json:"deleted"`
	Filters Filters `json:"filters"`
	// AggregationGroupTypeIndex, when set, aggregates the flag by a group type
	// (its index) instead of by the person.
	AggregationGroupTypeIndex *int `json:"aggregation_group_type_index,omitempty"`
}

Def is one flag definition.

func (*Def) UnmarshalJSON

func (d *Def) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes a definition, defaulting `active` to true when the key is absent — the wire contract a stored definition relies on.

type Filters

type Filters struct {
	Groups       []ConditionGroup `json:"groups,omitempty"`
	Multivariate *Multivariate    `json:"multivariate,omitempty"`
	// Payloads maps "true" -> the payload for a boolean match, or a variant key
	// -> that variant's payload.
	Payloads map[string]json.RawMessage `json:"payloads,omitempty"`
}

type GroupCtx

type GroupCtx struct {
	Key        string         `json:"key"`
	Properties map[string]any `json:"properties,omitempty"`
}

type Multivariate

type Multivariate struct {
	Variants []Variant `json:"variants,omitempty"`
}

type Operator

type Operator string

Operator is the comparison a property filter applies.

const (
	OpExact           Operator = "exact"
	OpIsNot           Operator = "is_not"
	OpIcontains       Operator = "icontains"
	OpNotIcontains    Operator = "not_icontains"
	OpRegex           Operator = "regex"
	OpNotRegex        Operator = "not_regex"
	OpGt              Operator = "gt"
	OpLt              Operator = "lt"
	OpGte             Operator = "gte"
	OpLte             Operator = "lte"
	OpSemverGt        Operator = "semver_gt"
	OpSemverGte       Operator = "semver_gte"
	OpSemverLt        Operator = "semver_lt"
	OpSemverLte       Operator = "semver_lte"
	OpSemverEq        Operator = "semver_eq"
	OpSemverNeq       Operator = "semver_neq"
	OpSemverTilde     Operator = "semver_tilde"
	OpSemverCaret     Operator = "semver_caret"
	OpSemverWildcard  Operator = "semver_wildcard"
	OpIsSet           Operator = "is_set"
	OpIsNotSet        Operator = "is_not_set"
	OpIsDateExact     Operator = "is_date_exact"
	OpIsDateAfter     Operator = "is_date_after"
	OpIsDateBefore    Operator = "is_date_before"
	OpIn              Operator = "in"
	OpNotIn           Operator = "not_in"
	OpFlagEvaluatesTo Operator = "flag_evaluates_to"
)

func (*Operator) UnmarshalJSON

func (o *Operator) UnmarshalJSON(b []byte) error

UnmarshalJSON rejects unknown operators so a definition carrying one fails to parse rather than silently never matching.

type Option

type Option func(*Client)

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient overrides the HTTP client (default 5s timeout).

func WithProject

func WithProject(p string) Option

WithProject scopes evaluation to a project (X-Project-Id).

func WithToken

func WithToken(t string) Option

WithToken sets the bearer token (server-side callers).

type PropertyFilter

type PropertyFilter struct {
	Key            string          `json:"key"`
	Value          json.RawMessage `json:"value,omitempty"`
	Operator       *Operator       `json:"operator,omitempty"`
	Type           PropertyType    `json:"type"`
	Negation       *bool           `json:"negation,omitempty"`
	GroupTypeIndex *int            `json:"group_type_index,omitempty"`
}

PropertyFilter is one property condition inside a condition group. Value is absent (nil) for the is_set / is_not_set operators, and for filters the API built without one.

type PropertyType

type PropertyType string

PropertyType is which bag a property filter reads from.

const (
	TypePerson PropertyType = "person"
	TypeCohort PropertyType = "cohort"
	TypeGroup  PropertyType = "group"
	// TypeFlag compares against another flag's evaluation result.
	TypeFlag PropertyType = "flag"
)

func (*PropertyType) UnmarshalJSON

func (p *PropertyType) UnmarshalJSON(b []byte) error

UnmarshalJSON rejects unknown property types for the same reason as Operator.

type Result

type Result struct {
	FeatureFlags              map[string]json.RawMessage `json:"featureFlags"`
	FeatureFlagPayloads       map[string]json.RawMessage `json:"featureFlagPayloads"`
	ErrorsWhileComputingFlags bool                       `json:"errorsWhileComputingFlags"`
}

Result is the PostHog-shaped evaluation response.

func Evaluate

func Evaluate(defs []Def, ctx Context) Result

Evaluate runs a set of flag definitions for one identity.

func (Result) IsEnabled

func (r Result) IsEnabled(key string) bool

IsEnabled is true for boolean true or any variant string.

func (Result) Payload

func (r Result) Payload(key string, v any) bool

Payload unmarshals the flag's payload into v; false when absent.

func (Result) Variant

func (r Result) Variant(key string) string

Variant returns the variant key for multivariate flags, or "".

type Variant

type Variant struct {
	Key               string  `json:"key"`
	RolloutPercentage float64 `json:"rollout_percentage"`
}

Jump to

Keyboard shortcuts

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