Documentation
¶
Overview ¶
Package zenmanage is the Zenmanage feature-flag SDK for Go server applications. It evaluates flags locally against cached rules fetched from the Zenmanage API, supporting context-based targeting, percentage rollouts, and usage reporting.
See the Zenmanage type for the main entry point, and the middleware sub-package for framework integrations.
Index ¶
- Constants
- func CRC32B(input string) uint32
- func IsInBucket(salt, contextIdentifier string, percentage int) bool
- type APIClient
- type Attribute
- type Cache
- type Config
- type ConfigBuilder
- func (b *ConfigBuilder) Build() (Config, error)
- func (b *ConfigBuilder) WithAPIEndpoint(endpoint string) *ConfigBuilder
- func (b *ConfigBuilder) WithCache(cache Cache) *ConfigBuilder
- func (b *ConfigBuilder) WithCacheBackend(backend string) *ConfigBuilder
- func (b *ConfigBuilder) WithCacheDirectory(dir string) *ConfigBuilder
- func (b *ConfigBuilder) WithCacheTTL(ttl time.Duration) *ConfigBuilder
- func (b *ConfigBuilder) WithClientAgent(agent string) *ConfigBuilder
- func (b *ConfigBuilder) WithEnvironmentToken(token string) *ConfigBuilder
- func (b *ConfigBuilder) WithHTTPClient(client *http.Client) *ConfigBuilder
- func (b *ConfigBuilder) WithLogger(logger Logger) *ConfigBuilder
- func (b *ConfigBuilder) WithSDKVersion(version string) *ConfigBuilder
- func (b *ConfigBuilder) WithUsageReporting(enabled bool) *ConfigBuilder
- type ConfigurationError
- type Context
- func (c *Context) AddAttribute(attr Attribute)
- func (c Context) Attributes() []ContextAttribute
- func (c Context) Data() ContextData
- func (c Context) GetAttribute(key string) (ContextAttribute, bool)
- func (c Context) Identifier() string
- func (c Context) IsEmpty() bool
- func (c Context) JSON() ([]byte, error)
- func (c Context) Name() string
- func (c Context) Type() string
- type ContextAttribute
- type ContextData
- type ContextValue
- type DefaultsCollection
- func (c *DefaultsCollection) All() map[string]any
- func (c *DefaultsCollection) Clear()
- func (c *DefaultsCollection) Delete(key string)
- func (c *DefaultsCollection) Get(key string) (any, bool)
- func (c *DefaultsCollection) Has(key string) bool
- func (c *DefaultsCollection) Keys() []string
- func (c *DefaultsCollection) Set(key string, value any)
- func (c *DefaultsCollection) Size() int
- type Error
- type EvaluationError
- type FetchRulesError
- type FileSystemCache
- type Flag
- func (f Flag) AsBool() bool
- func (f Flag) AsNumber() float64
- func (f Flag) AsString() string
- func (f Flag) IsEnabled() bool
- func (f Flag) Key() string
- func (f Flag) Name() string
- func (f Flag) Rollout() *RolloutData
- func (f Flag) Rules() []Rule
- func (f Flag) Target() Target
- func (f Flag) Type() FlagType
- func (f Flag) Value() any
- func (f Flag) Version() string
- type FlagData
- type FlagManager
- func (m *FlagManager) All(ctx context.Context) ([]Flag, error)
- func (m *FlagManager) RefreshRules(ctx context.Context) error
- func (m *FlagManager) ReportUsage(ctx context.Context, key string, defaultValue any) error
- func (m *FlagManager) Single(ctx context.Context, key string, inlineDefault ...any) (Flag, error)
- func (m *FlagManager) WithContext(ctx Context) *FlagManager
- func (m *FlagManager) WithDefaults(defaults *DefaultsCollection) *FlagManager
- type FlagType
- type InMemoryCache
- type InvalidRulesError
- type Logger
- type NullCache
- type NullLogger
- type RolloutData
- type Rule
- type RuleCondition
- type RuleContextTarget
- type RuleEngine
- type RulesResponse
- type Target
- type ValueEnvelope
- type Zenmanage
- func (z *Zenmanage) Flags() *FlagManager
- func (z *Zenmanage) GetNumber(ctx context.Context, key, userID string, defaultValue float64) (float64, error)
- func (z *Zenmanage) GetString(ctx context.Context, key, userID, defaultValue string) (string, error)
- func (z *Zenmanage) IsEnabled(ctx context.Context, key, userID string) (bool, error)
Constants ¶
const Version = "1.0.0"
Version is the SDK semantic version.
Variables ¶
This section is empty.
Functions ¶
func IsInBucket ¶
IsInBucket computes deterministic rollout inclusion for percentage rollouts.
Types ¶
type APIClient ¶
type APIClient struct {
// contains filtered or unexported fields
}
APIClient handles network communication with Zenmanage APIs.
func (*APIClient) FetchRules ¶
func (c *APIClient) FetchRules(ctx context.Context) (RulesResponse, error)
FetchRules fetches the full rules payload from API/CDN.
func (*APIClient) ReportUsage ¶
func (c *APIClient) ReportUsage(ctx context.Context, key string, contextData *Context, defaultValue any) error
ReportUsage emits usage telemetry for a flag key. defaultValue, when non-nil, is the fallback value the caller used because the flag couldn't be resolved from rules; it's sent as the X-ZEN-DEFAULT-VALUE header so it can be persisted and shown on the flag detail page.
type Attribute ¶
type Attribute struct {
// contains filtered or unexported fields
}
Attribute is a high-level context attribute helper.
func NewAttribute ¶
NewAttribute creates a context attribute.
type Cache ¶
type Cache interface {
Get(key string) (value string, found bool, err error)
Set(key, value string, ttl time.Duration) error
Delete(key string) error
Clear() error
}
Cache is the cache backend contract.
type Config ¶
type Config struct {
EnvironmentToken string
CacheTTL time.Duration
CacheBackend string
CacheDirectory string
EnableUsageReporting bool
APIEndpoint string
Logger Logger
CustomCache Cache
HTTPClient *http.Client
ClientAgent string
SDKVersion string
}
Config stores SDK configuration.
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
ConfigBuilder builds SDK Config instances.
func ConfigFromEnvironment ¶
func ConfigFromEnvironment() *ConfigBuilder
ConfigFromEnvironment creates a builder populated from env vars.
func NewConfigBuilder ¶
func NewConfigBuilder() *ConfigBuilder
NewConfigBuilder creates a builder with defaults.
func (*ConfigBuilder) Build ¶
func (b *ConfigBuilder) Build() (Config, error)
Build validates and returns config.
func (*ConfigBuilder) WithAPIEndpoint ¶
func (b *ConfigBuilder) WithAPIEndpoint(endpoint string) *ConfigBuilder
WithAPIEndpoint sets custom API endpoint.
func (*ConfigBuilder) WithCache ¶
func (b *ConfigBuilder) WithCache(cache Cache) *ConfigBuilder
WithCache sets custom cache implementation.
func (*ConfigBuilder) WithCacheBackend ¶
func (b *ConfigBuilder) WithCacheBackend(backend string) *ConfigBuilder
WithCacheBackend sets cache backend.
func (*ConfigBuilder) WithCacheDirectory ¶
func (b *ConfigBuilder) WithCacheDirectory(dir string) *ConfigBuilder
WithCacheDirectory sets filesystem cache directory.
func (*ConfigBuilder) WithCacheTTL ¶
func (b *ConfigBuilder) WithCacheTTL(ttl time.Duration) *ConfigBuilder
WithCacheTTL sets cache TTL.
func (*ConfigBuilder) WithClientAgent ¶
func (b *ConfigBuilder) WithClientAgent(agent string) *ConfigBuilder
WithClientAgent sets client agent name.
func (*ConfigBuilder) WithEnvironmentToken ¶
func (b *ConfigBuilder) WithEnvironmentToken(token string) *ConfigBuilder
WithEnvironmentToken sets the environment token.
func (*ConfigBuilder) WithHTTPClient ¶
func (b *ConfigBuilder) WithHTTPClient(client *http.Client) *ConfigBuilder
WithHTTPClient sets custom http client.
func (*ConfigBuilder) WithLogger ¶
func (b *ConfigBuilder) WithLogger(logger Logger) *ConfigBuilder
WithLogger sets logger.
func (*ConfigBuilder) WithSDKVersion ¶
func (b *ConfigBuilder) WithSDKVersion(version string) *ConfigBuilder
WithSDKVersion sets sdk version string.
func (*ConfigBuilder) WithUsageReporting ¶
func (b *ConfigBuilder) WithUsageReporting(enabled bool) *ConfigBuilder
WithUsageReporting toggles usage reporting.
type ConfigurationError ¶
type ConfigurationError struct {
Message string
// contains filtered or unexported fields
}
ConfigurationError indicates invalid SDK configuration.
func (*ConfigurationError) Error ¶
func (e *ConfigurationError) Error() string
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context captures the evaluation context.
func ContextFromData ¶
func ContextFromData(data ContextData) Context
ContextFromData creates context from already-structured data.
func NewContext ¶
NewContext creates a context.
func SingleContext ¶
SingleContext creates a simple context with optional name.
func (*Context) AddAttribute ¶
AddAttribute adds an attribute to the context.
func (Context) Attributes ¶
func (c Context) Attributes() []ContextAttribute
Attributes returns a copy of all attributes.
func (Context) GetAttribute ¶
func (c Context) GetAttribute(key string) (ContextAttribute, bool)
GetAttribute retrieves an attribute by key.
func (Context) Identifier ¶
Identifier returns the context identifier.
type ContextAttribute ¶
type ContextAttribute struct {
Key string `json:"key"`
Values []ContextValue `json:"values"`
}
ContextAttribute is an attribute used for context targeting.
type ContextData ¶
type ContextData struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Identifier string `json:"identifier,omitempty"`
Attributes []ContextAttribute `json:"attributes,omitempty"`
}
ContextData is the serialized context payload.
type ContextValue ¶
type ContextValue struct {
Value string `json:"value"`
}
ContextValue is a single context value.
type DefaultsCollection ¶
type DefaultsCollection struct {
// contains filtered or unexported fields
}
DefaultsCollection stores default values by flag key.
func DefaultsFromMap ¶
func DefaultsFromMap(values map[string]any) *DefaultsCollection
DefaultsFromMap creates defaults from a map.
func NewDefaultsCollection ¶
func NewDefaultsCollection() *DefaultsCollection
NewDefaultsCollection creates an empty defaults collection.
func (*DefaultsCollection) All ¶
func (c *DefaultsCollection) All() map[string]any
All returns a shallow copy.
func (*DefaultsCollection) Delete ¶
func (c *DefaultsCollection) Delete(key string)
Delete removes a key.
func (*DefaultsCollection) Get ¶
func (c *DefaultsCollection) Get(key string) (any, bool)
Get gets a default value.
func (*DefaultsCollection) Has ¶
func (c *DefaultsCollection) Has(key string) bool
Has checks if key exists.
func (*DefaultsCollection) Keys ¶
func (c *DefaultsCollection) Keys() []string
Keys returns all keys.
func (*DefaultsCollection) Set ¶
func (c *DefaultsCollection) Set(key string, value any)
Set sets a default value.
type Error ¶
type Error interface {
error
// contains filtered or unexported methods
}
Error is implemented by every error type this SDK returns, mirroring the shared ZenmanageError base class in the JavaScript, PHP, and Python SDKs. Use errors.As to distinguish SDK errors from other errors:
var zmErr zenmanage.Error
if errors.As(err, &zmErr) { ... }
type EvaluationError ¶
type EvaluationError struct {
Message string
// contains filtered or unexported fields
}
EvaluationError indicates a rule or flag evaluation failure.
func (*EvaluationError) Error ¶
func (e *EvaluationError) Error() string
type FetchRulesError ¶
type FetchRulesError struct {
Message string
StatusCode int
// contains filtered or unexported fields
}
FetchRulesError indicates a failure while loading rules from remote APIs.
func (*FetchRulesError) Error ¶
func (e *FetchRulesError) Error() string
type FileSystemCache ¶
type FileSystemCache struct {
// contains filtered or unexported fields
}
FileSystemCache stores cache entries on disk.
func NewFileSystemCache ¶
func NewFileSystemCache(dir string) *FileSystemCache
NewFileSystemCache creates a filesystem cache backend.
func (*FileSystemCache) Clear ¶
func (c *FileSystemCache) Clear() error
Clear removes all cache files in the cache directory.
func (*FileSystemCache) Delete ¶
func (c *FileSystemCache) Delete(key string) error
Delete removes a cache entry.
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag is an evaluated flag object with helper accessors.
func (Flag) Rollout ¶
func (f Flag) Rollout() *RolloutData
Rollout returns rollout metadata if present.
type FlagData ¶
type FlagData struct {
Version string `json:"version"`
Type FlagType `json:"type"`
Key string `json:"key"`
Name string `json:"name"`
Target Target `json:"target"`
Rules []Rule `json:"rules,omitempty"`
Rollout *RolloutData `json:"rollout,omitempty"`
}
FlagData is the API representation of a flag.
type FlagManager ¶
type FlagManager struct {
// contains filtered or unexported fields
}
FlagManager handles rule loading and flag evaluation.
func NewFlagManager ¶
func NewFlagManager(apiClient *APIClient, cache Cache, ruleEngine *RuleEngine, cacheTTL time.Duration, logger Logger) *FlagManager
NewFlagManager creates a flag manager.
func (*FlagManager) All ¶
func (m *FlagManager) All(ctx context.Context) ([]Flag, error)
All returns all evaluated flags.
func (*FlagManager) RefreshRules ¶
func (m *FlagManager) RefreshRules(ctx context.Context) error
RefreshRules forces rules refresh from API.
func (*FlagManager) ReportUsage ¶
ReportUsage manually reports flag usage to the API, using the manager's current context (if any). Single already reports usage automatically on every evaluation; call this directly only when usage needs to be recorded outside of a Single/All evaluation path, matching the explicit reportUsage method exposed by the JavaScript, PHP, and Python SDKs.
func (*FlagManager) WithContext ¶
func (m *FlagManager) WithContext(ctx Context) *FlagManager
WithContext returns a new flag manager that shares rules/cache with the receiver but uses a different context.
func (*FlagManager) WithDefaults ¶
func (m *FlagManager) WithDefaults(defaults *DefaultsCollection) *FlagManager
WithDefaults returns a new flag manager that shares rules/cache with the receiver but uses a different defaults collection.
type InMemoryCache ¶
type InMemoryCache struct {
// contains filtered or unexported fields
}
InMemoryCache is a process-local cache backend.
func NewInMemoryCache ¶
func NewInMemoryCache() *InMemoryCache
NewInMemoryCache creates an in-memory cache.
func (*InMemoryCache) Clear ¶
func (c *InMemoryCache) Clear() error
Clear removes all cache entries.
func (*InMemoryCache) Delete ¶
func (c *InMemoryCache) Delete(key string) error
Delete removes a cache entry.
type InvalidRulesError ¶
type InvalidRulesError struct {
Message string
// contains filtered or unexported fields
}
InvalidRulesError indicates malformed or semantically invalid rules payloads.
func (*InvalidRulesError) Error ¶
func (e *InvalidRulesError) Error() string
type Logger ¶
type Logger interface {
Debug(message string, meta map[string]any)
Info(message string, meta map[string]any)
Warn(message string, meta map[string]any)
Error(message string, meta map[string]any)
}
Logger is the SDK logger contract.
type NullLogger ¶
type NullLogger struct{}
NullLogger is a silent logger.
func (NullLogger) Debug ¶
func (NullLogger) Debug(string, map[string]any)
Debug discards the message.
type RolloutData ¶
type RolloutData struct {
Target Target `json:"target"`
Rules []Rule `json:"rules"`
Percentage int `json:"percentage"`
Salt string `json:"salt"`
Status string `json:"status"`
}
RolloutData configures percentage rollout behavior for a flag.
type Rule ¶
type Rule struct {
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
Criteria *RuleCondition `json:"criteria,omitempty"`
Clauses []RuleCondition `json:"clauses,omitempty"`
Position int `json:"position,omitempty"`
Value ValueEnvelope `json:"value"`
}
Rule holds rule criteria and the resulting target value.
type RuleCondition ¶
RuleCondition is a single clause for rule matching.
func (*RuleCondition) UnmarshalJSON ¶
func (rc *RuleCondition) UnmarshalJSON(data []byte) error
UnmarshalJSON maps both the legacy internal format (attribute/operator/value) and the CDN wire format (selector/selector_subtype/comparer/values) to the internal fields used by the rule engine.
type RuleContextTarget ¶
type RuleContextTarget struct {
Identifier string `json:"identifier"`
Type *string `json:"type,omitempty"`
}
RuleContextTarget is a typed context target used in context/segment clauses.
type RuleEngine ¶
type RuleEngine struct{}
RuleEngine evaluates flag rules against a context.
func (*RuleEngine) Evaluate ¶
func (e *RuleEngine) Evaluate(rules []Rule, context Context) (*ValueEnvelope, error)
Evaluate finds the first matching rule and returns its value envelope.
type RulesResponse ¶
RulesResponse is the top-level rule payload.
type Target ¶
type Target struct {
Version string `json:"version,omitempty"`
ExpiredAt string `json:"expired_at,omitempty"`
PublishedAt string `json:"published_at,omitempty"`
ScheduledAt string `json:"scheduled_at,omitempty"`
Value ValueEnvelope `json:"value"`
}
Target contains a value payload and metadata.
type ValueEnvelope ¶
type ValueEnvelope struct {
Version string `json:"version,omitempty"`
Value struct {
Boolean *bool `json:"boolean,omitempty"`
String *string `json:"string,omitempty"`
Number *float64 `json:"number,omitempty"`
} `json:"value"`
}
ValueEnvelope is the nested API value format.
type Zenmanage ¶
type Zenmanage struct {
// contains filtered or unexported fields
}
Zenmanage is the main SDK entry point.
func (*Zenmanage) Flags ¶
func (z *Zenmanage) Flags() *FlagManager
Flags returns the flag manager for advanced use (context, defaults, bulk evaluation).
func (*Zenmanage) GetNumber ¶
func (z *Zenmanage) GetNumber(ctx context.Context, key, userID string, defaultValue float64) (float64, error)
GetNumber returns the numeric value of a flag for the given user ID. defaultValue is returned when the flag is not found.
func (*Zenmanage) GetString ¶
func (z *Zenmanage) GetString(ctx context.Context, key, userID, defaultValue string) (string, error)
GetString returns the string value of a flag for the given user ID. defaultValue is returned when the flag is not found.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package examples contains runnable samples demonstrating the Zenmanage Go SDK, mirroring the sample set in the JavaScript and PHP SDKs.
|
Package examples contains runnable samples demonstrating the Zenmanage Go SDK, mirroring the sample set in the JavaScript and PHP SDKs. |
|
Package middleware provides net/http middleware for the Zenmanage Go SDK.
|
Package middleware provides net/http middleware for the Zenmanage Go SDK. |