Skip to content

Improved spec handling #206

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

Merged
merged 5 commits into from
Apr 24, 2025
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
28 changes: 16 additions & 12 deletions service/api/specs/specversion.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package specs

import "fmt"
import (
"fmt"

"golang.org/x/exp/slices"
)

const (
FLAG_V1_0 = "1.0"
FLAG_V1_1 = "1.1"
FLAG_V1_2 = "1.2"
FLAG_V1_0 = "1.0" // default
FLAG_V1_1 = "1.1" // Semver Matcher
FLAG_V1_2 = "1.2" // Large Segment Matcher
)

var flagSpecs = []string{FLAG_V1_0, FLAG_V1_1, FLAG_V1_2}
var Latest = flagSpecs[len(flagSpecs)-1]

// Match returns the spec version if it is valid, otherwise it returns nil
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
ok := slices.Contains(flagSpecs, version)
if !ok {
return nil
}
return nil

return &version
}

func ParseAndValidate(spec string) (string, error) {
Expand Down
25 changes: 12 additions & 13 deletions service/api/specs/splitversionfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@ import (
)

type SplitVersionFilter struct {
v1_0 map[string]bool
v1_1 map[string]bool
data map[string]map[string]bool
}

func NewSplitVersionFilter() SplitVersionFilter {
v1_1 := map[string]bool{matchers.MatcherTypeInLargeSegment: true}
v1_0 := mergeMaps(map[string]bool{
data := map[string]map[string]bool{
FLAG_V1_1: {matchers.MatcherTypeInLargeSegment: true},
}

data[FLAG_V1_0] = mergeMaps(map[string]bool{
matchers.MatcherEqualToSemver: true,
matchers.MatcherTypeLessThanOrEqualToSemver: true,
matchers.MatcherTypeGreaterThanOrEqualToSemver: true,
matchers.MatcherTypeBetweenSemver: true,
matchers.MatcherTypeInListSemver: true,
}, v1_1)
}, data[FLAG_V1_1])

return SplitVersionFilter{
v1_0: v1_0,
v1_1: v1_1,
data: data,
}
}

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]
matchers, ok := f.data[apiVersion]
if !ok {
return false
}

return false
return matchers[matcher]
}

func mergeMaps(versionMap map[string]bool, toMergeMap map[string]bool) map[string]bool {
Expand Down
2 changes: 1 addition & 1 deletion service/api/specs/splitversionfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestParseAndValidate(t *testing.T) {
}
}

func TestsplitVersionFilter(t *testing.T) {
func TestSplitVersionFilter(t *testing.T) {
filter := NewSplitVersionFilter()
shouldFilter := filter.ShouldFilter(matchers.MatcherTypeBetweenSemver, FLAG_V1_0)
if !shouldFilter {
Expand Down
Loading