-
Notifications
You must be signed in to change notification settings - Fork 2
/
relaxed_version.go
105 lines (91 loc) · 3.32 KB
/
relaxed_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package semver
import "fmt"
// RelaxedVersion allows any possible version string. If the version does not comply
// with semantic versioning it is saved as-is and only Equal comparison will match.
type RelaxedVersion struct {
customversion []byte
version *Version
}
// WarnInvalidVersionWhenParsingRelaxed must be set to true to show warnings while
// parsing RelaxedVersion if an invalid semver string is found. This allows a soft
// transition to strict semver
var WarnInvalidVersionWhenParsingRelaxed = false
// ParseRelaxed parse a RelaxedVersion
func ParseRelaxed(in string) *RelaxedVersion {
v, err := Parse(in)
if err == nil {
return &RelaxedVersion{version: v}
}
if WarnInvalidVersionWhenParsingRelaxed {
fmt.Printf("WARNING invalid semver version %s: %s\n", in, err)
}
return &RelaxedVersion{customversion: []byte(in[:])}
}
func (v *RelaxedVersion) String() string {
if v == nil {
return ""
}
if v.version != nil {
return v.version.String()
}
return string(v.customversion)
}
// NormalizedString return a string representation of the version that is
// normalized (always have a major, minor and patch version when semver compliant).
// This is useful to be used in maps and other places where the version is used as a key.
func (v *RelaxedVersion) NormalizedString() NormalizedString {
if v == nil {
return ""
}
if v.version != nil {
return v.version.NormalizedString()
}
return NormalizedString(v.customversion)
}
// CompareTo compares the RelaxedVersion with the one passed as parameter.
// Returns -1, 0 or 1 if the version is respectively less than, equal
// or greater than the compared Version
func (v *RelaxedVersion) CompareTo(u *RelaxedVersion) int {
if v.version == nil && u.version == nil {
return compareAlpha(v.customversion, u.customversion)
}
if v.version == nil {
return -1
}
if u.version == nil {
return 1
}
return v.version.CompareTo(u.version)
}
// LessThan returns true if the RelaxedVersion is less than the RelaxedVersion passed as parameter
func (v *RelaxedVersion) LessThan(u *RelaxedVersion) bool {
return v.CompareTo(u) < 0
}
// LessThanOrEqual returns true if the RelaxedVersion is less than or equal to the RelaxedVersion passed as parameter
func (v *RelaxedVersion) LessThanOrEqual(u *RelaxedVersion) bool {
return v.CompareTo(u) <= 0
}
// Equal returns true if the RelaxedVersion is equal to the RelaxedVersion passed as parameter
func (v *RelaxedVersion) Equal(u *RelaxedVersion) bool {
return v.CompareTo(u) == 0
}
// GreaterThan returns true if the RelaxedVersion is greater than the RelaxedVersion passed as parameter
func (v *RelaxedVersion) GreaterThan(u *RelaxedVersion) bool {
return v.CompareTo(u) > 0
}
// GreaterThanOrEqual returns true if the RelaxedVersion is greater than or equal to the RelaxedVersion passed as parameter
func (v *RelaxedVersion) GreaterThanOrEqual(u *RelaxedVersion) bool {
return v.CompareTo(u) >= 0
}
// CompatibleWith returns true if the RelaxedVersion is compatible with the RelaxedVersion passed as paramater
func (v *RelaxedVersion) CompatibleWith(u *RelaxedVersion) bool {
if v.version != nil && u.version != nil {
return v.version.CompatibleWith(u.version)
}
return v.Equal(u)
}