openfeature

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package openfeature provides global access to the OpenFeature API.

Index

Examples

Constants

View Source
const (
	DISABLED        string = "disabled"     // variant returned because feature is disabled
	TARGETING_MATCH string = "target match" // variant returned because matched target rule
	DEFAULT         string = "default"      // variant returned the default
	UNKNOWN         string = "unknown"      // variant returned for unknown reason
	ERROR           string = "error"        // variant returned due to 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

This section is empty.

Functions

func AddHooks

func AddHooks(hooks ...Hook)

AddHooks appends to the collection of any previously added hooks

func SetEvaluationContext

func SetEvaluationContext(evalCtx EvaluationContext)

SetEvaluationContext sets the global evaluation context.

func SetLogger

func SetLogger(l logr.Logger)

SetLogger sets the global logger.

func SetProvider

func SetProvider(provider FeatureProvider)

SetProvider sets the global provider.

Types

type BoolResolutionDetail

type BoolResolutionDetail struct {
	Value bool
	ResolutionDetail
}

BoolResolutionDetail provides a resolution detail with boolean type

type Client

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

Client implements the behaviour required of an openfeature client

func NewClient

func NewClient(name string) *Client

NewClient returns a new Client. Name is a unique identifier for this client

Example
package main

import (
	"fmt"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	fmt.Printf("Client Name: %s", client.Metadata().Name())
}
Output:

Client Name: example-client

func (*Client) AddHooks

func (c *Client) AddHooks(hooks ...Hook)

AddHooks appends to the client's collection of any previously added hooks

func (Client) BooleanValue

func (c Client) BooleanValue(flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (bool, error)

BooleanValue return boolean evaluation for flag

Example
package main

import (
	"fmt"
	"log"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	value, err := client.BooleanValue(
		"test-flag", true, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
	)
	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(flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)

BooleanValueDetails return boolean evaluation for flag

func (*Client) EvaluationContext

func (c *Client) EvaluationContext() EvaluationContext

EvaluationContext returns the client's evaluation context

func (Client) FloatValue

func (c Client) FloatValue(flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (float64, error)

FloatValue return float evaluation for flag

Example
package main

import (
	"fmt"
	"log"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	value, err := client.FloatValue(
		"test-flag", 0.55, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
	)
	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(flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)

FloatValueDetails return float evaluation for flag

func (Client) IntValue

func (c Client) IntValue(flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (int64, error)

IntValue return int evaluation for flag

Example
package main

import (
	"fmt"
	"log"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	value, err := client.IntValue(
		"test-flag", 3, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
	)
	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(flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)

IntValueDetails return int evaluation for flag

func (Client) Metadata

func (c Client) Metadata() ClientMetadata

Metadata returns the client's metadata

func (Client) ObjectValue

func (c Client) ObjectValue(flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (interface{}, error)

ObjectValue return object evaluation for flag

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	value, err := client.ObjectValue(
		"test-flag", map[string]string{"foo": "bar"}, openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
	)
	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(flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)

ObjectValueDetails return object evaluation for flag

func (*Client) SetEvaluationContext

func (c *Client) SetEvaluationContext(evalCtx EvaluationContext)

SetEvaluationContext sets the client's evaluation context

func (Client) StringValue

func (c Client) StringValue(flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (string, error)

StringValue return string evaluation for flag

Example
package main

import (
	"fmt"
	"log"

	"github.com/open-feature/golang-sdk/pkg/openfeature"
)

func main() {
	client := openfeature.NewClient("example-client")
	value, err := client.StringValue(
		"test-flag", "openfeature", openfeature.EvaluationContext{}, openfeature.EvaluationOptions{},
	)
	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(flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)

StringValueDetails return string evaluation for flag

func (*Client) WithLogger

func (c *Client) WithLogger(l logr.Logger) *Client

WithLogger sets the logger of the client

type ClientMetadata

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

ClientMetadata provides a client's metadata

func (ClientMetadata) Name

func (cm ClientMetadata) Name() string

Name returns the client's name

type EvaluationContext

type EvaluationContext struct {
	TargetingKey string                 `json:"targetingKey"` // uniquely identifying the subject (end-user, or client service) of a flag evaluation
	Attributes   map[string]interface{} `json:"attributes"`
}

EvaluationContext https://github.com/open-feature/spec/blob/main/specification/evaluation-context/evaluation-context.md

type EvaluationDetails

type EvaluationDetails struct {
	FlagKey  string
	FlagType Type
	InterfaceResolutionDetail
}

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 NewEvaluationOptions

func NewEvaluationOptions(hooks []Hook, hookHints HookHints) EvaluationOptions

NewEvaluationOptions constructs an EvaluationOptions

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 FeatureProvider

type FeatureProvider interface {
	Metadata() Metadata
	BooleanEvaluation(flag string, defaultValue bool, evalCtx map[string]interface{}) BoolResolutionDetail
	StringEvaluation(flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail
	FloatEvaluation(flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail
	IntEvaluation(flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail
	ObjectEvaluation(flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail
	Hooks() []Hook
}

FeatureProvider interface defines a set of functions that can be called in order to evaluate a flag. vendors should implement

type FloatResolutionDetail

type FloatResolutionDetail struct {
	Value float64
	ResolutionDetail
}

FloatResolutionDetail provides a resolution detail with float64 type

type Hook

type Hook interface {
	Before(hookContext HookContext, hookHints HookHints) (*EvaluationContext, error)
	After(hookContext HookContext, flagEvaluationDetails EvaluationDetails, hookHints HookHints) error
	Error(hookContext HookContext, err error, hookHints HookHints)
	Finally(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 (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

func NewHookHints(mapOfHints map[string]interface{}) HookHints

NewHookHints constructs HookHints

func (HookHints) Value

func (h HookHints) Value(key string) interface{}

Value returns the value at the given key in the underlying map. Maintains immutability of the map.

type IClient

type IClient interface {
	Metadata() ClientMetadata
	AddHooks(hooks ...Hook)
	SetEvaluationContext(evalCtx EvaluationContext)
	EvaluationContext() EvaluationContext
	BooleanValue(flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (bool, error)
	StringValue(flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (string, error)
	FloatValue(flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (float64, error)
	IntValue(flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (int64, error)
	ObjectValue(flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (interface{}, error)
	BooleanValueDetails(flag string, defaultValue bool, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
	StringValueDetails(flag string, defaultValue string, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
	FloatValueDetails(flag string, defaultValue float64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
	IntValueDetails(flag string, defaultValue int64, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
	ObjectValueDetails(flag string, defaultValue interface{}, evalCtx EvaluationContext, options EvaluationOptions) (EvaluationDetails, error)
}

IClient defines the behaviour required of an openfeature client

type IntResolutionDetail

type IntResolutionDetail struct {
	Value int64
	ResolutionDetail
}

IntResolutionDetail provides a resolution detail with int64 type

type InterfaceResolutionDetail added in v0.3.0

type InterfaceResolutionDetail struct {
	Value interface{}
	ResolutionDetail
}

InterfaceResolutionDetail provides a resolution detail with interface{} type

type Metadata

type Metadata struct {
	Name string
}

Metadata provides provider name

func ProviderMetadata

func ProviderMetadata() Metadata

ProviderMetadata returns the global provider's metadata

type NoopProvider

type NoopProvider struct{}

NoopProvider implements the FeatureProvider interface and provides functions for evaluating flags

func (NoopProvider) BooleanEvaluation

func (e NoopProvider) BooleanEvaluation(flag string, defaultValue bool, evalCtx map[string]interface{}) BoolResolutionDetail

BooleanEvaluation returns a boolean flag.

func (NoopProvider) FloatEvaluation

func (e NoopProvider) FloatEvaluation(flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail

FloatEvaluation returns a float flag.

func (NoopProvider) Hooks

func (e NoopProvider) Hooks() []Hook

Hooks returns hooks

func (NoopProvider) IntEvaluation

func (e NoopProvider) IntEvaluation(flag string, defaultValue int64, evalCtx map[string]interface{}) 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(flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail

ObjectEvaluation returns an object flag

func (NoopProvider) StringEvaluation

func (e NoopProvider) StringEvaluation(flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail

StringEvaluation returns a string flag.

type ResolutionDetail

type ResolutionDetail struct {
	ErrorCode string
	Reason    string
	Variant   string
}

ResolutionDetail 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 golang for now we will have type specific resolution detail

func (ResolutionDetail) Error

func (resolution ResolutionDetail) Error() error

type StringResolutionDetail

type StringResolutionDetail struct {
	Value string
	ResolutionDetail
}

StringResolutionDetail provides a resolution detail with string type

type Type

type Type int64

Type represents the type of a flag

const (
	Boolean Type = iota
	String
	Float
	Int
	Object
)

func (Type) String

func (t Type) String() string

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 (u UnimplementedHook) After(hookContext HookContext, flagEvaluationDetails EvaluationDetails, hookHints HookHints) error

func (UnimplementedHook) Before

func (u UnimplementedHook) Before(hookContext HookContext, hookHints HookHints) (*EvaluationContext, error)

func (UnimplementedHook) Error

func (u UnimplementedHook) Error(hookContext HookContext, err error, hookHints HookHints)

func (UnimplementedHook) Finally

func (u UnimplementedHook) Finally(hookContext HookContext, hookHints HookHints)

Jump to

Keyboard shortcuts

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