Skip to content

Commit a3b4b29

Browse files
authored
Merge pull request #181 from splitio/SDKS-8703-spec-filter
[SDKS-8703] Update splitversionfilter
2 parents 67c86fe + 737cd15 commit a3b4b29

File tree

6 files changed

+111
-20
lines changed

6 files changed

+111
-20
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
6.0.1 (Sep 12, 2024)
2+
- Updated split version filter to support 1.2.
3+
- Fixed healcheck monitor for cases with no segments.
4+
15
6.0.0 (May 14, 2024)
26
- BREAKING CHANGE:
37
- Changed FetchOptions and Fetch API.

engine/grammar/matchers/matchers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const (
6262
MatcherTypeBetweenSemver = "BETWEEN_SEMVER"
6363
// MatcherTypeInListSemver string value
6464
MatcherTypeInListSemver = "IN_LIST_SEMVER"
65+
66+
// MatcherInLargeSegment string value
67+
MatcherInLargeSegment = "IN_LARGE_SEGMENT"
6568
)
6669

6770
// MatcherInterface should be implemented by all matchers

service/api/specs/specversion.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
package specs
22

3+
import "fmt"
4+
35
const (
46
FLAG_V1_0 = "1.0"
57
FLAG_V1_1 = "1.1"
8+
FLAG_V1_2 = "1.2"
69
)
710

811
// Match returns the spec version if it is valid, otherwise it returns nil
9-
func Match(specVersion string) *string {
10-
if specVersion == FLAG_V1_0 || specVersion == FLAG_V1_1 {
11-
return &specVersion
12+
func Match(version string) *string {
13+
switch version {
14+
case FLAG_V1_0:
15+
return &version
16+
case FLAG_V1_1:
17+
return &version
18+
case FLAG_V1_2:
19+
return &version
1220
}
1321
return nil
1422
}
23+
24+
func ParseAndValidate(spec string) (string, error) {
25+
if len(spec) == 0 {
26+
// return default flag spec
27+
return FLAG_V1_0, nil
28+
}
29+
30+
if Match(spec) == nil {
31+
return spec, fmt.Errorf("unsupported flag spec: %s", spec)
32+
}
33+
34+
return spec, nil
35+
}

service/api/specs/specversion_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ func TestMatch(t *testing.T) {
1414
}
1515

1616
specVersion = "1.2"
17+
if Match(specVersion) == nil {
18+
t.Error("Expected 1.2")
19+
}
20+
21+
specVersion = "1.3"
1722
if Match(specVersion) != nil {
1823
t.Error("Expected nil")
1924
}
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
package specs
22

3-
import "github.com/splitio/go-split-commons/v6/engine/grammar/matchers"
3+
import (
4+
"github.com/splitio/go-split-commons/v6/engine/grammar/matchers"
5+
)
46

57
type SplitVersionFilter struct {
6-
excluded map[mkey]struct{}
8+
v1_0 map[string]bool
9+
v1_1 map[string]bool
710
}
811

9-
type mkey struct {
10-
api string
11-
matcher string
12+
func NewSplitVersionFilter() SplitVersionFilter {
13+
v1_1 := map[string]bool{matchers.MatcherInLargeSegment: true}
14+
v1_0 := mergeMaps(map[string]bool{
15+
matchers.MatcherEqualToSemver: true,
16+
matchers.MatcherTypeLessThanOrEqualToSemver: true,
17+
matchers.MatcherTypeGreaterThanOrEqualToSemver: true,
18+
matchers.MatcherTypeBetweenSemver: true,
19+
matchers.MatcherTypeInListSemver: true,
20+
}, v1_1)
21+
22+
return SplitVersionFilter{
23+
v1_0: v1_0,
24+
v1_1: v1_1,
25+
}
1226
}
1327

14-
func NewSplitVersionFilter() SplitVersionFilter {
15-
matchersToExclude := map[mkey]struct{}{
16-
{FLAG_V1_0, matchers.MatcherEqualToSemver}: {},
17-
{FLAG_V1_0, matchers.MatcherTypeLessThanOrEqualToSemver}: {},
18-
{FLAG_V1_0, matchers.MatcherTypeGreaterThanOrEqualToSemver}: {},
19-
{FLAG_V1_0, matchers.MatcherTypeBetweenSemver}: {},
20-
{FLAG_V1_0, matchers.MatcherTypeInListSemver}: {},
28+
func (f *SplitVersionFilter) ShouldFilter(matcher string, apiVersion string) bool {
29+
switch apiVersion {
30+
case FLAG_V1_1:
31+
return f.v1_1[matcher]
32+
case FLAG_V1_0:
33+
return f.v1_0[matcher]
2134
}
2235

23-
return SplitVersionFilter{excluded: matchersToExclude}
36+
return false
2437
}
2538

26-
func (s *SplitVersionFilter) ShouldFilter(matcher string, apiVersion string) bool {
27-
_, ok := s.excluded[mkey{apiVersion, matcher}]
28-
return ok
39+
func mergeMaps(versionMap map[string]bool, toMergeMap map[string]bool) map[string]bool {
40+
for key, value := range toMergeMap {
41+
versionMap[key] = value
42+
}
43+
44+
return versionMap
2945
}

service/api/specs/splitversionfilter_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@ import (
66
"github.com/splitio/go-split-commons/v6/engine/grammar/matchers"
77
)
88

9-
func Test_splitVersionFilter(t *testing.T) {
9+
func TestParseAndValidate(t *testing.T) {
10+
res, err := ParseAndValidate("")
11+
if err != nil || res != FLAG_V1_0 {
12+
t.Error("It should be 1.1")
13+
}
14+
15+
res, err = ParseAndValidate("1.1")
16+
if err != nil || res != FLAG_V1_1 {
17+
t.Error("It should be 1.1")
18+
}
19+
20+
res, err = ParseAndValidate("1.2")
21+
if err != nil || res != FLAG_V1_2 {
22+
t.Error("It should be 1.2")
23+
}
24+
25+
res, err = ParseAndValidate("2.3")
26+
if err == nil || res != "2.3" {
27+
t.Error("Should be a unsupported version")
28+
}
29+
}
30+
31+
func TestsplitVersionFilter(t *testing.T) {
1032
filter := NewSplitVersionFilter()
1133
shouldFilter := filter.ShouldFilter(matchers.MatcherTypeBetweenSemver, FLAG_V1_0)
1234
if !shouldFilter {
@@ -22,4 +44,24 @@ func Test_splitVersionFilter(t *testing.T) {
2244
if shouldFilter {
2345
t.Error("It should not filtered")
2446
}
47+
48+
shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_0)
49+
if !shouldFilter {
50+
t.Error("It should filtered")
51+
}
52+
53+
shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_1)
54+
if !shouldFilter {
55+
t.Error("It should filtered")
56+
}
57+
58+
shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, FLAG_V1_2)
59+
if shouldFilter {
60+
t.Error("It should not filtered")
61+
}
62+
63+
shouldFilter = filter.ShouldFilter(matchers.MatcherInLargeSegment, "4.3")
64+
if shouldFilter {
65+
t.Error("It should not filtered")
66+
}
2567
}

0 commit comments

Comments
 (0)