forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
90 lines (73 loc) · 1.65 KB
/
version.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"errors"
"fmt"
)
const (
defaultVersionSeparator = "."
defaultVersionPrefix = "v"
)
var (
// DefaultVersion1_0_0 is a useful version to use in tests
DefaultVersion1_0_0 = NewDefaultVersion(1, 0, 0)
errDifferentMajor = errors.New("different major version")
)
type Version interface {
fmt.Stringer
Major() int
Minor() int
Patch() int
// Compare returns a positive number if v > o, 0 if v == o, or a negative number if v < 0.
Compare(o Version) int
}
type version struct {
major, minor, patch int
str string
}
func NewDefaultVersion(major, minor, patch int) Version {
return NewVersion(major, minor, patch, defaultVersionPrefix, defaultVersionSeparator)
}
func NewVersion(major, minor, patch int, prefix, versionSeparator string) Version {
return &version{
major: major,
minor: minor,
patch: patch,
str: fmt.Sprintf(
"%s%d%s%d%s%d",
prefix,
major,
versionSeparator,
minor,
versionSeparator,
patch,
),
}
}
func (v *version) String() string { return v.str }
func (v *version) Major() int { return v.major }
func (v *version) Minor() int { return v.minor }
func (v *version) Patch() int { return v.patch }
// Compare returns a positive number if v > o, 0 if v == o, or a negative number if v < 0.
func (v *version) Compare(o Version) int {
{
vm := v.Major()
om := o.Major()
if vm != om {
return vm - om
}
}
{
vm := v.Minor()
om := o.Minor()
if vm != om {
return vm - om
}
}
{
vp := v.Patch()
op := o.Patch()
return vp - op
}
}