/
versionutils.go
45 lines (38 loc) · 1.12 KB
/
versionutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package api
// Generic version handling
import (
"fmt"
"golang.org/x/exp/constraints"
"github.com/neondatabase/autoscaling/pkg/util"
)
// VersionRange is a helper type to represent a range of versions.
//
// The bounds are inclusive, representing all versions v with Min <= v <= Max.
//
// This type is sent directly to the monitor during the creation of a new
// Dispatcher as part of figuring out which protocol to use.
type VersionRange[V constraints.Ordered] struct {
Min V `json:"min"`
Max V `json:"max"`
}
func (r VersionRange[V]) String() string {
if r.Min == r.Max {
return fmt.Sprintf("%v", r.Min)
} else {
return fmt.Sprintf("%v to %v", r.Min, r.Max)
}
}
// LatestSharedVersion returns the latest version covered by both VersionRanges, if there is one.
//
// If either range is invalid, or no such version exists (i.e. the ranges are disjoint), then the
// returned values will be (0, false).
func (r VersionRange[V]) LatestSharedVersion(cmp VersionRange[V]) (_ V, ok bool) {
max := util.Min(r.Max, cmp.Max)
min := util.Max(r.Min, cmp.Min)
if max >= min {
return max, true
} else {
var v V
return v, false
}
}