Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: defined type for provider interface evaluation context #74

Merged
merged 5 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require (
golang.org/x/text v0.3.7
)

require github.com/go-logr/logr v1.2.3 // indirect
require github.com/go-logr/logr v1.2.3
4 changes: 2 additions & 2 deletions pkg/openfeature/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ func (c Client) evaluate(
return evalDetails, nil
}

func flattenContext(evalCtx EvaluationContext) map[string]interface{} {
flatCtx := map[string]interface{}{}
func flattenContext(evalCtx EvaluationContext) FlattenedContext {
flatCtx := FlattenedContext{}
if evalCtx.Attributes != nil {
flatCtx = evalCtx.Attributes
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/openfeature/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestRequirement_1_4_9(t *testing.T) {
func TestFlattenContext(t *testing.T) {
tests := map[string]struct {
inCtx EvaluationContext
outCtx map[string]interface{}
outCtx FlattenedContext
}{
"happy path": {
inCtx: EvaluationContext{
Expand All @@ -427,7 +427,7 @@ func TestFlattenContext(t *testing.T) {
},
TargetingKey: "user",
},
outCtx: map[string]interface{}{
outCtx: FlattenedContext{
TargetingKey: "user",
"1": "string",
"2": 0.01,
Expand All @@ -442,7 +442,7 @@ func TestFlattenContext(t *testing.T) {
"3": false,
},
},
outCtx: map[string]interface{}{
outCtx: FlattenedContext{
"1": "string",
"2": 0.01,
"3": false,
Expand All @@ -458,7 +458,7 @@ func TestFlattenContext(t *testing.T) {
"3": false,
},
},
outCtx: map[string]interface{}{
outCtx: FlattenedContext{
TargetingKey: "user",
"1": "string",
"2": 0.01,
Expand All @@ -469,7 +469,7 @@ func TestFlattenContext(t *testing.T) {
inCtx: EvaluationContext{
TargetingKey: "user",
},
outCtx: map[string]interface{}{
outCtx: FlattenedContext{
TargetingKey: "user",
},
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/openfeature/noop_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (e NoopProvider) Metadata() Metadata {
}

// BooleanEvaluation returns a boolean flag.
func (e NoopProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx map[string]interface{}) BoolResolutionDetail {
func (e NoopProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx FlattenedContext) BoolResolutionDetail {
return BoolResolutionDetail{
Value: defaultValue,
ResolutionDetail: ResolutionDetail{
Expand All @@ -22,7 +22,7 @@ func (e NoopProvider) BooleanEvaluation(ctx context.Context, flag string, defaul
}

// StringEvaluation returns a string flag.
func (e NoopProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail {
func (e NoopProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx FlattenedContext) StringResolutionDetail {
return StringResolutionDetail{
Value: defaultValue,
ResolutionDetail: ResolutionDetail{
Expand All @@ -33,7 +33,7 @@ func (e NoopProvider) StringEvaluation(ctx context.Context, flag string, default
}

// FloatEvaluation returns a float flag.
func (e NoopProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail {
func (e NoopProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx FlattenedContext) FloatResolutionDetail {
return FloatResolutionDetail{
Value: defaultValue,
ResolutionDetail: ResolutionDetail{
Expand All @@ -44,7 +44,7 @@ func (e NoopProvider) FloatEvaluation(ctx context.Context, flag string, defaultV
}

// IntEvaluation returns an int flag.
func (e NoopProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail {
func (e NoopProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx FlattenedContext) IntResolutionDetail {
return IntResolutionDetail{
Value: defaultValue,
ResolutionDetail: ResolutionDetail{
Expand All @@ -55,7 +55,7 @@ func (e NoopProvider) IntEvaluation(ctx context.Context, flag string, defaultVal
}

// ObjectEvaluation returns an object flag
func (e NoopProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail {
func (e NoopProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx FlattenedContext) InterfaceResolutionDetail {
return InterfaceResolutionDetail{
Value: defaultValue,
ResolutionDetail: ResolutionDetail{
Expand Down
14 changes: 9 additions & 5 deletions pkg/openfeature/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ const (
TargetingKey string = "targetingKey" // evaluation context map key. The targeting key uniquely identifies the subject (end-user, or client service) of a flag evaluation.
)

// 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 FlattenedContext map[string]interface{}

// FeatureProvider interface defines a set of functions that can be called in order to evaluate a flag.
// vendors should implement
type FeatureProvider interface {
Metadata() Metadata
BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx map[string]interface{}) BoolResolutionDetail
StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail
FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail
IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail
ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail
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
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/openfeature/provider_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pkg/openfeature/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func TestRequirement_2_2(t *testing.T) {
mockProvider := NewMockFeatureProvider(ctrl)

type requirements interface {
BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx map[string]interface{}) BoolResolutionDetail
StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx map[string]interface{}) StringResolutionDetail
FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx map[string]interface{}) FloatResolutionDetail
IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx map[string]interface{}) IntResolutionDetail
ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx map[string]interface{}) InterfaceResolutionDetail
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
}

var mockProviderI interface{} = mockProvider
Expand Down