Skip to content

Commit

Permalink
refactor: remove protobuf dependency from eval package (open-feature#701
Browse files Browse the repository at this point in the history
)

Signed-off-by: James Milligan <[email protected]>
  • Loading branch information
james-milligan authored Jun 21, 2023
1 parent 56df71a commit 34ffafd
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 282 deletions.
95 changes: 36 additions & 59 deletions core/pkg/eval/fractional_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/open-feature/flagd/core/pkg/logger"
"github.com/open-feature/flagd/core/pkg/model"
"github.com/open-feature/flagd/core/pkg/store"
"google.golang.org/protobuf/types/known/structpb"
)

func TestFractionalEvaluation(t *testing.T) {
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestFractionalEvaluation(t *testing.T) {
tests := map[string]struct {
flags Flags
flagKey string
context *structpb.Struct
context map[string]any
expectedValue string
expectedVariant string
expectedReason string
Expand All @@ -67,47 +66,39 @@ func TestFractionalEvaluation(t *testing.T) {
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "yellow",
expectedValue: "#FFFF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedReason: model.TargetingMatchReason,
Expand Down Expand Up @@ -154,11 +145,9 @@ func TestFractionalEvaluation(t *testing.T) {
},
},
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
Expand Down Expand Up @@ -200,7 +189,7 @@ func TestFractionalEvaluation(t *testing.T) {
},
},
flagKey: "headerColor",
context: &structpb.Struct{},
context: map[string]any{},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.DefaultReason,
Expand Down Expand Up @@ -230,11 +219,9 @@ func TestFractionalEvaluation(t *testing.T) {
},
},
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.DefaultReason,
Expand Down Expand Up @@ -268,11 +255,9 @@ func TestFractionalEvaluation(t *testing.T) {
},
},
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.DefaultReason,
Expand Down Expand Up @@ -364,7 +349,7 @@ func BenchmarkFractionalEvaluation(b *testing.B) {
tests := map[string]struct {
flags Flags
flagKey string
context *structpb.Struct
context map[string]any
expectedValue string
expectedVariant string
expectedReason string
Expand All @@ -373,47 +358,39 @@ func BenchmarkFractionalEvaluation(b *testing.B) {
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "yellow",
expectedValue: "#FFFF00",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected]": {
flags: flags,
flagKey: "headerColor",
context: &structpb.Struct{Fields: map[string]*structpb.Value{
"email": {Kind: &structpb.Value_StringValue{
StringValue: "[email protected]",
}},
}},
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedReason: model.TargetingMatchReason,
Expand Down
13 changes: 6 additions & 7 deletions core/pkg/eval/ievaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/open-feature/flagd/core/pkg/sync"
"google.golang.org/protobuf/types/known/structpb"
)

type AnyValue struct {
Expand Down Expand Up @@ -37,29 +36,29 @@ type IEvaluator interface {
ctx context.Context,
reqID string,
flagKey string,
context *structpb.Struct) (value bool, variant string, reason string, err error)
context map[string]any) (value bool, variant string, reason string, err error)
ResolveStringValue(
ctx context.Context,
reqID string,
flagKey string,
context *structpb.Struct) (value string, variant string, reason string, err error)
context map[string]any) (value string, variant string, reason string, err error)
ResolveIntValue(
ctx context.Context,
reqID string,
flagKey string,
context *structpb.Struct) (value int64, variant string, reason string, err error)
context map[string]any) (value int64, variant string, reason string, err error)
ResolveFloatValue(
ctx context.Context,
reqID string,
flagKey string,
context *structpb.Struct) (value float64, variant string, reason string, err error)
context map[string]any) (value float64, variant string, reason string, err error)
ResolveObjectValue(
ctx context.Context,
reqID string,
flagKey string,
context *structpb.Struct) (value map[string]any, variant string, reason string, err error)
context map[string]any) (value map[string]any, variant string, reason string, err error)
ResolveAllValues(
ctx context.Context,
reqID string,
context *structpb.Struct) (values []AnyValue)
context map[string]any) (values []AnyValue)
}
19 changes: 9 additions & 10 deletions core/pkg/eval/json_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)

var regBrace *regexp.Regexp
Expand Down Expand Up @@ -117,7 +116,7 @@ func (je *JSONEvaluator) SetState(payload sync.DataSync) (map[string]interface{}
return events, reSync, nil
}

func (je *JSONEvaluator) ResolveAllValues(ctx context.Context, reqID string, context *structpb.Struct) []AnyValue {
func (je *JSONEvaluator) ResolveAllValues(ctx context.Context, reqID string, context map[string]any) []AnyValue {
_, span := je.jsonEvalTracer.Start(ctx, "resolveAll")
defer span.End()

Expand Down Expand Up @@ -177,7 +176,7 @@ func (je *JSONEvaluator) ResolveAllValues(ctx context.Context, reqID string, con
}

func (je *JSONEvaluator) ResolveBooleanValue(
ctx context.Context, reqID string, flagKey string, context *structpb.Struct) (
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value bool,
variant string,
reason string,
Expand All @@ -192,7 +191,7 @@ func (je *JSONEvaluator) ResolveBooleanValue(
}

func (je *JSONEvaluator) ResolveStringValue(
ctx context.Context, reqID string, flagKey string, context *structpb.Struct) (
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value string,
variant string,
reason string,
Expand All @@ -207,7 +206,7 @@ func (je *JSONEvaluator) ResolveStringValue(
}

func (je *JSONEvaluator) ResolveFloatValue(
ctx context.Context, reqID string, flagKey string, context *structpb.Struct) (
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value float64,
variant string,
reason string,
Expand All @@ -222,7 +221,7 @@ func (je *JSONEvaluator) ResolveFloatValue(
return
}

func (je *JSONEvaluator) ResolveIntValue(ctx context.Context, reqID string, flagKey string, context *structpb.Struct) (
func (je *JSONEvaluator) ResolveIntValue(ctx context.Context, reqID string, flagKey string, context map[string]any) (
value int64,
variant string,
reason string,
Expand All @@ -240,7 +239,7 @@ func (je *JSONEvaluator) ResolveIntValue(ctx context.Context, reqID string, flag
}

func (je *JSONEvaluator) ResolveObjectValue(
ctx context.Context, reqID string, flagKey string, context *structpb.Struct) (
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value map[string]any,
variant string,
reason string,
Expand All @@ -254,8 +253,8 @@ func (je *JSONEvaluator) ResolveObjectValue(
return resolve[map[string]any](reqID, flagKey, context, je.evaluateVariant, flag.Variants)
}

func resolve[T constraints](reqID string, key string, context *structpb.Struct,
variantEval func(string, string, *structpb.Struct) (string, string, error),
func resolve[T constraints](reqID string, key string, context map[string]any,
variantEval func(string, string, map[string]any) (string, string, error),
variants map[string]any) (
value T,
variant string,
Expand All @@ -280,7 +279,7 @@ func resolve[T constraints](reqID string, key string, context *structpb.Struct,
func (je *JSONEvaluator) evaluateVariant(
reqID string,
flagKey string,
context *structpb.Struct,
context map[string]any,
) (variant string, reason string, err error) {
flag, ok := je.store.Get(flagKey)
if !ok {
Expand Down
Loading

0 comments on commit 34ffafd

Please sign in to comment.