Documentation
¶
Overview ¶
Package flaggr provides a Go client for the Flaggr feature flag platform (https://flaggr.dev).
It wraps the Flaggr REST API for flag evaluation with typed methods for boolean, string, number, and object flags.
Install it with:
go get github.com/flaggr-dev/flaggr-go@latest
Usage:
client, err := flaggr.NewClient(
flaggr.WithAPIKey(os.Getenv("FLAGGR_API_KEY")), // project API token (fgr_...)
flaggr.WithServiceID(os.Getenv("FLAGGR_SERVICE_ID")), // the service's ID, not its slug
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
enabled, err := client.Boolean(ctx, "checkout-v2", false, flaggr.EvalContext{
TargetingKey: "user-123",
})
Index ¶
- type CachedFlag
- type CachedOverride
- type Client
- func (c *Client) Boolean(ctx context.Context, flagKey string, defaultValue bool, evalCtx ...EvalContext) (bool, error)
- func (c *Client) Close()
- func (c *Client) InvalidateFlag(flagKey string)
- func (c *Client) LoadFlags(flags []CachedFlag)
- func (c *Client) Number(ctx context.Context, flagKey string, defaultValue float64, ...) (float64, error)
- func (c *Client) Object(ctx context.Context, flagKey string, defaultValue map[string]interface{}, ...) (map[string]interface{}, error)
- func (c *Client) Resolve(ctx context.Context, flagKey string, defaultValue interface{}, ...) (*EvaluationDetail, error)
- func (c *Client) SetFlag(flag CachedFlag)
- func (c *Client) String(ctx context.Context, flagKey string, defaultValue string, ...) (string, error)
- type EvalContext
- type EvaluationDetail
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedFlag ¶
type CachedFlag struct {
Key string
Enabled bool
DefaultValue interface{}
Overrides []CachedOverride
}
CachedFlag stores flag configuration for zero-network local in-process evaluation.
type CachedOverride ¶
type CachedOverride struct {
Identifiers []string
Value interface{}
}
CachedOverride stores user/identity overrides.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Flaggr feature flag evaluation client.
func NewClient ¶
NewClient creates a new Flaggr client with the given options.
Example ¶
package main
import (
"context"
"fmt"
"log"
"os"
flaggr "github.com/flaggr-dev/flaggr-go"
)
func main() {
client, err := flaggr.NewClient(
flaggr.WithAPIKey(os.Getenv("FLAGGR_API_KEY")), // project API token (fgr_...)
flaggr.WithServiceID(os.Getenv("FLAGGR_SERVICE_ID")), // the service's ID, not its slug
flaggr.WithEnvironment("production"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
enabled, err := client.Boolean(context.Background(), "checkout-v2", false, flaggr.EvalContext{
TargetingKey: "user-123",
Attributes: map[string]interface{}{"plan": "enterprise", "country": "AU"},
})
if err != nil {
log.Printf("checkout-v2: using the default: %v", err)
}
fmt.Println("checkout-v2:", enabled)
}
Output:
func (*Client) Boolean ¶
func (c *Client) Boolean(ctx context.Context, flagKey string, defaultValue bool, evalCtx ...EvalContext) (bool, error)
Boolean evaluates a boolean flag.
func (*Client) InvalidateFlag ¶
InvalidateFlag removes a flag from the local in-process cache.
func (*Client) LoadFlags ¶
func (c *Client) LoadFlags(flags []CachedFlag)
LoadFlags replaces all flags in the local in-process cache.
func (*Client) Number ¶
func (c *Client) Number(ctx context.Context, flagKey string, defaultValue float64, evalCtx ...EvalContext) (float64, error)
Number evaluates a number flag.
func (*Client) Object ¶
func (c *Client) Object(ctx context.Context, flagKey string, defaultValue map[string]interface{}, evalCtx ...EvalContext) (map[string]interface{}, error)
Object evaluates an object flag.
func (*Client) Resolve ¶
func (c *Client) Resolve(ctx context.Context, flagKey string, defaultValue interface{}, evalCtx ...EvalContext) (*EvaluationDetail, error)
Resolve evaluates a flag and returns the full evaluation detail.
Example ¶
package main
import (
"context"
"fmt"
"log"
"os"
flaggr "github.com/flaggr-dev/flaggr-go"
)
func main() {
client, err := flaggr.NewClient(
flaggr.WithAPIKey(os.Getenv("FLAGGR_API_KEY")),
flaggr.WithServiceID(os.Getenv("FLAGGR_SERVICE_ID")),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
detail, err := client.Resolve(context.Background(), "checkout-flow-version", "v1", flaggr.EvalContext{
TargetingKey: "user-42",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("value=%v reason=%s variant=%s\n", detail.Value, detail.Reason, detail.Variant)
}
Output:
func (*Client) SetFlag ¶
func (c *Client) SetFlag(flag CachedFlag)
SetFlag inserts or updates a flag in the local in-process cache.
type EvalContext ¶
EvalContext provides targeting attributes for flag evaluation.
type EvaluationDetail ¶
type EvaluationDetail struct {
Value interface{} `json:"value"`
Reason string `json:"reason"`
Variant string `json:"variant"`
ErrorCode string `json:"errorCode,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
}
EvaluationDetail contains the full result of a flag evaluation.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithAPIKey ¶
WithAPIKey sets the API authentication token. Surrounding whitespace, such as a trailing newline, is removed.
func WithAPIURL ¶
WithAPIURL sets the Flaggr API URL. Surrounding whitespace and trailing slashes are removed.
func WithEnvironment ¶
WithEnvironment sets the target environment (default: "production"). Surrounding whitespace is removed.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client. A nil client keeps the default.
func WithLocalEvaluation ¶
WithLocalEvaluation enables zero-network in-process memory evaluation.
Example ¶
package main
import (
"context"
"fmt"
"log"
flaggr "github.com/flaggr-dev/flaggr-go"
)
func main() {
client, err := flaggr.NewClient(
flaggr.WithAPIKey("fgr_example"),
flaggr.WithServiceID("example-service-id"),
flaggr.WithLocalEvaluation(true),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
client.LoadFlags([]flaggr.CachedFlag{{
Key: "dark-mode",
Enabled: true,
DefaultValue: false,
Overrides: []flaggr.CachedOverride{
{Identifiers: []string{"user-123"}, Value: true},
},
}})
ctx := context.Background()
forUser, _ := client.Boolean(ctx, "dark-mode", false, flaggr.EvalContext{TargetingKey: "user-123"})
forOthers, _ := client.Boolean(ctx, "dark-mode", false, flaggr.EvalContext{TargetingKey: "user-456"})
missing, _ := client.Boolean(ctx, "not-cached", true)
fmt.Println(forUser, forOthers, missing)
}
Output: true false true
func WithServiceID ¶
WithServiceID sets the service identifier for evaluations. Surrounding whitespace is removed.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package errorreport sends a Go program's errors and panics to Flaggr, where they are grouped into issues alongside the flag variants that were active when they happened (the console's Issues page).
|
Package errorreport sends a Go program's errors and panics to Flaggr, where they are grouped into issues alongside the flag variants that were active when they happened (the console's Issues page). |