Skip to content

Commit 32d72e2

Browse files
authored
Merge pull request #206 from splitio/SDKS-9559-fix-flagspec
Improved spec handling
2 parents d1426ab + 1cf706b commit 32d72e2

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

service/api/specs/specversion.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package specs
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/exp/slices"
7+
)
48

59
const (
6-
FLAG_V1_0 = "1.0"
7-
FLAG_V1_1 = "1.1"
8-
FLAG_V1_2 = "1.2"
10+
FLAG_V1_0 = "1.0" // default
11+
FLAG_V1_1 = "1.1" // Semver Matcher
12+
FLAG_V1_2 = "1.2" // Large Segment Matcher
913
)
1014

15+
var flagSpecs = []string{FLAG_V1_0, FLAG_V1_1, FLAG_V1_2}
16+
var Latest = flagSpecs[len(flagSpecs)-1]
17+
1118
// Match returns the spec version if it is valid, otherwise it returns nil
1219
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
20+
ok := slices.Contains(flagSpecs, version)
21+
if !ok {
22+
return nil
2023
}
21-
return nil
24+
25+
return &version
2226
}
2327

2428
func ParseAndValidate(spec string) (string, error) {

service/api/specs/splitversionfilter.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,34 @@ import (
55
)
66

77
type SplitVersionFilter struct {
8-
v1_0 map[string]bool
9-
v1_1 map[string]bool
8+
data map[string]map[string]bool
109
}
1110

1211
func NewSplitVersionFilter() SplitVersionFilter {
13-
v1_1 := map[string]bool{matchers.MatcherTypeInLargeSegment: true}
14-
v1_0 := mergeMaps(map[string]bool{
12+
data := map[string]map[string]bool{
13+
FLAG_V1_1: {matchers.MatcherTypeInLargeSegment: true},
14+
}
15+
16+
data[FLAG_V1_0] = mergeMaps(map[string]bool{
1517
matchers.MatcherEqualToSemver: true,
1618
matchers.MatcherTypeLessThanOrEqualToSemver: true,
1719
matchers.MatcherTypeGreaterThanOrEqualToSemver: true,
1820
matchers.MatcherTypeBetweenSemver: true,
1921
matchers.MatcherTypeInListSemver: true,
20-
}, v1_1)
22+
}, data[FLAG_V1_1])
2123

2224
return SplitVersionFilter{
23-
v1_0: v1_0,
24-
v1_1: v1_1,
25+
data: data,
2526
}
2627
}
2728

2829
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]
30+
matchers, ok := f.data[apiVersion]
31+
if !ok {
32+
return false
3433
}
3534

36-
return false
35+
return matchers[matcher]
3736
}
3837

3938
func mergeMaps(versionMap map[string]bool, toMergeMap map[string]bool) map[string]bool {

service/api/specs/splitversionfilter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestParseAndValidate(t *testing.T) {
2828
}
2929
}
3030

31-
func TestsplitVersionFilter(t *testing.T) {
31+
func TestSplitVersionFilter(t *testing.T) {
3232
filter := NewSplitVersionFilter()
3333
shouldFilter := filter.ShouldFilter(matchers.MatcherTypeBetweenSemver, FLAG_V1_0)
3434
if !shouldFilter {

0 commit comments

Comments
 (0)