Documentation
¶
Overview ¶
Package openfeature provides global access to the OpenFeature API.
Index ¶
- Constants
- Variables
- func AddHandler(eventType EventType, callback EventCallback)
- func AddHooks(hooks ...Hook)
- func MergeTransactionContext(ctx context.Context, ec EvaluationContext) context.Context
- func RemoveHandler(eventType EventType, callback EventCallback)
- func SetEvaluationContext(evalCtx EvaluationContext)
- func SetLogger(l logr.Logger)
- func SetNamedProvider(domain string, provider FeatureProvider) error
- func SetNamedProviderAndWait(domain string, provider FeatureProvider) error
- func SetProvider(provider FeatureProvider) error
- func SetProviderAndWait(provider FeatureProvider) error
- func Shutdown()
- func WithTransactionContext(ctx context.Context, ec EvaluationContext) context.Context
- type BoolResolutionDetail
- type BooleanEvaluationDetails
- type Client
- func (c *Client) AddHandler(eventType EventType, callback EventCallback)
- func (c *Client) AddHooks(hooks ...Hook)
- func (c *Client) Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, ...) bool
- func (c *Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, ...) (bool, error)
- func (c *Client) BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, ...) (BooleanEvaluationDetails, error)
- func (c *Client) EvaluationContext() EvaluationContext
- func (c *Client) Float(ctx context.Context, flag string, defaultValue float64, ...) float64
- func (c *Client) FloatValue(ctx context.Context, flag string, defaultValue float64, ...) (float64, error)
- func (c *Client) FloatValueDetails(ctx context.Context, flag string, defaultValue float64, ...) (FloatEvaluationDetails, error)
- func (c *Client) Int(ctx context.Context, flag string, defaultValue int64, ...) int64
- func (c *Client) IntValue(ctx context.Context, flag string, defaultValue int64, ...) (int64, error)
- func (c *Client) IntValueDetails(ctx context.Context, flag string, defaultValue int64, ...) (IntEvaluationDetails, error)
- func (c *Client) Metadata() ClientMetadata
- func (c *Client) Object(ctx context.Context, flag string, defaultValue interface{}, ...) interface{}
- func (c *Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, ...) (interface{}, error)
- func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, ...) (InterfaceEvaluationDetails, error)
- func (c *Client) RemoveHandler(eventType EventType, callback EventCallback)
- func (c *Client) SetEvaluationContext(evalCtx EvaluationContext)
- func (c *Client) State() State
- func (c *Client) String(ctx context.Context, flag string, defaultValue string, ...) string
- func (c *Client) StringValue(ctx context.Context, flag string, defaultValue string, ...) (string, error)
- func (c *Client) StringValueDetails(ctx context.Context, flag string, defaultValue string, ...) (StringEvaluationDetails, error)
- func (c *Client) Track(ctx context.Context, trackingEventName string, evalCtx EvaluationContext, ...)
- func (c *Client) WithLogger(l logr.Logger) *Client
- type ClientMetadata
- type ErrorCode
- type EvaluationContext
- type EvaluationDetails
- type EvaluationOptions
- type Event
- type EventCallback
- type EventDetails
- type EventHandler
- type EventType
- type FeatureProvider
- type FlagMetadata
- type FlattenedContext
- type FloatEvaluationDetails
- type FloatResolutionDetail
- type Hook
- type HookContext
- type HookHints
- type IClient
- type IEvaluation
- type IEventing
- type ITracking
- type IntEvaluationDetails
- type IntResolutionDetail
- type InterfaceEvaluationDetails
- type InterfaceResolutionDetail
- type Metadata
- type NoopEventHandler
- type NoopProvider
- func (e NoopProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx FlattenedContext) BoolResolutionDetail
- func (e NoopProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, ...) FloatResolutionDetail
- func (e NoopProvider) Hooks() []Hook
- func (e NoopProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx FlattenedContext) IntResolutionDetail
- func (e NoopProvider) Metadata() Metadata
- func (e NoopProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, ...) InterfaceResolutionDetail
- func (e NoopProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, ...) StringResolutionDetail
- func (e NoopProvider) Track(ctx context.Context, eventName string, evalCtx EvaluationContext, ...)
- type NoopStateHandler
- type Option
- type ProviderEventDetails
- type ProviderInitError
- type ProviderResolutionDetail
- type Reason
- type ResolutionDetail
- type ResolutionError
- func NewFlagNotFoundResolutionError(msg string) ResolutionError
- func NewGeneralResolutionError(msg string) ResolutionError
- func NewInvalidContextResolutionError(msg string) ResolutionError
- func NewParseErrorResolutionError(msg string) ResolutionError
- func NewProviderNotReadyResolutionError(msg string) ResolutionError
- func NewTargetingKeyMissingResolutionError(msg string) ResolutionError
- func NewTypeMismatchResolutionError(msg string) ResolutionError
- type State
- type StateHandler
- type StringEvaluationDetails
- type StringResolutionDetail
- type Tracker
- type TrackingEventDetails
- func (t TrackingEventDetails) Add(key string, value interface{}) TrackingEventDetails
- func (t TrackingEventDetails) Attribute(key string) interface{}
- func (t TrackingEventDetails) Attributes() map[string]interface{}
- func (t TrackingEventDetails) Copy(value float64) TrackingEventDetails
- func (t TrackingEventDetails) Value() float64
- type Type
- type UnimplementedHook
- func (UnimplementedHook) After(context.Context, HookContext, InterfaceEvaluationDetails, HookHints) error
- func (UnimplementedHook) Before(context.Context, HookContext, HookHints) (*EvaluationContext, error)
- func (UnimplementedHook) Error(context.Context, HookContext, error, HookHints)
- func (UnimplementedHook) Finally(context.Context, HookContext, HookHints)
Examples ¶
Constants ¶
const ( // DefaultReason - the resolved value was configured statically, or otherwise fell back to a pre-configured value. DefaultReason Reason = "DEFAULT" // TargetingMatchReason - the resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting. TargetingMatchReason Reason = "TARGETING_MATCH" // SplitReason - the resolved value was the result of pseudorandom assignment. SplitReason Reason = "SPLIT" // DisabledReason - the resolved value was the result of the flag being disabled in the management system. DisabledReason Reason = "DISABLED" // StaticReason - the resolved value is static (no dynamic evaluation) StaticReason Reason = "STATIC" // CachedReason - the resolved value was retrieved from cache CachedReason Reason = "CACHED" // UnknownReason - the reason for the resolved value could not be determined. UnknownReason Reason = "UNKNOWN" // ErrorReason - the resolved value was the result of an error. ErrorReason Reason = "ERROR" NotReadyState State = "NOT_READY" ReadyState State = "READY" ErrorState State = "ERROR" StaleState State = "STALE" FatalState State = "FATAL" ProviderReady EventType = "PROVIDER_READY" ProviderConfigChange EventType = "PROVIDER_CONFIGURATION_CHANGED" ProviderStale EventType = "PROVIDER_STALE" ProviderError EventType = "PROVIDER_ERROR" TargetingKey string = "targetingKey" // evaluation context map key. The targeting key uniquely identifies the subject (end-user, or client service) of a flag evaluation. )
Variables ¶
var ( // ProviderNotReadyError signifies that an operation failed because the provider is in a NOT_READY state. ProviderNotReadyError = errors.New("provider not yet initialized") // ProviderFatalError signifies that an operation failed because the provider is in a FATAL state. ProviderFatalError = errors.New("provider is in an irrecoverable error state") )
Functions ¶
func AddHandler ¶
func AddHandler(eventType EventType, callback EventCallback)
AddHandler allows to add API level event handler
func AddHooks ¶
func AddHooks(hooks ...Hook)
AddHooks appends to the collection of any previously added hooks
func MergeTransactionContext ¶ added in v1.13.0
func MergeTransactionContext(ctx context.Context, ec EvaluationContext) context.Context
MergeTransactionContext merges the provided EvaluationContext with the current TransactionContext (if it exists)
ctx - the context to pull existing TransactionContext from ec - the EvaluationContext to merge with the existing TransactionContext
func RemoveHandler ¶
func RemoveHandler(eventType EventType, callback EventCallback)
RemoveHandler allows to remove API level event handler
func SetEvaluationContext ¶
func SetEvaluationContext(evalCtx EvaluationContext)
SetEvaluationContext sets the global evaluation context.
func SetNamedProvider ¶
func SetNamedProvider(domain string, provider FeatureProvider) error
SetNamedProvider sets a provider mapped to the given Client domain. Provider initialization is asynchronous and status can be checked from provider status
func SetNamedProviderAndWait ¶ added in v1.10.0
func SetNamedProviderAndWait(domain string, provider FeatureProvider) error
SetNamedProviderAndWait sets a provider mapped to the given Client domain and waits for its initialization. Returns an error if initialization cause error
func SetProvider ¶
func SetProvider(provider FeatureProvider) error
SetProvider sets the default provider. Provider initialization is asynchronous and status can be checked from provider status
func SetProviderAndWait ¶ added in v1.10.0
func SetProviderAndWait(provider FeatureProvider) error
SetProviderAndWait sets the default provider and waits for its initialization. Returns an error if initialization cause error
func WithTransactionContext ¶ added in v1.13.0
func WithTransactionContext(ctx context.Context, ec EvaluationContext) context.Context
NewTransactionContext constructs a TransactionContext
ctx - the context to embed the EvaluationContext in ec - the EvaluationContext to embed into the context
Types ¶
type BoolResolutionDetail ¶
type BoolResolutionDetail struct { Value bool ProviderResolutionDetail }
BoolResolutionDetail provides a resolution detail with boolean type
type BooleanEvaluationDetails ¶
type BooleanEvaluationDetails struct { Value bool EvaluationDetails }
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements the behaviour required of an openfeature client
func NewClient ¶
NewClient returns a new Client. Name is a unique identifier for this client This helper exists for historical reasons. It is recommended to interact with IEvaluation to derive IClient instances.
Example ¶
package main import ( "fmt" "github.com/open-feature/go-sdk/openfeature" ) func main() { client := openfeature.NewClient("example-client") fmt.Printf("Client Domain: %s", client.Metadata().Domain()) }
Output: Client Domain: example-client
func (*Client) AddHandler ¶
func (c *Client) AddHandler(eventType EventType, callback EventCallback)
AddHandler allows to add Client level event handler
func (*Client) Boolean ¶ added in v1.11.0
func (c *Client) Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool
Boolean performs a flag evaluation that returns a boolean. Any error encountered during the evaluation will result in the default value being returned. To explicitly handle errors, use [BooleanValue] or [BooleanValueDetails]
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } ctx := context.Background() client := openfeature.NewClient("example-client") if client.Boolean(ctx, "myflag", true, openfeature.EvaluationContext{}) { fmt.Println("myflag is true") } else { fmt.Println("myflag is false") } }
Output: myflag is true
func (*Client) BooleanValue ¶
func (c *Client) BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error)
BooleanValue performs a flag evaluation that returns a boolean.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } client := openfeature.NewClient("example-client") value, err := client.BooleanValue( context.Background(), "test-flag", true, openfeature.EvaluationContext{}, ) if err != nil { log.Fatal("error while getting boolean value : ", err) } fmt.Printf("test-flag value: %v", value) }
Output: test-flag value: true
func (*Client) BooleanValueDetails ¶
func (c *Client) BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (BooleanEvaluationDetails, error)
BooleanValueDetails performs a flag evaluation that returns an evaluation details struct.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (*Client) EvaluationContext ¶
func (c *Client) EvaluationContext() EvaluationContext
EvaluationContext returns the client's evaluation context
func (*Client) Float ¶ added in v1.11.0
func (c *Client) Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64
Float performs a flag evaluation that returns a float64. Any error encountered during the evaluation will result in the default value being returned. To explicitly handle errors, use [FloatValue] or [FloatValueDetails]
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } ctx := context.Background() client := openfeature.NewClient("example-client") fmt.Println(client.Float(ctx, "myflag", 0.5, openfeature.EvaluationContext{})) }
Output: 0.5
func (*Client) FloatValue ¶
func (c *Client) FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error)
FloatValue performs a flag evaluation that returns a float64.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } client := openfeature.NewClient("example-client") value, err := client.FloatValue( context.Background(), "test-flag", 0.55, openfeature.EvaluationContext{}, ) if err != nil { log.Fatalf("error while getting float value: %v", err) } fmt.Printf("test-flag value: %v", value) }
Output: test-flag value: 0.55
func (*Client) FloatValueDetails ¶
func (c *Client) FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error)
FloatValueDetails performs a flag evaluation that returns an evaluation details struct.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (*Client) Int ¶ added in v1.11.0
func (c *Client) Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64
Int performs a flag evaluation that returns an int64. Any error encountered during the evaluation will result in the default value being returned. To explicitly handle errors, use [IntValue] or [IntValueDetails]
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } ctx := context.Background() client := openfeature.NewClient("example-client") fmt.Println(client.Int(ctx, "myflag", 5, openfeature.EvaluationContext{})) }
Output: 5
func (*Client) IntValue ¶
func (c *Client) IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error)
IntValue performs a flag evaluation that returns an int64.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } client := openfeature.NewClient("example-client") value, err := client.IntValue( context.Background(), "test-flag", 3, openfeature.EvaluationContext{}, ) if err != nil { log.Fatalf("error while getting int value: %v", err) } fmt.Printf("test-flag value: %v", value) }
Output: test-flag value: 3
func (*Client) IntValueDetails ¶
func (c *Client) IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error)
IntValueDetails performs a flag evaluation that returns an evaluation details struct.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (*Client) Metadata ¶
func (c *Client) Metadata() ClientMetadata
Metadata returns the client's metadata
func (*Client) Object ¶ added in v1.11.0
func (c *Client) Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{}
Object performs a flag evaluation that returns an object. Any error encountered during the evaluation will result in the default value being returned. To explicitly handle errors, use [ObjectValue] or [ObjectValueDetails]
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } ctx := context.Background() client := openfeature.NewClient("example-client") fmt.Println(client.Object(ctx, "myflag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{})) }
Output: map[foo:bar]
func (*Client) ObjectValue ¶
func (c *Client) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error)
ObjectValue performs a flag evaluation that returns an object.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "encoding/json" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } client := openfeature.NewClient("example-client") value, err := client.ObjectValue( context.Background(), "test-flag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{}, ) if err != nil { log.Fatal("error while getting object value : ", err) } str, _ := json.Marshal(value) fmt.Printf("test-flag value: %v", string(str)) }
Output: test-flag value: {"foo":"bar"}
func (*Client) ObjectValueDetails ¶
func (c *Client) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)
ObjectValueDetails performs a flag evaluation that returns an evaluation details struct.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (*Client) RemoveHandler ¶
func (c *Client) RemoveHandler(eventType EventType, callback EventCallback)
RemoveHandler allows to remove Client level event handler
func (*Client) SetEvaluationContext ¶
func (c *Client) SetEvaluationContext(evalCtx EvaluationContext)
SetEvaluationContext sets the client's evaluation context
func (*Client) String ¶ added in v1.11.0
func (c *Client) String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string
String performs a flag evaluation that returns a string. Any error encountered during the evaluation will result in the default value being returned. To explicitly handle errors, use [StringValue] or [StringValueDetails]
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } ctx := context.Background() client := openfeature.NewClient("example-client") fmt.Println(client.String(ctx, "myflag", "default", openfeature.EvaluationContext{})) }
Output: default
func (*Client) StringValue ¶
func (c *Client) StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error)
StringValue performs a flag evaluation that returns a string.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
Example ¶
package main import ( "context" "fmt" "log" "github.com/open-feature/go-sdk/openfeature" ) func main() { if err := openfeature.SetNamedProviderAndWait("example-client", openfeature.NoopProvider{}); err != nil { log.Fatalf("error setting up provider %v", err) } client := openfeature.NewClient("example-client") value, err := client.StringValue( context.Background(), "test-flag", "openfeature", openfeature.EvaluationContext{}, ) if err != nil { log.Fatal("error while getting string value : ", err) } fmt.Printf("test-flag value: %v", value) }
Output: test-flag value: openfeature
func (*Client) StringValueDetails ¶
func (c *Client) StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (StringEvaluationDetails, error)
StringValueDetails performs a flag evaluation that returns an evaluation details struct.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - flag is the key that uniquely identifies a particular flag - defaultValue is returned if an error occurs - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - options are optional additional evaluation options e.g. WithHooks & WithHookHints
func (*Client) Track ¶ added in v1.14.0
func (c *Client) Track(ctx context.Context, trackingEventName string, evalCtx EvaluationContext, details TrackingEventDetails)
Track performs an action for tracking for occurrence of a particular action or application state.
Parameters: - ctx is the standard go context struct used to manage requests (e.g. timeouts) - trackingEventName is the event name to track - evalCtx is the evaluation context used in a flag evaluation (not to be confused with ctx) - trackingEventDetails defines optional data pertinent to a particular
Example ¶
package main import ( "context" "github.com/open-feature/go-sdk/openfeature" ) func main() { ctx := context.Background() client := openfeature.NewClient("example-client") evaluationContext := openfeature.EvaluationContext{} // example tracking event recording that a subject reached a page associated with a business goal client.Track(ctx, "visited-promo-page", evaluationContext, openfeature.TrackingEventDetails{}) // example tracking event recording that a subject performed an action associated with a business goal, with the tracking event details having a particular numeric value client.Track(ctx, "clicked-checkout", evaluationContext, openfeature.NewTrackingEventDetails(99.77)) // example tracking event recording that a subject performed an action associated with a business goal, with the tracking event details having a particular numeric value client.Track(ctx, "clicked-checkout", evaluationContext, openfeature.NewTrackingEventDetails(99.77).Add("currencyCode", "USD")) }
Output:
type ClientMetadata ¶
type ClientMetadata struct {
// contains filtered or unexported fields
}
ClientMetadata provides a client's metadata
func NewClientMetadata ¶
func NewClientMetadata(domain string) ClientMetadata
NewClientMetadata constructs ClientMetadata Allows for simplified hook test cases while maintaining immutability
func (ClientMetadata) Domain ¶ added in v1.12.0
func (cm ClientMetadata) Domain() string
Domain returns the client's domain
func (ClientMetadata) Name ¶
func (cm ClientMetadata) Name() string
Name returns the client's domain name Deprecated: Name() exists for historical compatibility, use Domain() instead.
type ErrorCode ¶
type ErrorCode string
const ( // ProviderNotReadyCode - the value was resolved before the provider was ready. ProviderNotReadyCode ErrorCode = "PROVIDER_NOT_READY" // ProviderFatalCode - a fatal provider error occured ProviderFatalCode ErrorCode = "PROVIDER_FATAL" // FlagNotFoundCode - the flag could not be found. FlagNotFoundCode ErrorCode = "FLAG_NOT_FOUND" // ParseErrorCode - an error was encountered parsing data, such as a flag configuration. ParseErrorCode ErrorCode = "PARSE_ERROR" // TypeMismatchCode - the type of the flag value does not match the expected type. TypeMismatchCode ErrorCode = "TYPE_MISMATCH" // TargetingKeyMissingCode - the provider requires a targeting key and one was not provided in the evaluation context. TargetingKeyMissingCode ErrorCode = "TARGETING_KEY_MISSING" // InvalidContextCode - the evaluation context does not meet provider requirements. InvalidContextCode ErrorCode = "INVALID_CONTEXT" // GeneralCode - the error was for a reason not enumerated above. GeneralCode ErrorCode = "GENERAL" )
type EvaluationContext ¶
type EvaluationContext struct {
// contains filtered or unexported fields
}
EvaluationContext provides ambient information for the purposes of flag evaluation The use of the constructor, NewEvaluationContext, is enforced to set EvaluationContext's fields in order to enforce immutability. https://openfeature.dev/specification/sections/evaluation-context
func NewEvaluationContext ¶
func NewEvaluationContext(targetingKey string, attributes map[string]interface{}) EvaluationContext
NewEvaluationContext constructs an EvaluationContext
targetingKey - uniquely identifying the subject (end-user, or client service) of a flag evaluation attributes - contextual data used in flag evaluation
func NewTargetlessEvaluationContext ¶
func NewTargetlessEvaluationContext(attributes map[string]interface{}) EvaluationContext
NewTargetlessEvaluationContext constructs an EvaluationContext with an empty targeting key
attributes - contextual data used in flag evaluation
func TransactionContext ¶ added in v1.13.0
func TransactionContext(ctx context.Context) EvaluationContext
TransactionContext extracts a EvaluationContext from the current golang.org/x/net/context. if no EvaluationContext exist, it will construct an empty EvaluationContext
ctx - the context to pull EvaluationContext from
func (EvaluationContext) Attribute ¶
func (e EvaluationContext) Attribute(key string) interface{}
Attribute retrieves the attribute with the given key
func (EvaluationContext) Attributes ¶
func (e EvaluationContext) Attributes() map[string]interface{}
Attributes returns a copy of the EvaluationContext's attributes
func (EvaluationContext) TargetingKey ¶
func (e EvaluationContext) TargetingKey() string
TargetingKey returns the key uniquely identifying the subject (end-user, or client service) of a flag evaluation
type EvaluationDetails ¶
type EvaluationDetails struct { FlagKey string FlagType Type ResolutionDetail }
type EvaluationOptions ¶
type EvaluationOptions struct {
// contains filtered or unexported fields
}
EvaluationOptions should contain a list of hooks to be executed for a flag evaluation
func (EvaluationOptions) HookHints ¶
func (e EvaluationOptions) HookHints() HookHints
HookHints returns evaluation options' hook hints
func (EvaluationOptions) Hooks ¶
func (e EvaluationOptions) Hooks() []Hook
Hooks returns evaluation options' hooks
type Event ¶
type Event struct { ProviderName string EventType ProviderEventDetails }
Event is an event emitted by a FeatureProvider.
type EventCallback ¶
type EventCallback *func(details EventDetails)
type EventDetails ¶
type EventDetails struct { ProviderName string ProviderEventDetails }
type EventHandler ¶
type EventHandler interface {
EventChannel() <-chan Event
}
EventHandler is the eventing contract enforced for FeatureProvider
type FeatureProvider ¶
type FeatureProvider interface { Metadata() Metadata BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx FlattenedContext) BoolResolutionDetail StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx FlattenedContext) StringResolutionDetail FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx FlattenedContext) FloatResolutionDetail IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx FlattenedContext) IntResolutionDetail ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx FlattenedContext) InterfaceResolutionDetail Hooks() []Hook }
FeatureProvider interface defines a set of functions that can be called in order to evaluate a flag. This should be implemented by flag management systems.
type FlagMetadata ¶
type FlagMetadata map[string]interface{}
FlagMetadata is a structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, int64 or float64. This structure is populated by a provider for use by an Application Author (via the Evaluation API) or an Application Integrator (via hooks).
func (FlagMetadata) GetBool ¶
func (f FlagMetadata) GetBool(key string) (bool, error)
GetBool fetch bool value from FlagMetadata. Returns an error if the key does not exist, or, the value is of the wrong type
func (FlagMetadata) GetFloat ¶
func (f FlagMetadata) GetFloat(key string) (float64, error)
GetFloat fetch float64 value from FlagMetadata. Returns an error if the key does not exist, or, the value is of the wrong type
type FlattenedContext ¶
type FlattenedContext map[string]interface{}
FlattenedContext contains metadata for a given flag evaluation in a flattened structure. TargetingKey ("targetingKey") is stored as a string value if provided in the evaluation context.
type FloatEvaluationDetails ¶
type FloatEvaluationDetails struct { Value float64 EvaluationDetails }
type FloatResolutionDetail ¶
type FloatResolutionDetail struct { Value float64 ProviderResolutionDetail }
FloatResolutionDetail provides a resolution detail with float64 type
type Hook ¶
type Hook interface { Before(ctx context.Context, hookContext HookContext, hookHints HookHints) (*EvaluationContext, error) After(ctx context.Context, hookContext HookContext, flagEvaluationDetails InterfaceEvaluationDetails, hookHints HookHints) error Error(ctx context.Context, hookContext HookContext, err error, hookHints HookHints) Finally(ctx context.Context, hookContext HookContext, hookHints HookHints) }
Hook allows application developers to add arbitrary behavior to the flag evaluation lifecycle. They operate similarly to middleware in many web frameworks. https://github.com/open-feature/spec/blob/main/specification/hooks.md
type HookContext ¶
type HookContext struct {
// contains filtered or unexported fields
}
HookContext defines the base level fields of a hook context
func NewHookContext ¶
func NewHookContext( flagKey string, flagType Type, defaultValue interface{}, clientMetadata ClientMetadata, providerMetadata Metadata, evaluationContext EvaluationContext, ) HookContext
NewHookContext constructs HookContext Allows for simplified hook test cases while maintaining immutability
func (HookContext) ClientMetadata ¶
func (h HookContext) ClientMetadata() ClientMetadata
ClientMetadata returns the client's metadata
func (HookContext) DefaultValue ¶
func (h HookContext) DefaultValue() interface{}
DefaultValue returns the hook context's default value
func (HookContext) EvaluationContext ¶
func (h HookContext) EvaluationContext() EvaluationContext
EvaluationContext returns the hook context's EvaluationContext
func (HookContext) FlagKey ¶
func (h HookContext) FlagKey() string
FlagKey returns the hook context's flag key
func (HookContext) FlagType ¶
func (h HookContext) FlagType() Type
FlagType returns the hook context's flag type
func (HookContext) ProviderMetadata ¶
func (h HookContext) ProviderMetadata() Metadata
ProviderMetadata returns the provider's metadata
type HookHints ¶
type HookHints struct {
// contains filtered or unexported fields
}
HookHints contains a map of hints for hooks
func NewHookHints ¶
NewHookHints constructs HookHints
type IClient ¶
type IClient interface { Metadata() ClientMetadata AddHooks(hooks ...Hook) SetEvaluationContext(evalCtx EvaluationContext) EvaluationContext() EvaluationContext BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error) StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error) FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error) IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error) ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error) BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (BooleanEvaluationDetails, error) StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (StringEvaluationDetails, error) FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error) IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error) ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error) Boolean(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) bool String(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) string Float(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) float64 Int(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) int64 Object(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) interface{} State() State IEventing ITracking }
IClient defines the behaviour required of an OpenFeature client
type IEvaluation ¶ added in v1.12.0
type IEvaluation interface { SetProvider(provider FeatureProvider) error SetProviderAndWait(provider FeatureProvider) error GetProviderMetadata() Metadata SetNamedProvider(clientName string, provider FeatureProvider, async bool) error GetNamedProviderMetadata(name string) Metadata GetClient() IClient GetNamedClient(clientName string) IClient SetEvaluationContext(apiCtx EvaluationContext) AddHooks(hooks ...Hook) Shutdown() IEventing }
IEvaluation defines the OpenFeature API contract
func GetApiInstance ¶ added in v1.12.0
func GetApiInstance() IEvaluation
GetApiInstance returns the current singleton IEvaluation instance. This is the preferred interface to interact with OpenFeature functionalities
type IEventing ¶ added in v1.12.0
type IEventing interface { AddHandler(eventType EventType, callback EventCallback) RemoveHandler(eventType EventType, callback EventCallback) }
IEventing defines the OpenFeature eventing contract
type ITracking ¶ added in v1.14.0
type ITracking interface {
Track(ctx context.Context, trackingEventName string, evalCtx EvaluationContext, details TrackingEventDetails)
}
ITracking defines the Tracking contract
type IntEvaluationDetails ¶
type IntEvaluationDetails struct { Value int64 EvaluationDetails }
type IntResolutionDetail ¶
type IntResolutionDetail struct { Value int64 ProviderResolutionDetail }
IntResolutionDetail provides a resolution detail with int64 type
type InterfaceEvaluationDetails ¶
type InterfaceEvaluationDetails struct { Value interface{} EvaluationDetails }
type InterfaceResolutionDetail ¶
type InterfaceResolutionDetail struct { Value interface{} ProviderResolutionDetail }
InterfaceResolutionDetail provides a resolution detail with interface{} type
type Metadata ¶
type Metadata struct {
Name string
}
Metadata provides provider name
func NamedProviderMetadata ¶ added in v1.12.0
NamedProviderMetadata returns the named provider's Metadata
func ProviderMetadata ¶
func ProviderMetadata() Metadata
ProviderMetadata returns the default provider's metadata
type NoopEventHandler ¶
type NoopEventHandler struct { }
NoopEventHandler is the out-of-the-box EventHandler which is noop
func (NoopEventHandler) EventChannel ¶
func (s NoopEventHandler) EventChannel() <-chan Event
type NoopProvider ¶
type NoopProvider struct { }
NoopProvider implements the FeatureProvider interface and provides functions for evaluating flags
func (NoopProvider) BooleanEvaluation ¶
func (e NoopProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx FlattenedContext) BoolResolutionDetail
BooleanEvaluation returns a boolean flag.
func (NoopProvider) FloatEvaluation ¶
func (e NoopProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx FlattenedContext) FloatResolutionDetail
FloatEvaluation returns a float flag.
func (NoopProvider) IntEvaluation ¶
func (e NoopProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx FlattenedContext) IntResolutionDetail
IntEvaluation returns an int flag.
func (NoopProvider) Metadata ¶
func (e NoopProvider) Metadata() Metadata
Metadata returns the metadata of the provider
func (NoopProvider) ObjectEvaluation ¶
func (e NoopProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx FlattenedContext) InterfaceResolutionDetail
ObjectEvaluation returns an object flag
func (NoopProvider) StringEvaluation ¶
func (e NoopProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx FlattenedContext) StringResolutionDetail
StringEvaluation returns a string flag.
func (NoopProvider) Track ¶ added in v1.14.0
func (e NoopProvider) Track(ctx context.Context, eventName string, evalCtx EvaluationContext, details TrackingEventDetails)
type NoopStateHandler ¶
type NoopStateHandler struct { }
NoopStateHandler is a noop StateHandler implementation Status always set to ReadyState to comply with specification
func (*NoopStateHandler) Init ¶
func (s *NoopStateHandler) Init(e EvaluationContext) error
func (*NoopStateHandler) Shutdown ¶
func (s *NoopStateHandler) Shutdown()
func (*NoopStateHandler) Status ¶
func (s *NoopStateHandler) Status() State
type Option ¶
type Option func(*EvaluationOptions)
Option applies a change to EvaluationOptions
func WithHookHints ¶
WithHookHints applies provided hook hints.
type ProviderEventDetails ¶
type ProviderEventDetails struct { Message string FlagChanges []string EventMetadata map[string]interface{} ErrorCode ErrorCode }
ProviderEventDetails is the event payload emitted by FeatureProvider
type ProviderInitError ¶ added in v1.14.0
type ProviderInitError struct { ErrorCode ErrorCode // Field to store the specific error code Message string // Custom error message }
ProviderInitError represents an error that occurs during provider initialization.
func (*ProviderInitError) Error ¶ added in v1.14.0
func (e *ProviderInitError) Error() string
Error implements the error interface for ProviderInitError.
type ProviderResolutionDetail ¶
type ProviderResolutionDetail struct { ResolutionError ResolutionError Reason Reason Variant string FlagMetadata FlagMetadata }
ProviderResolutionDetail is a structure which contains a subset of the fields defined in the EvaluationDetail, representing the result of the provider's flag resolution process see https://github.com/open-feature/spec/blob/main/specification/types.md#resolution-details N.B we could use generics but to support older versions of go for now we will have type specific resolution detail
func (ProviderResolutionDetail) Error ¶
func (p ProviderResolutionDetail) Error() error
func (ProviderResolutionDetail) ResolutionDetail ¶
func (p ProviderResolutionDetail) ResolutionDetail() ResolutionDetail
type ResolutionDetail ¶
type ResolutionDetail struct { Variant string Reason Reason ErrorCode ErrorCode ErrorMessage string FlagMetadata FlagMetadata }
type ResolutionError ¶
type ResolutionError struct {
// contains filtered or unexported fields
}
ResolutionError is an enumerated error code with an optional message
func NewFlagNotFoundResolutionError ¶
func NewFlagNotFoundResolutionError(msg string) ResolutionError
NewFlagNotFoundResolutionError constructs a resolution error with code FLAG_NOT_FOUND
Explanation - The flag could not be found.
func NewGeneralResolutionError ¶
func NewGeneralResolutionError(msg string) ResolutionError
NewGeneralResolutionError constructs a resolution error with code GENERAL
Explanation - The error was for a reason not enumerated above.
func NewInvalidContextResolutionError ¶
func NewInvalidContextResolutionError(msg string) ResolutionError
NewInvalidContextResolutionError constructs a resolution error with code INVALID_CONTEXT
Explanation - The evaluation context does not meet provider requirements.
func NewParseErrorResolutionError ¶
func NewParseErrorResolutionError(msg string) ResolutionError
NewParseErrorResolutionError constructs a resolution error with code PARSE_ERROR
Explanation - An error was encountered parsing data, such as a flag configuration.
func NewProviderNotReadyResolutionError ¶
func NewProviderNotReadyResolutionError(msg string) ResolutionError
NewProviderNotReadyResolutionError constructs a resolution error with code PROVIDER_NOT_READY
Explanation - The value was resolved before the provider was ready.
func NewTargetingKeyMissingResolutionError ¶
func NewTargetingKeyMissingResolutionError(msg string) ResolutionError
NewTargetingKeyMissingResolutionError constructs a resolution error with code TARGETING_KEY_MISSING
Explanation - The provider requires a targeting key and one was not provided in the evaluation context.
func NewTypeMismatchResolutionError ¶
func NewTypeMismatchResolutionError(msg string) ResolutionError
NewTypeMismatchResolutionError constructs a resolution error with code TYPE_MISMATCH
Explanation - The type of the flag value does not match the expected type.
func (ResolutionError) Error ¶
func (r ResolutionError) Error() string
type StateHandler ¶
type StateHandler interface { Init(evaluationContext EvaluationContext) error Shutdown() }
StateHandler is the contract for initialization & shutdown. FeatureProvider can opt in for this behavior by implementing the interface
type StringEvaluationDetails ¶
type StringEvaluationDetails struct { Value string EvaluationDetails }
type StringResolutionDetail ¶
type StringResolutionDetail struct { Value string ProviderResolutionDetail }
StringResolutionDetail provides a resolution detail with string type
type Tracker ¶ added in v1.14.0
type Tracker interface {
Track(ctx context.Context, trackingEventName string, evaluationContext EvaluationContext, details TrackingEventDetails)
}
Tracker is the contract for tracking FeatureProvider can opt in for this behavior by implementing the interface
type TrackingEventDetails ¶ added in v1.14.0
type TrackingEventDetails struct {
// contains filtered or unexported fields
}
TrackingEventDetails provides a tracking details with float64 value
func NewTrackingEventDetails ¶ added in v1.14.0
func NewTrackingEventDetails(value float64) TrackingEventDetails
NewTrackingEventDetails return TrackingEventDetails associated with numeric value value
func (TrackingEventDetails) Add ¶ added in v1.14.0
func (t TrackingEventDetails) Add(key string, value interface{}) TrackingEventDetails
Add insert new key-value pair into TrackingEventDetails and return the TrackingEventDetails itself. If the key already exists in TrackingEventDetails, it will be replaced.
Usage: trackingEventDetails.Add('active-time', 2).Add('unit': 'seconds')
func (TrackingEventDetails) Attribute ¶ added in v1.14.0
func (t TrackingEventDetails) Attribute(key string) interface{}
Attribute retrieves the attribute with the given key.
func (TrackingEventDetails) Attributes ¶ added in v1.14.0
func (t TrackingEventDetails) Attributes() map[string]interface{}
Attributes return a map contains the key-value pairs stored in TrackingEventDetails.
func (TrackingEventDetails) Copy ¶ added in v1.14.0
func (t TrackingEventDetails) Copy(value float64) TrackingEventDetails
Copy return a new TrackingEventDetails with new value. It will copy details of old TrackingEventDetails into the new one to ensure the immutability.
func (TrackingEventDetails) Value ¶ added in v1.14.0
func (t TrackingEventDetails) Value() float64
Value retrieves the value of TrackingEventDetails.
type UnimplementedHook ¶
type UnimplementedHook struct{}
UnimplementedHook implements all hook methods with empty functions Include UnimplementedHook in your hook struct to avoid defining empty functions e.g.
type MyHook struct { UnimplementedHook }
func (UnimplementedHook) After ¶
func (UnimplementedHook) After(context.Context, HookContext, InterfaceEvaluationDetails, HookHints) error
func (UnimplementedHook) Before ¶
func (UnimplementedHook) Before(context.Context, HookContext, HookHints) (*EvaluationContext, error)
func (UnimplementedHook) Error ¶
func (UnimplementedHook) Error(context.Context, HookContext, error, HookHints)
func (UnimplementedHook) Finally ¶
func (UnimplementedHook) Finally(context.Context, HookContext, HookHints)