Skip to content
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
6 changes: 6 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type AdvancedConfig struct {
AuthSpecVersion string
FlagsSpecVersion string
LargeSegment *LargeSegmentConfig
RulesConfig *RulesConfig
}

type LargeSegmentConfig struct {
Expand All @@ -99,3 +100,8 @@ type LargeSegmentConfig struct {
QueueSize int
RefreshRate int
}

type RulesConfig struct {
FeatureFlagRules []string
RuleBasedSegmentRules []string
}
17 changes: 13 additions & 4 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import (
"github.com/splitio/go-toolkit/v5/logging"
)

var syncProxyFeatureFlagsRules = []string{grammar.MatcherTypeAllKeys, grammar.MatcherTypeInSegment, grammar.MatcherTypeWhitelist, grammar.MatcherTypeEqualTo, grammar.MatcherTypeGreaterThanOrEqualTo, grammar.MatcherTypeLessThanOrEqualTo, grammar.MatcherTypeBetween,
grammar.MatcherTypeEqualToSet, grammar.MatcherTypePartOfSet, grammar.MatcherTypeContainsAllOfSet, grammar.MatcherTypeContainsAnyOfSet, grammar.MatcherTypeStartsWith, grammar.MatcherTypeEndsWith, grammar.MatcherTypeContainsString, grammar.MatcherTypeInSplitTreatment,
grammar.MatcherTypeEqualToBoolean, grammar.MatcherTypeMatchesString, grammar.MatcherEqualToSemver, grammar.MatcherTypeGreaterThanOrEqualToSemver, grammar.MatcherTypeLessThanOrEqualToSemver, grammar.MatcherTypeBetweenSemver, grammar.MatcherTypeInListSemver, grammar.MatcherTypeInLargeSegment,
grammar.MatcherTypeInRuleBasedSegment}
var syncProxyRuleBasedSegmentRules = []string{grammar.MatcherTypeAllKeys, grammar.MatcherTypeInSegment, grammar.MatcherTypeWhitelist, grammar.MatcherTypeEqualTo, grammar.MatcherTypeGreaterThanOrEqualTo, grammar.MatcherTypeLessThanOrEqualTo, grammar.MatcherTypeBetween,
grammar.MatcherTypeEqualToSet, grammar.MatcherTypePartOfSet, grammar.MatcherTypeContainsAllOfSet, grammar.MatcherTypeContainsAnyOfSet, grammar.MatcherTypeStartsWith, grammar.MatcherTypeEndsWith, grammar.MatcherTypeContainsString,
grammar.MatcherTypeEqualToBoolean, grammar.MatcherTypeMatchesString, grammar.MatcherEqualToSemver, grammar.MatcherTypeGreaterThanOrEqualToSemver, grammar.MatcherTypeLessThanOrEqualToSemver, grammar.MatcherTypeBetweenSemver, grammar.MatcherTypeInListSemver, grammar.MatcherTypeInLargeSegment,
grammar.MatcherTypeInRuleBasedSegment}

func TestProperHashFunctionIsUsed(t *testing.T) {
eng := Engine{}

Expand Down Expand Up @@ -86,7 +95,7 @@ func TestTreatmentOnTrafficAllocation1(t *testing.T) {
},
}

split := grammar.NewSplit(&splitDTO, nil, logger)
split := grammar.NewSplit(&splitDTO, nil, logger, grammar.NewRuleBuilder(nil, nil, nil, syncProxyFeatureFlagsRules, syncProxyRuleBasedSegmentRules, logger))

eng := Engine{}
eng.logger = logger
Expand Down Expand Up @@ -135,7 +144,7 @@ func TestTreatmentOnTrafficAllocation99(t *testing.T) {
},
}

split := grammar.NewSplit(&splitDTO, nil, logger)
split := grammar.NewSplit(&splitDTO, nil, logger, grammar.NewRuleBuilder(nil, nil, nil, syncProxyFeatureFlagsRules, syncProxyRuleBasedSegmentRules, logger))

eng := Engine{}
eng.logger = logger
Expand Down Expand Up @@ -237,7 +246,7 @@ func TestEvaluations(t *testing.T) {
t.Error("Data was not added for testing consistency")
}

split := grammar.NewSplit(&splitDTO, nil, logger)
split := grammar.NewSplit(&splitDTO, nil, logger, grammar.NewRuleBuilder(nil, nil, nil, syncProxyFeatureFlagsRules, syncProxyRuleBasedSegmentRules, logger))

