{"modulePath":"github.com/GoCodeAlone/workflow","version":"v0.87.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package featureflag\n\n## Types\n\n```go\ntype EvaluationContext struct {\n\tUserKey    string            `json:\"user_key\"`\n\tAttributes map[string]string `json:\"attributes,omitempty\"`\n}\n```\nEvaluationContext holds the contextual data used to evaluate a feature flag. The UserKey uniquely identifies the subject (user, service, etc.). Attributes carry additional targeting information such as email, groups, plan, etc.\n\n```go\ntype FlagCache struct {\n\t// contains filtered or unexported fields\n}\n```\nFlagCache is a thread-safe in-memory TTL cache for evaluated flag values. A TTL of zero disables caching (every Get returns a miss).\n\n```go\nfunc NewFlagCache(ttl time.Duration) *FlagCache\n```\nNewFlagCache creates a cache with the given TTL. Pass 0 to disable caching.\n\n```go\nfunc (c *FlagCache) Flush()\n```\nFlush removes all entries.\n\n```go\nfunc (c *FlagCache) Get(flagKey, userKey string) (FlagValue, bool)\n```\nGet returns the cached value and true if a non-expired entry exists.\n\n```go\nfunc (c *FlagCache) Invalidate(flagKey, userKey string)\n```\nInvalidate removes a single entry from the cache.\n\n```go\nfunc (c *FlagCache) InvalidateFlag(flagKey string)\n```\nInvalidateFlag removes all entries for the given flag key.\n\n```go\nfunc (c *FlagCache) Len() int\n```\nLen returns the number of entries (including expired ones not yet evicted).\n\n```go\nfunc (c *FlagCache) Set(flagKey, userKey string, val FlagValue)\n```\nSet stores a flag value in the cache. No-op when TTL is zero.\n\n```go\ntype FlagChangeEvent struct {\n\tKey    string   `json:\"key\"`\n\tValue  any      `json:\"value\"`\n\tType   FlagType `json:\"type\"`\n\tSource string   `json:\"source\"`\n}\n```\nFlagChangeEvent is emitted when a flag value changes within a provider.\n\n```go\ntype FlagMeta struct {\n\tKey         string    `json:\"key\"`\n\tType        FlagType  `json:\"type\"`\n\tDescription string    `json:\"description,omitempty\"`\n\tEnabled     bool      `json:\"enabled\"`\n\tTags        []string  `json:\"tags,omitempty\"`\n\tCreatedAt   time.Time `json:\"created_at\"`\n\tUpdatedAt   time.Time `json:\"updated_at\"`\n}\n```\nFlagMeta describes a flag definition without evaluating it.\n\n```go\ntype FlagType string\n```\nFlagType enumerates the supported feature flag value types.\n\n```go\nconst (\n\tFlagTypeBoolean FlagType = \"boolean\"\n\tFlagTypeString  FlagType = \"string\"\n\tFlagTypeInteger FlagType = \"integer\"\n\tFlagTypeFloat   FlagType = \"float\"\n\tFlagTypeJSON    FlagType = \"json\"\n)\n```\n\n```go\ntype FlagValue struct {\n\tKey      string          `json:\"key\"`\n\tValue    any             `json:\"value\"`\n\tType     FlagType        `json:\"type\"`\n\tSource   string          `json:\"source\"`             // provider name that produced the value\n\tReason   string          `json:\"reason,omitempty\"`   // why this value was selected\n\tMetadata json.RawMessage `json:\"metadata,omitempty\"` // provider-specific extra data\n}\n```\nFlagValue represents the evaluated result of a single feature flag.\n\n```go\ntype Provider interface {\n\t// Name returns a unique identifier for this provider (e.g. \"generic\", \"launchdarkly\").\n\tName() string\n\n\t// Evaluate returns the resolved flag value for the given key and context.\n\t// Returns an error if the flag does not exist or evaluation fails.\n\tEvaluate(ctx context.Context, key string, evalCtx EvaluationContext) (FlagValue, error)\n\n\t// AllFlags returns the evaluated values for every flag visible to the given context.\n\tAllFlags(ctx context.Context, evalCtx EvaluationContext) ([]FlagValue, error)\n\n\t// Subscribe registers a callback that is invoked whenever a flag changes.\n\t// The returned function cancels the subscription.\n\tSubscribe(fn func(FlagChangeEvent)) (cancel func())\n}\n```\nProvider is the interface that all feature-flag backends must implement.\n\n```go\ntype Service struct {\n\t// contains filtered or unexported fields\n}\n```\nService is a caching proxy that sits between consumers and a Provider. It adds an in-memory cache, SSE broadcasting, and audit logging.\n\n```go\nfunc NewService(provider Provider, cache *FlagCache, logger *slog.Logger) *Service\n```\nNewService creates a Service wrapping the given provider and cache.\n\n```go\nfunc (s *Service) AllFlags(ctx context.Context, evalCtx EvaluationContext) ([]FlagValue, error)\n```\nAllFlags delegates to the provider (cache is per-key, so we don't cache AllFlags).\n\n```go\nfunc (s *Service) Evaluate(ctx context.Context, key string, evalCtx EvaluationContext) (FlagValue, error)\n```\nEvaluate returns the flag value for the given key. Cache is checked first; on a miss the provider is queried and the result is cached.\n\n```go\nfunc (s *Service) SSEHandler() http.HandlerFunc\n```\nSSEHandler returns an HTTP handler that streams flag change events to clients. Events are formatted as:\n\n\tevent: flag.updated\n\tdata: {\"key\":\"my-flag\",\"value\":true,\"type\":\"boolean\",\"source\":\"generic\"}\n\n```go\nfunc (s *Service) SubscriberCount() int\n```\nSubscriberCount returns the number of active SSE subscribers.\n\n","path":"github.com/GoCodeAlone/workflow/featureflag","name":"featureflag","synopsis":"","isRedistributable":true}
