Skip to content

Commit

Permalink
Trim unnecessary dependencies for ordered generic constraint. (#4709)
Browse files Browse the repository at this point in the history
A generic test function was introduced in #4515 using
`constraints.Ordered` from golang.org/x/exp/constaints. This
unfortunately introduces further transitive dependencies on packages
such as golang.org/x/tools which has versions with various breaking
changes. This makes it more difficult to pull in nats-server in
downstream projects.

By inlining the trivial `ordered` definition, we can trim this
dependency.

An alternative to this change would be to downgrade the required version
of golang.org/x/exp since the constraints package has been there (and
unchanged) since 2022-02-02.

Signed-off-by: Augusto Roman <[email protected]>
  • Loading branch information
derekcollison authored Oct 25, 2023
2 parents 3f7ad66 + c660e25 commit 9671a1f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/nats-io/nuid v1.0.1
go.uber.org/automaxprocs v1.5.3
golang.org/x/crypto v0.14.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/sys v0.13.0
golang.org/x/time v0.3.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
18 changes: 15 additions & 3 deletions server/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"strings"
"testing"
"time"

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

// DefaultTestOptions are default options for the unit tests.
Expand Down Expand Up @@ -135,7 +133,7 @@ func require_Len(t *testing.T, a, b int) {
}
}

func require_LessThan[T constraints.Ordered](t *testing.T, a, b T) {
func require_LessThan[T ordered](t *testing.T, a, b T) {
t.Helper()
if a >= b {
t.Fatalf("require %v to be less than %v", a, b)
Expand Down Expand Up @@ -330,3 +328,17 @@ func runSolicitLeafServerToURL(surl string) (*Server, *Options) {
o.LeafNode.ReconnectInterval = 100 * time.Millisecond
return RunServer(&o), &o
}

// ordered is a constraint that permits any ordered type.
//
// To avoid a dependency on go1.21+ or golang.org/x/exp, we copy the ordered
// interface def from go 1.21.3:src/cmp/cmp.go (https://pkg.go.dev/cmp#Ordered).
//
// When this repo is updated to go 1.21+, this should be deleted and references
// replaced by cmp.Ordered.
type ordered interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~string
}

0 comments on commit 9671a1f

Please sign in to comment.