3 | 3 |
"fmt"
|
4 | 4 |
"reflect"
|
5 | 5 |
"regexp"
|
|
6 |
"sort"
|
6 | 7 |
"strings"
|
7 | 8 |
)
|
8 | 9 |
|
|
10 | 11 |
// ">= 1.0".
|
11 | 12 |
type Constraint struct {
|
12 | 13 |
f constraintFunc
|
|
14 |
op operator
|
13 | 15 |
check *Version
|
14 | 16 |
original string
|
|
17 |
}
|
|
18 |
|
|
19 |
func (c *Constraint) Equals(con *Constraint) bool {
|
|
20 |
return c.op == con.op && c.check.Equal(con.check)
|
15 | 21 |
}
|
16 | 22 |
|
17 | 23 |
// Constraints is a slice of constraints. We make a custom type so that
|
|
20 | 26 |
|
21 | 27 |
type constraintFunc func(v, c *Version) bool
|
22 | 28 |
|
23 | |
var constraintOperators map[string]constraintFunc
|
|
29 |
var constraintOperators map[string]constraintOperation
|
|
30 |
|
|
31 |
type constraintOperation struct {
|
|
32 |
op operator
|
|
33 |
f constraintFunc
|
|
34 |
}
|
24 | 35 |
|
25 | 36 |
var constraintRegexp *regexp.Regexp
|
26 | 37 |
|
27 | 38 |
func init() {
|
28 | |
constraintOperators = map[string]constraintFunc{
|
29 | |
"": constraintEqual,
|
30 | |
"=": constraintEqual,
|
31 | |
"!=": constraintNotEqual,
|
32 | |
">": constraintGreaterThan,
|
33 | |
"<": constraintLessThan,
|
34 | |
">=": constraintGreaterThanEqual,
|
35 | |
"<=": constraintLessThanEqual,
|
36 | |
"~>": constraintPessimistic,
|
|
39 |
constraintOperators = map[string]constraintOperation{
|
|
40 |
"": {op: equal, f: constraintEqual},
|
|
41 |
"=": {op: equal, f: constraintEqual},
|
|
42 |
"!=": {op: notEqual, f: constraintNotEqual},
|
|
43 |
">": {op: greaterThan, f: constraintGreaterThan},
|
|
44 |
"<": {op: lessThan, f: constraintLessThan},
|
|
45 |
">=": {op: greaterThanEqual, f: constraintGreaterThanEqual},
|
|
46 |
"<=": {op: lessThanEqual, f: constraintLessThanEqual},
|
|
47 |
"~>": {op: pessimistic, f: constraintPessimistic},
|
37 | 48 |
}
|
38 | 49 |
|
39 | 50 |
ops := make([]string, 0, len(constraintOperators))
|
|
65 | 76 |
return Constraints(result), nil
|
66 | 77 |
}
|
67 | 78 |
|
|
79 |
// MustConstraints is a helper that wraps a call to a function
|
|
80 |
// returning (Constraints, error) and panics if error is non-nil.
|
|
81 |
func MustConstraints(c Constraints, err error) Constraints {
|
|
82 |
if err != nil {
|
|
83 |
panic(err)
|
|
84 |
}
|
|
85 |
|
|
86 |
return c
|
|
87 |
}
|
|
88 |
|
68 | 89 |
// Check tests if a version satisfies all the constraints.
|
69 | 90 |
func (cs Constraints) Check(v *Version) bool {
|
70 | 91 |
for _, c := range cs {
|
|
76 | 97 |
return true
|
77 | 98 |
}
|
78 | 99 |
|
|
100 |
// Equals compares Constraints with other Constraints
|
|
101 |
// for equality. This may not represent logical equivalence
|
|
102 |
// of compared constraints.
|
|
103 |
// e.g. even though '>0.1,>0.2' is logically equivalent
|
|
104 |
// to '>0.2' it is *NOT* treated as equal.
|
|
105 |
//
|
|
106 |
// Missing operator is treated as equal to '=', whitespaces
|
|
107 |
// are ignored and constraints are sorted before comaparison.
|
|
108 |
func (cs Constraints) Equals(c Constraints) bool {
|
|
109 |
if len(cs) != len(c) {
|
|
110 |
return false
|
|
111 |
}
|
|
112 |
|
|
113 |
// make copies to retain order of the original slices
|
|
114 |
left := make(Constraints, len(cs))
|
|
115 |
copy(left, cs)
|
|
116 |
sort.Stable(left)
|
|
117 |
right := make(Constraints, len(c))
|
|
118 |
copy(right, c)
|
|
119 |
sort.Stable(right)
|
|
120 |
|
|
121 |
// compare sorted slices
|
|
122 |
for i, con := range left {
|
|
123 |
if !con.Equals(right[i]) {
|
|
124 |
return false
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
return true
|
|
129 |
}
|
|
130 |
|
|
131 |
func (cs Constraints) Len() int {
|
|
132 |
return len(cs)
|
|
133 |
}
|
|
134 |
|
|
135 |
func (cs Constraints) Less(i, j int) bool {
|
|
136 |
if cs[i].op < cs[j].op {
|
|
137 |
return true
|
|
138 |
}
|
|
139 |
if cs[i].op > cs[j].op {
|
|
140 |
return false
|
|
141 |
}
|
|
142 |
|
|
143 |
return cs[i].check.LessThan(cs[j].check)
|
|
144 |
}
|
|
145 |
|
|
146 |
func (cs Constraints) Swap(i, j int) {
|
|
147 |
cs[i], cs[j] = cs[j], cs[i]
|
|
148 |
}
|
|
149 |
|
79 | 150 |
// Returns the string format of the constraints
|
80 | 151 |
func (cs Constraints) String() string {
|
81 | 152 |
csStr := make([]string, len(cs))
|
|
106 | 177 |
return nil, err
|
107 | 178 |
}
|
108 | 179 |
|
|
180 |
cop := constraintOperators[matches[1]]
|
|
181 |
|
109 | 182 |
return &Constraint{
|
110 | |
f: constraintOperators[matches[1]],
|
|
183 |
f: cop.f,
|
|
184 |
op: cop.op,
|
111 | 185 |
check: check,
|
112 | 186 |
original: v,
|
113 | 187 |
}, nil
|
|
137 | 211 |
// Constraint functions
|
138 | 212 |
//-------------------------------------------------------------------
|
139 | 213 |
|
|
214 |
type operator rune
|
|
215 |
|
|
216 |
const (
|
|
217 |
equal operator = '='
|
|
218 |
notEqual operator = '≠'
|
|
219 |
greaterThan operator = '>'
|
|
220 |
lessThan operator = '<'
|
|
221 |
greaterThanEqual operator = '≥'
|
|
222 |
lessThanEqual operator = '≤'
|
|
223 |
pessimistic operator = '~'
|
|
224 |
)
|
|
225 |
|
140 | 226 |
func constraintEqual(v, c *Version) bool {
|
141 | 227 |
return v.Equal(c)
|
142 | 228 |
}
|