-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemver.go
More file actions
105 lines (95 loc) · 2.93 KB
/
Copy pathsemver.go
File metadata and controls
105 lines (95 loc) · 2.93 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package semver
import (
"fmt"
)
// SemVer represents [Semantic Versioning Specification].
// SemVer's [zero value] is "0.0.0" version.
//
// [Semantic Versioning Specification]: https://semver.org/#semantic-versioning-specification-semver
// [zero value]: https://go.dev/ref/spec#The_zero_value
type SemVer struct {
// Major contains SemVer's [major version].
//
// [major version]: https://semver.org/#spec-item-8
Major int64
// Minor contains SemVer's [minor version].
//
// [minor version]: https://semver.org/#spec-item-7
Minor int64
// Patch contains SemVer's [patch version].
//
// [patch version]: https://semver.org/#spec-item-6
Patch int64
// PreRelease contains SemVer's [pre-release version].
//
// [pre-release version]: https://semver.org/#spec-item-9
PreRelease string
// Build contains SemVer's [build metadata].
//
// [build metadata]: https://semver.org/#spec-item-10
Build string
}
// String implements the [fmt.Stringer] interface.
// String does not validate 'v', so an invalid [SemVer]
// (e.g. with negative fields) may produce an invalid version string.
// Use [SemVer.IsValid] to check 'v' beforehand.
func (v SemVer) String() string {
s := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
if len(v.PreRelease) > 0 {
s += "-" + v.PreRelease
}
if len(v.Build) > 0 {
s += "+" + v.Build
}
return s
}
// MarshalText implements the [encoding.TextMarshaler] interface.
// Like [SemVer.String], MarshalText does not validate 'v' and never
// returns an error; an invalid [SemVer] may marshal to invalid text.
// Use [SemVer.IsValid] to check 'v' beforehand.
func (v SemVer) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}
// UnmarshalText implements the [encoding.TextUnmarshaler] interface.
func (v *SemVer) UnmarshalText(text []byte) error {
sv, err := Parse(string(text))
if err != nil {
return err
}
*v = sv
return nil
}
// IsValid reports whether 'v' is [valid].
//
// [valid]: https://semver.org/#semantic-versioning-specification-semver
func (v SemVer) IsValid() bool {
return Valid(v) == nil
}
// CompareTo [compares] 'v' with 'other'.
// CompareTo returns -1 if 'v' is less than 'other', 0 if 'v' is equal to 'other', 1 if 'v' is more than 'other'.
//
// [compares]: https://semver.org/#spec-item-11
func (v SemVer) CompareTo(other SemVer) (int, error) {
return Compare(v, other)
}
// LessThan reports whether 'v' is [less] than 'other'.
//
// [less]: https://semver.org/#spec-item-11
func (v SemVer) LessThan(other SemVer) (bool, error) {
r, err := Compare(v, other)
return r < 0, err
}
// EqualTo reports whether 'v' is [equal] to 'other'.
//
// [equal]: https://semver.org/#spec-item-11
func (v SemVer) EqualTo(other SemVer) (bool, error) {
r, err := Compare(v, other)
return r == 0, err
}
// MoreThan reports whether 'v' is [more] than 'other'.
//
// [more]: https://semver.org/#spec-item-11
func (v SemVer) MoreThan(other SemVer) (bool, error) {
r, err := Compare(v, other)
return r > 0, err
}