Skip to content

Commit

Permalink
feat: Add new semver targeting operators
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentabtasty committed Nov 4, 2024
1 parent eefd2fd commit 9f22889
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/flagship-io/flagship-common

go 1.21
go 1.22.0

require (
github.com/flagship-io/flagship-proto v0.0.21
github.com/golang/protobuf v1.5.2
github.com/sirupsen/logrus v1.8.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
google.golang.org/protobuf v1.27.1
golang.org/x/mod v0.21.0
google.golang.org/protobuf v1.32.0
)

require (
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
36 changes: 36 additions & 0 deletions targeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/flagship-io/flagship-common/targeting"
protoTargeting "github.com/flagship-io/flagship-proto/targeting"
"golang.org/x/mod/semver"
"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -142,6 +143,30 @@ func targetingMatchOperatorString(operator protoTargeting.Targeting_TargetingOpe
return strings.Contains(strings.ToLower(contextValue), strings.ToLower(targetingValue)), nil
case protoTargeting.Targeting_NOT_CONTAINS:
return !strings.Contains(strings.ToLower(contextValue), strings.ToLower(targetingValue)), nil
case protoTargeting.Targeting_SEMVER_LOWER_THAN:

Check failure on line 146 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_LOWER_THAN

Check failure on line 146 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_LOWER_THAN
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) == -1, nil
case protoTargeting.Targeting_SEMVER_GREATER_THAN:

Check failure on line 150 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_GREATER_THAN

Check failure on line 150 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_GREATER_THAN
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) == 1, nil
case protoTargeting.Targeting_SEMVER_LOWER_THAN_OR_EQUALS:

Check failure on line 154 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_LOWER_THAN_OR_EQUALS

Check failure on line 154 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_LOWER_THAN_OR_EQUALS
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) <= 0, nil
case protoTargeting.Targeting_SEMVER_GREATER_THAN_OR_EQUALS:

Check failure on line 158 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_GREATER_THAN_OR_EQUALS

Check failure on line 158 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_GREATER_THAN_OR_EQUALS
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) >= 0, nil
case protoTargeting.Targeting_SEMVER_EQUALS:

Check failure on line 162 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_EQUALS
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) == 0, nil
case protoTargeting.Targeting_SEMVER_NOT_EQUALS:

Check failure on line 166 in targeting.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

undefined: protoTargeting.Targeting_SEMVER_NOT_EQUALS
return isValidSemver(contextValue) &&
isValidSemver(targetingValue) &&
semver.Compare(getGoSemver(contextValue), getGoSemver(targetingValue)) != 0, nil
// case "regex":
// match, err := regexp.MatchString(targetingValue, contextValue)
// return match, err
Expand All @@ -150,6 +175,17 @@ func targetingMatchOperatorString(operator protoTargeting.Targeting_TargetingOpe
}
}

func getGoSemver(v string) string {
if string(v[0]) != "v" {
return "v" + v
}
return v
}

func isValidSemver(v string) bool {
return semver.IsValid(getGoSemver(v))
}

func targetingMatchOperatorNumber(operator protoTargeting.Targeting_TargetingOperator, targetingValue float64, contextValue float64) (bool, error) {
switch operator {
case protoTargeting.Targeting_LOWER_THAN:
Expand Down
54 changes: 54 additions & 0 deletions targeting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,60 @@ func TestStringTargeting(t *testing.T) {
testTargetingString(targetingProto.Targeting_STARTS_WITH, "d", "abc", t, false, false)
testTargetingString(targetingProto.Targeting_STARTS_WITH, "c", "abc", t, false, false)
testTargetingString(targetingProto.Targeting_STARTS_WITH, "", "abc", t, true, false)

testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10.3", "2.10.3", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10.3-alpha", "2.10.3-beta", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10.3-beta", "2.10.3-beta", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10.3", "2.10.4", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10", "2.10.3", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.10", "2.10.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2", "2.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2.1", "2.1", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2", "2", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "2", "3", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "invalid", "invalid", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_EQUALS, "1.0.0-alpha+00", "v1.0.0-alpha+01", t, true, false)

testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10.3", "2.10.3", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10.3-alpha", "2.10.3-beta", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10.3-beta", "2.10.3-beta", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10.3", "2.10.4", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10", "2.10.3", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.10", "2.10.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2", "2.0.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2.1", "2.1", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2", "2", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "2", "3", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "invalid", "invalid2", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_NOT_EQUALS, "1.0.0-alpha+00", "v1.0.0-alpha+01", t, false, false)

testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "2.0.0", "2.0.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "2.0.1", "2.0.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "2.0.0", "2.0.1", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "1.0.0-alpha", "1.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "1.0.0-alpha", "1.0.0-alpha.1", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN, "1.0.0-alpha.beta", "1.0.0-beta", t, true, false)

testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "2.0.0", "2.0.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "2.0.1", "2.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "2.0.0", "2.0.1", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "1.0.0", "1.0.0-alpha", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "1.0.0-alpha.1", "1.0.0-alpha", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN, "1.0.0-beta", "1.0.0-alpha.beta", t, true, false)

testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "2.0.0", "2.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "2.0.1", "2.0.0", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "2.0.0", "2.0.1", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "1.0.0-alpha", "1.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "1.0.0-alpha", "1.0.0-alpha.1", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_GREATER_THAN_OR_EQUALS, "1.0.0-alpha.beta", "1.0.0-beta", t, true, false)

testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "2.0.0", "2.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "2.0.1", "2.0.0", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "2.0.0", "2.0.1", t, false, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "1.0.0", "1.0.0-alpha", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "1.0.0-alpha.1", "1.0.0-alpha", t, true, false)
testTargetingString(targetingProto.Targeting_SEMVER_LOWER_THAN_OR_EQUALS, "1.0.0-beta", "1.0.0-alpha.beta", t, true, false)
}

// TestListStringTargeting checks all possible string list targeting
Expand Down

0 comments on commit 9f22889

Please sign in to comment.