eng := Engine{}
eng.logger = logger
Expand Down Expand Up @@ -267,7 +276,7 @@ func TestNoConditionMatched(t *testing.T) {
Conditions: []dtos.ConditionDTO{},
}

split := grammar.NewSplit(&splitDTO, nil, logger)
split := grammar.NewSplit(&splitDTO, nil, logger, grammar.NewRuleBuilder(nil, nil, nil, syncProxyFeatureFlagsRules, syncProxyRuleBasedSegmentRules, logger))

eng := Engine{}
eng.logger = logger
Expand Down
8 changes: 7 additions & 1 deletion engine/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Evaluator struct {
ruleBasedSegmentStorage storage.RuleBasedSegmentStorageConsumer
eng *engine.Engine
logger logging.LoggerInterface
featureFlagRules []string
ruleBasedSegmentRules []string
}

// NewEvaluator instantiates an Evaluator struct and returns a reference to it
Expand All @@ -52,13 +54,17 @@ func NewEvaluator(
ruleBasedSegmentStorage storage.RuleBasedSegmentStorageConsumer,
eng *engine.Engine,
logger logging.LoggerInterface,
featureFlagRules []string,
ruleBasedSegmentRules []string,
) *Evaluator {
return &Evaluator{
splitStorage: splitStorage,
segmentStorage: segmentStorage,
eng: eng,
logger: logger,
ruleBasedSegmentStorage: ruleBasedSegmentStorage,
featureFlagRules: featureFlagRules,
ruleBasedSegmentRules: ruleBasedSegmentRules,
}
}

