-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.go
240 lines (225 loc) · 6.12 KB
/
range.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2024 Six After, Inc
//
// This source code is licensed under the Apache 2.0 License found in the
// LICENSE file in the root directory of this source tree.
package semver
import (
"fmt"
"regexp"
"strings"
)
// Operator represents a version comparison operator.
//
// Supported Operators:
// - OpEq ("="): Equal
// - OpGt (">"): Greater than
// - OpGte (">="): Greater than or equal
// - OpLt ("<"): Less than
// - OpLte ("<="): Less than or equal
// - OpNeq ("!="): Not equal
type Operator string
const (
OpEq Operator = "="
OpGt Operator = ">"
OpGte Operator = ">="
OpLt Operator = "<"
OpLte Operator = "<="
OpNeq Operator = "!="
)
// Requirement represents a single version requirement.
// It consists of a comparison operator and a target version.
//
// Example:
//
// req := Requirement{Op: OpGt, Ver: semver.MustParse("1.0.0")}
// v := semver.MustParse("1.1.0")
// fmt.Println(req.Contains(v)) // Output: true
type Requirement struct {
Op Operator
Ver Version
}
// VersionRange represents a set of requirements separated by AND (space) and OR (||).
// A VersionRange can be used to check if a Version satisfies it.
//
// Example:
//
// r, err := semver.ParseRange(">1.0.0 <2.0.0 || >=3.0.0 !=4.2.1")
// if err != nil {
// // Handle error
// }
// v := semver.MustParse("1.2.3")
// fmt.Println(r.Contains(v)) // Output: true
type VersionRange struct {
Requirements [][]Requirement
}
// rangeRegex helps to parse individual range tokens.
var rangeRegex = regexp.MustCompile(`^(>=|<=|>|<|=|!=)?\s*([0-9A-Za-z.\-+]+)$`)
// ParseRange parses a range string into a VersionRange struct.
//
// Valid ranges are:
// - "<1.0.0"
// - "<=1.0.0"
// - ">1.0.0"
// - ">=1.0.0"
// - "1.0.0", "=1.0.0", "==1.0.0"
// - "!1.0.0", "!=1.0.0"
//
// Ranges can be combined with logical AND (space-separated) and logical OR (||):
// - ">1.0.0 <2.0.0" matches between both versions.
// - "<2.0.0 || >=3.0.0" matches either version ranges.
//
// Example:
//
// r, err := semver.ParseRange(">1.0.0 <2.0.0")
// if err != nil {
// fmt.Println("Error parsing range:", err)
// return
// }
// v := semver.MustParse("1.5.0")
// fmt.Println(r.Contains(v)) // Output: true
func ParseRange(r string) (*VersionRange, error) {
orParts := strings.Split(r, "||")
var requirements [][]Requirement
for _, part := range orParts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
tokens := strings.Fields(part)
var reqs []Requirement
for _, token := range tokens {
matches := rangeRegex.FindStringSubmatch(token)
if matches == nil {
return nil, fmt.Errorf("invalid range token: %s", token)
}
opStr := matches[1]
verStr := matches[2]
var op Operator
if opStr == "" {
op = OpEq
} else {
op = Operator(opStr)
}
ver, err := Parse(verStr)
if err != nil {
return nil, fmt.Errorf("invalid version in range: %s", verStr)
}
reqs = append(reqs, Requirement{
Op: op,
Ver: ver,
})
}
requirements = append(requirements, reqs)
}
return &VersionRange{
Requirements: requirements,
}, nil
}
// MustParseRange is like ParseRange but panics if the range cannot be parsed.
//
// This function is useful for scenarios where you are certain the input is valid
// and want to avoid handling errors explicitly. However, use it with caution
// in production code, as it will terminate the program if the range is invalid.
//
// Example:
//
// r := semver.MustParseRange(">1.0.0 <2.0.0")
// v := semver.MustParse("1.5.0")
// fmt.Println(r.Contains(v)) // Output: true
func MustParseRange(s string) *VersionRange {
r, err := ParseRange(s)
if err != nil {
panic(`semver: ParseRange(` + s + `): ` + err.Error())
}
return r
}
// Contains checks if a version satisfies the range.
//
// Example:
//
// r, _ := semver.ParseRange(">1.0.0 <2.0.0")
// v := semver.MustParse("1.5.0")
// fmt.Println(r.Contains(v)) // Output: true
func (vr *VersionRange) Contains(v Version) bool {
for _, andReqs := range vr.Requirements {
matchesAll := true
for _, req := range andReqs {
if !req.Contains(v) {
matchesAll = false
break
}
}
if matchesAll {
return true
}
}
return false
}
// Contains checks if a version satisfies the requirement.
//
// Example:
//
// req := Requirement{Op: OpGt, Ver: semver.MustParse("1.0.0")}
// v := semver.MustParse("1.1.0")
// fmt.Println(req.Contains(v)) // Output: true
func (r *Requirement) Contains(v Version) bool {
switch r.Op {
case OpEq:
return v.Equal(r.Ver)
case OpGt:
return v.GreaterThan(r.Ver)
case OpGte:
return v.GreaterThanOrEqual(r.Ver)
case OpLt:
return v.LessThan(r.Ver)
case OpLte:
return v.LessThanOrEqual(r.Ver)
case OpNeq:
return !v.Equal(r.Ver)
default:
return false
}
}
// OR combines the current VersionRange with another VersionRange using logical OR.
//
// Example:
//
// r1, _ := semver.ParseRange(">1.0.0 <2.0.0")
// r2, _ := semver.ParseRange(">=3.0.0 !=4.2.1")
// combined := r1.OR(r2)
// v := semver.MustParse("3.1.0")
// fmt.Println(combined.Contains(v)) // Output: true
func (vr *VersionRange) OR(other *VersionRange) *VersionRange {
combined := &VersionRange{
Requirements: append(vr.Requirements, other.Requirements...),
}
return combined
}
// AND combines the current VersionRange with another VersionRange using logical AND.
//
// This function returns a new VersionRange that represents the intersection of the two ranges.
// It effectively creates a range that only matches versions satisfying both original ranges.
//
// Example:
//
// r1, _ := semver.ParseRange(">1.0.0 <3.0.0")
// r2, _ := semver.ParseRange("!=2.0.3-beta.2")
// combined := r1.AND(r2)
//
// v := semver.MustParse("2.1.0")
// fmt.Println(combined.Contains(v)) // Output: true
//
// v2 := semver.MustParse("2.0.3-beta.2")
// fmt.Println(combined.Contains(v2)) // Output: false
func (vr *VersionRange) AND(other *VersionRange) *VersionRange {
var combinedRequirements [][]Requirement
for _, reqs1 := range vr.Requirements {
for _, reqs2 := range other.Requirements {
combinedReqs := append(reqs1, reqs2...)
combinedRequirements = append(combinedRequirements, combinedReqs)
}
}
return &VersionRange{
Requirements: combinedRequirements,
}
}