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

chore: renaming of evaluation components #1064

Merged
merged 2 commits into from
Dec 14, 2023
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"errors"
Expand All @@ -11,7 +11,7 @@ import (

const FractionEvaluationName = "fractional"

type FractionalEvaluator struct {
type Fractional struct {
Logger *logger.Logger
}

Expand All @@ -20,11 +20,11 @@ type fractionalEvaluationDistribution struct {
percentage int
}

func NewFractionalEvaluator(logger *logger.Logger) *FractionalEvaluator {
return &FractionalEvaluator{Logger: logger}
func NewFractional(logger *logger.Logger) *Fractional {
return &Fractional{Logger: logger}
}

func (fe *FractionalEvaluator) FractionalEvaluation(values, data any) any {
func (fe *Fractional) Evaluate(values, data any) any {
valueToDistribute, feDistributions, err := parseFractionalEvaluationData(values, data)
if err != nil {
fe.Logger.Error(fmt.Sprintf("parse fractional evaluation data: %v", err))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"testing"
Expand Down Expand Up @@ -323,12 +323,12 @@ func TestFractionalEvaluation(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
log := logger.NewLogger(nil, false)
je := NewJSONEvaluator(
je := NewJSON(
log,
store.NewFlags(),
WithEvaluator(
FractionEvaluationName,
NewFractionalEvaluator(log).FractionalEvaluation,
NewFractional(log).Evaluate,
),
)
je.store.Flags = tt.flags.Flags
Expand Down Expand Up @@ -457,12 +457,12 @@ func BenchmarkFractionalEvaluation(b *testing.B) {
for name, tt := range tests {
b.Run(name, func(b *testing.B) {
log := logger.NewLogger(nil, false)
je := NewJSONEvaluator(
je := NewJSON(
log,
&store.Flags{Flags: tt.flags.Flags},
WithEvaluator(
FractionEvaluationName,
NewFractionalEvaluator(log).FractionalEvaluation,
NewFractional(log).Evaluate,
),
)
for i := 0; i < b.N; i++ {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"fmt"
Expand Down
36 changes: 18 additions & 18 deletions core/pkg/eval/json_evaluator.go → core/pkg/evaluator/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"bytes"
Expand Down Expand Up @@ -50,7 +50,7 @@ func init() {
type variantEvaluator func(string, string, map[string]any) (
variant string, variants map[string]interface{}, reason string, metadata map[string]interface{}, error error)

type JSONEvaluator struct {
type JSON struct {
store *store.Flags
Logger *logger.Logger
jsonEvalTracer trace.Tracer
Expand All @@ -64,16 +64,16 @@ const (
Disabled = "DISABLED"
)

type JSONEvaluatorOption func(je *JSONEvaluator)
type JSONEvaluatorOption func(je *JSON)

func WithEvaluator(name string, evalFunc func(interface{}, interface{}) interface{}) JSONEvaluatorOption {
return func(_ *JSONEvaluator) {
return func(_ *JSON) {
jsonlogic.AddOperator(name, evalFunc)
}
}

func NewJSONEvaluator(logger *logger.Logger, s *store.Flags, opts ...JSONEvaluatorOption) *JSONEvaluator {
ev := JSONEvaluator{
func NewJSON(logger *logger.Logger, s *store.Flags, opts ...JSONEvaluatorOption) *JSON {
ev := JSON{
Logger: logger.WithFields(
zap.String("component", "evaluator"),
zap.String("evaluator", "json"),
Expand All @@ -89,15 +89,15 @@ func NewJSONEvaluator(logger *logger.Logger, s *store.Flags, opts ...JSONEvaluat
return &ev
}

func (je *JSONEvaluator) GetState() (string, error) {
func (je *JSON) GetState() (string, error) {
s, err := je.store.String()
if err != nil {
return "", fmt.Errorf("unable to fetch evaluator state: %w", err)
}
return s, nil
}

func (je *JSONEvaluator) SetState(payload sync.DataSync) (map[string]interface{}, bool, error) {
func (je *JSON) SetState(payload sync.DataSync) (map[string]interface{}, bool, error) {
_, span := je.jsonEvalTracer.Start(
context.Background(),
"flagSync",
Expand Down Expand Up @@ -136,7 +136,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 map[string]any) []AnyValue {
func (je *JSON) ResolveAllValues(ctx context.Context, reqID string, context map[string]any) []AnyValue {
_, span := je.jsonEvalTracer.Start(ctx, "resolveAll")
defer span.End()

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

func (je *JSONEvaluator) ResolveBooleanValue(
func (je *JSON) ResolveBooleanValue(
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value bool,
variant string,
Expand All @@ -207,7 +207,7 @@ func (je *JSONEvaluator) ResolveBooleanValue(
return resolve[bool](reqID, flagKey, context, je.evaluateVariant)
}

func (je *JSONEvaluator) ResolveStringValue(
func (je *JSON) ResolveStringValue(
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value string,
variant string,
Expand All @@ -222,7 +222,7 @@ func (je *JSONEvaluator) ResolveStringValue(
return resolve[string](reqID, flagKey, context, je.evaluateVariant)
}

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

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

func (je *JSONEvaluator) ResolveObjectValue(
func (je *JSON) ResolveObjectValue(
ctx context.Context, reqID string, flagKey string, context map[string]any) (
value map[string]any,
variant string,
Expand Down Expand Up @@ -289,7 +289,7 @@ func resolve[T constraints](reqID string, key string, context map[string]any, va

// runs the rules (if defined) to determine the variant, otherwise falling through to the default
// nolint: funlen
func (je *JSONEvaluator) evaluateVariant(reqID string, flagKey string, context map[string]any) (
func (je *JSON) evaluateVariant(reqID string, flagKey string, context map[string]any) (
variant string, variants map[string]interface{}, reason string, metadata map[string]interface{}, err error,
) {
metadata = map[string]interface{}{}
Expand Down Expand Up @@ -362,7 +362,7 @@ func (je *JSONEvaluator) evaluateVariant(reqID string, flagKey string, context m
return flag.DefaultVariant, flag.Variants, model.StaticReason, metadata, nil
}

func (je *JSONEvaluator) setFlagdProperties(
func (je *JSON) setFlagdProperties(
context map[string]any,
properties flagdProperties,
) map[string]any {
Expand Down Expand Up @@ -401,7 +401,7 @@ func getFlagdProperties(context map[string]any) (flagdProperties, bool) {
}

// configToFlags convert string configurations to flags and store them to pointer newFlags
func (je *JSONEvaluator) configToFlags(config string, newFlags *Flags) error {
func (je *JSON) configToFlags(config string, newFlags *Flags) error {
schemaLoader := gojsonschema.NewStringLoader(schema.FlagdDefinitions)
flagStringLoader := gojsonschema.NewStringLoader(config)

Expand Down Expand Up @@ -438,7 +438,7 @@ func validateDefaultVariants(flags *Flags) error {
return nil
}

func (je *JSONEvaluator) transposeEvaluators(state string) (string, error) {
func (je *JSON) transposeEvaluators(state string) (string, error) {
var evaluators Evaluators
if err := json.Unmarshal([]byte(state), &evaluators); err != nil {
return "", fmt.Errorf("unmarshal: %w", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eval
package evaluator

import (
"encoding/json"
Expand Down
Loading
Loading