Expand All @@ -74,7 +80,7 @@ func (e *Evaluator) evaluateTreatment(key string, bucketingKey string, featureFl
ctx.AddDependency("evaluator", e)
ctx.AddDependency("ruleBasedSegmentStorage", e.ruleBasedSegmentStorage)

split := grammar.NewSplit(splitDto, ctx, e.logger)
split := grammar.NewSplit(splitDto, ctx, e.logger, grammar.NewRuleBuilder(ctx, e.segmentStorage, e.ruleBasedSegmentStorage, e.featureFlagRules, e.ruleBasedSegmentRules, e.logger))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about evaluator receiving the entire ruleBuilder instead of passing the config parameters and creating the ruleBuilder here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, we need to remove the context.


if split.Killed() {
e.logger.Warning(fmt.Sprintf(
Expand Down
42 changes: 34 additions & 8 deletions engine/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/splitio/go-split-commons/v6/dtos"
"github.com/splitio/go-split-commons/v6/engine/grammar"
"github.com/splitio/go-split-commons/v6/flagsets"
"github.com/splitio/go-split-commons/v6/storage/inmemory/mutexmap"
"github.com/splitio/go-split-commons/v6/storage/mocks"
Expand All @@ -12,6 +13,15 @@ import (
"github.com/splitio/go-toolkit/v5/logging"
)

var syncProxyFeatureFlagsRules = []string{grammar.MatcherTypeAllKeys, grammar.MatcherTypeInSegment, grammar.MatcherTypeWhitelist, grammar.MatcherTypeEqualTo, grammar.MatcherTypeGreaterThanOrEqualTo, grammar.MatcherTypeLessThanOrEqualTo, grammar.MatcherTypeBetween,
grammar.MatcherTypeEqualToSet, grammar.MatcherTypePartOfSet, grammar.MatcherTypeContainsAllOfSet, grammar.MatcherTypeContainsAnyOfSet, grammar.MatcherTypeStartsWith, grammar.MatcherTypeEndsWith, grammar.MatcherTypeContainsString, grammar.MatcherTypeInSplitTreatment,
grammar.MatcherTypeEqualToBoolean, grammar.MatcherTypeMatchesString, grammar.MatcherEqualToSemver, grammar.MatcherTypeGreaterThanOrEqualToSemver, grammar.MatcherTypeLessThanOrEqualToSemver, grammar.MatcherTypeBetweenSemver, grammar.MatcherTypeInListSemver, grammar.MatcherTypeInLargeSegment,
grammar.MatcherTypeInRuleBasedSegment}
var syncProxyRuleBasedSegmentRules = []string{grammar.MatcherTypeAllKeys, grammar.MatcherTypeInSegment, grammar.MatcherTypeWhitelist, grammar.MatcherTypeEqualTo, grammar.MatcherTypeGreaterThanOrEqualTo, grammar.MatcherTypeLessThanOrEqualTo, grammar.MatcherTypeBetween,
grammar.MatcherTypeEqualToSet, grammar.MatcherTypePartOfSet, grammar.MatcherTypeContainsAllOfSet, grammar.MatcherTypeContainsAnyOfSet, grammar.MatcherTypeStartsWith, grammar.MatcherTypeEndsWith, grammar.MatcherTypeContainsString,
grammar.MatcherTypeEqualToBoolean, grammar.MatcherTypeMatchesString, grammar.MatcherEqualToSemver, grammar.MatcherTypeGreaterThanOrEqualToSemver, grammar.MatcherTypeLessThanOrEqualToSemver, grammar.MatcherTypeBetweenSemver, grammar.MatcherTypeInListSemver, grammar.MatcherTypeInLargeSegment,
grammar.MatcherTypeInRuleBasedSegment}

type mockStorage struct{}

var mysplittest = &dtos.SplitDTO{
Expand Down Expand Up @@ -236,7 +246,9 @@ func TestSplitWithoutConfigurations(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
result := evaluator.EvaluateFeature(key, &key, "mysplittest", nil)
Expand All @@ -258,7 +270,9 @@ func TestSplitWithtConfigurations(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
result := evaluator.EvaluateFeature(key, &key, "mysplittest2", nil)
Expand All @@ -280,7 +294,9 @@ func TestSplitWithtConfigurationsButKilled(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
result := evaluator.EvaluateFeature(key, &key, "mysplittest3", nil)
Expand All @@ -302,7 +318,9 @@ func TestSplitWithConfigurationsButKilledWithConfigsOnDefault(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
result := evaluator.EvaluateFeature(key, &key, "mysplittest4", nil)
Expand All @@ -324,7 +342,9 @@ func TestMultipleEvaluations(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
splits := []string{"mysplittest", "mysplittest2", "mysplittest3", "mysplittest4", "mysplittest5"}
Expand Down Expand Up @@ -380,7 +400,9 @@ func TestNoConditionMatched(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)

key := "test"
result := evaluator.EvaluateFeature(key, &key, "some", nil)
Expand Down Expand Up @@ -426,7 +448,9 @@ func TestEvaluationByFlagSets(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)
result := evaluator.EvaluateFeatureByFlagSets(key, &key, []string{"set1", "set2", "set3"}, nil)

if result.Evaluations["mysplittest"].Treatment != "off" {
Expand Down Expand Up @@ -489,7 +513,9 @@ func TestEvaluationByFlagSetsASetEmpty(t *testing.T) {
nil,
nil,
nil,
logger)
logger,
syncProxyFeatureFlagsRules,
syncProxyRuleBasedSegmentRules)
result := evaluator.EvaluateFeatureByFlagSets(key, &key, []string{"set2"}, nil)

if len(result.Evaluations) != 0 {
Expand Down
3 changes: 2 additions & 1 deletion engine/grammar/allkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func TestAllKeysMatcher(t *testing.T) {
dto := &dtos.MatcherDTO{
MatcherType: "ALL_KEYS",
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about matcher receive the rule builder so then you only call the BuildMatcher>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test. For rule-based segments, it's receiving a ruleBuilder and calling the method.


matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
}
Expand Down
3 changes: 2 additions & 1 deletion engine/grammar/allofset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func TestAllOfSetMatcher(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down
3 changes: 2 additions & 1 deletion engine/grammar/anyofset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func TestAnyOfSetMatcher(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down
6 changes: 4 additions & 2 deletions engine/grammar/between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func TestBetweenMatcherInt(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down Expand Up @@ -70,8 +71,9 @@ func TestBetweenMatcherDatetime(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down
6 changes: 4 additions & 2 deletions engine/grammar/boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func TestBooleanMatcherTrue(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down Expand Up @@ -56,8 +57,9 @@ func TestBooleanMatcherFalse(t *testing.T) {
Attribute: &attrName,
},
}
ruleBuilder := NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger)

matcher, err := BuildMatcher(dto, nil, logger)
matcher, err := ruleBuilder.BuildMatcher(dto)
if err != nil {
t.Error("There should be no errors when building the matcher")
t.Error(err)
Expand Down
13 changes: 6 additions & 7 deletions engine/grammar/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package grammar
import (
"github.com/splitio/go-split-commons/v6/dtos"
"github.com/splitio/go-split-commons/v6/engine/grammar/datatypes"
"github.com/splitio/go-toolkit/v5/injection"
"github.com/splitio/go-toolkit/v5/logging"
)

Expand All @@ -17,12 +16,12 @@ type Condition struct {
}

// NewCondition instantiates a new Condition struct with appropriate wrappers around dtos and returns it.
func NewCondition(cond *dtos.ConditionDTO, ctx *injection.Context, logger logging.LoggerInterface) (*Condition, error) {
func NewCondition(cond *dtos.ConditionDTO, logger logging.LoggerInterface, ruleBuilder RuleBuilder) (*Condition, error) {
partitions := make([]Partition, 0)
for _, part := range cond.Partitions {
partitions = append(partitions, Partition{PartitionData: part})
}
matcherObjs, err := processMatchers(cond.MatcherGroup.Matchers, ctx, logger)
matcherObjs, err := processMatchers(cond.MatcherGroup.Matchers, logger, ruleBuilder)
if err != nil {
// At this point the only error forwarded is UnsupportedMatcherError
return nil, err
Expand All @@ -37,9 +36,9 @@ func NewCondition(cond *dtos.ConditionDTO, ctx *injection.Context, logger loggin
}, nil
}

func NewRBCondition(cond *dtos.RuleBasedConditionDTO, ctx *injection.Context, logger logging.LoggerInterface) (*Condition, error) {
func NewRBCondition(cond *dtos.RuleBasedConditionDTO, logger logging.LoggerInterface, ruleBuilder RuleBuilder) (*Condition, error) {
partitions := make([]Partition, 0)
matcherObjs, err := processMatchers(cond.MatcherGroup.Matchers, ctx, logger)
matcherObjs, err := processMatchers(cond.MatcherGroup.Matchers, logger, ruleBuilder)
if err != nil {
// At this point the only error forwarded is UnsupportedMatcherError
return nil, err
Expand All @@ -53,10 +52,10 @@ func NewRBCondition(cond *dtos.RuleBasedConditionDTO, ctx *injection.Context, lo
}, nil
}

func processMatchers(condMatchers []dtos.MatcherDTO, ctx *injection.Context, logger logging.LoggerInterface) ([]MatcherInterface, error) {
func processMatchers(condMatchers []dtos.MatcherDTO, logger logging.LoggerInterface, ruleBuilder RuleBuilder) ([]MatcherInterface, error) {
matcherObjs := make([]MatcherInterface, 0)
for _, matcher := range condMatchers {
m, err := BuildMatcher(&matcher, ctx, logger)
m, err := ruleBuilder.BuildMatcher(&matcher)
if err == nil {
matcherObjs = append(matcherObjs, m)
} else {
Expand Down
12 changes: 5 additions & 7 deletions engine/grammar/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/splitio/go-split-commons/v6/dtos"
"github.com/splitio/go-split-commons/v6/engine/grammar/datatypes"
"github.com/splitio/go-toolkit/v5/injection"
"github.com/splitio/go-toolkit/v5/logging"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func TestConditionWrapperObject(t *testing.T) {
},
}

wrapped, err := NewCondition(&condition1, nil, logger)
wrapped, err := NewCondition(&condition1, logger, NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger))

if err != nil {
t.Error("err should be nil")
Expand Down Expand Up @@ -119,7 +118,7 @@ func TestAnotherWrapper(t *testing.T) {
},
}

wrapped, err := NewCondition(&condition1, nil, logger)
wrapped, err := NewCondition(&condition1, logger, NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger))
if err != nil {
t.Error("err should be nil")
}
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestConditionUnsupportedMatcherWrapperObject(t *testing.T) {
},
}

_, err := NewCondition(&condition1, nil, logger)
_, err := NewCondition(&condition1, logger, NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger))

if err == nil {
t.Error("err should not be nil")
Expand Down Expand Up @@ -231,7 +230,7 @@ func TestConditionMatcherWithNilStringWrapperObject(t *testing.T) {
},
}

condition, err := NewCondition(&condition1, nil, logger)
condition, err := NewCondition(&condition1, logger, NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger))

if err != nil {
t.Error("err should be nil")
Expand Down Expand Up @@ -307,11 +306,10 @@ func TestNewRBCondition(t *testing.T) {
}

logger := logging.NewLogger(&logging.LoggerOptions{})
ctx := injection.NewContext()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cond, err := NewRBCondition(tt.condition, ctx, logger)
cond, err := NewRBCondition(tt.condition, logger, NewRuleBuilder(nil, nil, nil, SyncProxyFeatureFlagsRules, SyncProxyRuleBasedSegmentRules, logger))

if tt.wantErr {
assert.Error(t, err)
Expand Down
Loading
Loading