diff --git a/CHANGES b/CHANGES index e27f5438..d0d3228c 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +6.0.1 (Sep 12, 2024) +- Updated split version filter to support 1.2. +- Fixed healcheck monitor for cases with no segments. + 6.0.0 (May 14, 2024) - BREAKING CHANGE: - Changed FetchOptions and Fetch API. diff --git a/engine/grammar/matchers/matchers.go b/engine/grammar/matchers/matchers.go index 92ee972a..d8ca62c4 100644 --- a/engine/grammar/matchers/matchers.go +++ b/engine/grammar/matchers/matchers.go @@ -62,6 +62,9 @@ const ( MatcherTypeBetweenSemver = "BETWEEN_SEMVER" // MatcherTypeInListSemver string value MatcherTypeInListSemver = "IN_LIST_SEMVER" + + // MatcherInLargeSegment string value + MatcherInLargeSegment = "IN_LARGE_SEGMENT" ) // MatcherInterface should be implemented by all matchers diff --git a/service/api/specs/specversion.go b/service/api/specs/specversion.go index 95ba6c8c..78fafcee 100644 --- a/service/api/specs/specversion.go +++ b/service/api/specs/specversion.go @@ -1,14 +1,35 @@ package specs +import "fmt" + const ( FLAG_V1_0 = "1.0" FLAG_V1_1 = "1.1" + FLAG_V1_2 = "1.2" ) // Match returns the spec version if it is valid, otherwise it returns nil -func Match(specVersion string) *string { - if specVersion == FLAG_V1_0 || specVersion == FLAG_V1_1 { - return &specVersion +func Match(version string) *string { + switch version { + case FLAG_V1_0: + return &version + case FLAG_V1_1: + return &version + case FLAG_V1_2: + return &version } return nil } + +func ParseAndValidate(spec string) (string, error) { + if len(spec) == 0 { + // return default flag spec + return FLAG_V1_0, nil + } + + if Match(spec) == nil { + return spec, fmt.Errorf("unsupported flag spec: %s", spec) + } + + return spec, nil +} diff --git a/service/api/specs/specversion_test.go b/service/api/specs/specversion_test.go index 594fbbfe..58865c7a 100644 --- a/service/api/specs/specversion_test.go +++ b/service/api/specs/specversion_test.go @@ -14,6 +14,11 @@ func TestMatch(t *testing.T) { } specVersion = "1.2" + if Match(specVersion) == nil { + t.Error("Expected 1.2") + } + + specVersion = "1.3" if Match(specVersion) != nil { t.Error("Expected nil") } diff --git a/service/api/specs/splitversionfilter.go b/service/api/specs/splitversionfilter.go index aaea9b84..4aadb78c 100644 --- a/service/api/specs/splitversionfilter.go +++ b/service/api/specs/splitversionfilter.go @@ -1,29 +1,45 @@ package specs -import "github.com/splitio/go-split-commons/v6/engine/grammar/matchers" +import ( + "github.com/splitio/go-split-commons/v6/engine/grammar/matchers" +) type SplitVersionFilter struct { - excluded map[mkey]struct{} + v1_0 map[string]bool + v1_1 map[string]bool } -type mkey struct { - api string - matcher string +func NewSplitVersionFilter() SplitVersionFilter { + v1_1 := map[string]bool{matchers.MatcherInLargeSegment: true} + v1_0 := mergeMaps(map[string]bool{ + matchers.MatcherEqualToSemver: true, + matchers.MatcherTypeLessThanOrEqualToSemver: true, + matchers.MatcherTypeGreaterThanOrEqualToSemver: true, + matchers.MatcherTypeBetweenSemver: true, + matchers.MatcherTypeInListSemver: true, + }, v1_1) + + return SplitVersionFilter{ + v1_0: v1_0, + v1_1: v1_1, + } } -func NewSplitVersionFilter() SplitVersionFilter { - matchersToExclude := map[mkey]struct{}{ - {FLAG_V1_0, matchers.MatcherEqualToSemver}: {}, - {FLAG_V1_0, matchers.MatcherTypeLessThanOrEqualToSemver}: {}, - {FLAG_V1_0, matchers.MatcherTypeGreaterThanOrEqualToSemver}: {}, - {FLAG_V1_0, matchers.MatcherTypeBetweenSemver}: {}, - {FLAG_V1_0, matchers.MatcherTypeInListSemver}: {}, +func (f *SplitVersionFilter) ShouldFilter(matcher string, apiVersion string) bool { + switch apiVersion { + case FLAG_V1_1: + return f.v1_1[matcher] + case FLAG_V1_0: + return f.v1_0[matcher] } - return SplitVersionFilter{excluded: matchersToExclude} + return false } -func (s *SplitVersionFilter) ShouldFilter(matcher string, apiVersion string) bool { - _, ok := s.excluded[mkey{apiVersion, matcher}] - return ok +func mergeMaps(versionMap map[string]bool, toMergeMap map[string]bool) map[string]bool { + for key, value := range toMergeMap { + versionMap[key] = value + } + + return versionMap } diff --git a/service/api/specs/splitversionfilter_test.go b/service/api/specs/splitversionfilter_test.go index 8d076783..1f0f358d 100644 --- a/service/api/specs/splitversionfilter_test.go +++ b/service/api/specs/splitversionfilter_test.go @@ -6,7 +6,29 @@ import ( "github.com/splitio/go-split-commons/v6/engine/grammar/matchers" ) -func Test_splitVersionFilter(t *testing.T) { +func TestParseAndValidate(t *testing.T) { + res, err := ParseAndValidate("") + if err != nil || res != FLAG_V1_0 { + t.Error("It should be 1.1") + } + + res, err = ParseAndValidate("1.1") + if err != nil || res != FLAG_V1_1 { + t.Error("It should be 1.1") + } + + res, err = ParseAndValidate("1.2") + if err != nil || res != FLAG_V1_2 { + t.Error("It should be 1.2") + } + + res, err = ParseAndValidate("2.3") + if err == nil || res != "2.3" { + t.Error("Should be a unsupported version") + } +} + +func TestsplitVersionFilter(t *testing.T) { filter := NewSplitVersionFilter() shouldFilter := filter.ShouldFilter(matchers.MatcherTypeBetweenSemver, FLAG_V1_0) if !shouldFilter { @@ -22,4 +44,24 @@ func Test_splitVersionFilter(t *testing.T) { if shouldFilter { t.Error("It should not filtered") } + + shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_0) + if !shouldFilter { + t.Error("It should filtered") + } + + shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_1) + if !shouldFilter { + t.Error("It should filtered") + } + + shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_2) + if shouldFilter { + t.Error("It should not filtered") + } + + shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, "4.3") + if shouldFilter { + t.Error("It should not filtered") + } }